Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

serialseesion to read write rs232 in c#

hello,

i am trying to read and write to a serial port, manually everything works, when i do the following:

            SerialSession handle = new SerialSession("ASRL7::INSTR");
            handle.BaudRate = 9600;
            handle.Parity = Parity.None;
            handle.StopBits = StopBitType.One;
            handle.DataBits = 8;
            handle.FlowControl = FlowControlTypes.None;

handle.Write("R\r\n");
handle.ReadByteArray();\\succeed
handle.Write("R\r\n");
handle.ReadByteArray();\\fails

 the second read fails with the following exception :

Timeout expired before operation completed.  VISA error code -1073807339 

trace:

at NationalInstruments.VisaNS.Internal.ErrorChecker.CheckStatusAndThrow(Int32 status, VisaHandle visaObject)
at NationalInstruments.VisaNS.Session.CheckStatusAndThrow(Int32 status)
at NationalInstruments.VisaNS.Internal.MessageBasedSessionImpl.ReadByteArray(Int32 countToRead)
at NationalInstruments.VisaNS.MessageBasedSession.ReadByteArray()

 

the funny thing is that once it fails the next run fails from the first read and the only way to "release" the com is to open it with another software (like TeraTerm) .

 

i am running with vs2012, NationalInstruments.VisaNS:13.0.45.167, NationalInstruments.Common:13.0.40.188

 

i have been trying for days to get it working and no success (looked at a few examples which have the same simple reads and write- https://forums.ni.com/t5/Example-Code/How-to-Perform-Basic-Serial-Writes-and-Reads-Using-NI-VISA-in/...https://forums.ni.com/t5/Measurement-Studio-for-VC/Need-SerialSession-Example-for-VC-NET/td-p/156319)

 

thanks, mosh.

 

0 Kudos
Message 1 of 3
(5,687 Views)

Hi mosh,

 

I hope you're doing well. 

 

Have you tried putting a wait to change the timing of the program? Many times, this will help prevent your VISA from timing out if there is too much data read. If this doesn't work for you, can you trying reading only one byte at a time to determine you are trying to read too many bytes? If this works, you can keep increasing the number of bytes to read until you get the timeout error again. This will tell you how many bytes the command sends back.

 

I hope this helps.

 

Regards,

 

Anna L

Applications Engineer
National Instruments
0 Kudos
Message 2 of 3
(5,669 Views)

thanks,

i found the problem.

the read operation fails if it doesnt read the number of bytes until timeout (logical) but doesnt fill the buffer with what it has successfully read (not so good for me)

so i found two solutions:

1. stream.Handle.TerminationCharacter = (byte)'\r'; needed to avoid timeouts - actually i have found a bug that even if TerminationCharacterEnabled==false it still stops onTerminationCharacter.

 

2. i have implemented a byte by byte read with try catch and this way i can return the bytes read before timing out.

 

0 Kudos
Message 3 of 3
(5,658 Views)