Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Determining how many bytes are available to read from a visa serial port.

In labview under visa there is a number of bytes at serial port property. How do I access this from c# if I'm using ni visa? When I use the ReadString() function it throws an exception unless I specify this number of bytes. Is there a way to call Readstring to report only what is in the buffer without having to worry about how many bytes to read?

 

 

0 Kudos
Message 1 of 7
(5,131 Views)

VB uses SerialPort1.BytesToRead.   I would guess there is a C# equivalent.

0 Kudos
Message 2 of 7
(5,109 Views)

I need to use the visa serial ports because I want to make my drivers able to use any available interface on the fly.

0 Kudos
Message 3 of 7
(5,102 Views)

Hi,

 

I don't know if this will help, but when I use a Visa Serial Session with C# I set the termimnation character property to true

and select a terminating character. In this case char 13.

 

Curt

 

private

private int m_comport = 2;

private int m_baudrate = 19200;

private int m_timeout = 10000;

SerialSession m_session;
public bool OpenSession()
        {

            try
            {

                m_session = new SerialSession("ASRL" + m_comport + "::INSTR");
                m_session.BaudRate = m_baudrate;
                m_session.StopBits = StopBitType.One;
                m_session.Parity = Parity.None;
                m_session.FlowControl = FlowControlTypes.None;
                m_session.Timeout = m_timeout;
                m_session.TerminationCharacter = 13;
                m_session.TerminationCharacterEnabled = true;
                return true;

            }
            catch (NationalInstruments.VisaNS.VisaException visaex)
            {
                Trace.WriteLine("OpenSession VisaException: " + visaex.Message);
                return false;
            }
            catch (Exception ex)
            {
                Trace.WriteLine("OpenSession Exception: " + ex.Message);
                return false;
            }
        }

//and then read it


strReceive = m_session.ReadString();

 

0 Kudos
Message 4 of 7
(5,081 Views)

Hi Steve,

 

Take a look at the following VISA function:

 

viGetAttribute (, VI_ATTR_ASRL_AVAIL_NUM, );

 

You may need to change the parameters accordingly, but use the NI-VISA Help as a reference (Start->All Programs->National Instruments->VISA).

Best regards,
Rohan B
0 Kudos
Message 5 of 7
(5,070 Views)

So far Brohan's response is the closest to what I'm looking for. This looks like a CVI parameter though and I need to know how to access it in C#. The other answers don't suffice because they are serial port specific and I need code that allows me to interchange protocals freely.

0 Kudos
Message 6 of 7
(5,066 Views)

Check out the .NET help for VISA from the same location. I found this function call:

 

public byte GetAttributeByte( AttributeType attribute );

 

You can use the AsrlAvailNum attribute. 

Best regards,
Rohan B
0 Kudos
Message 7 of 7
(5,036 Views)