11-11-2013
03:11 AM
- last edited on
06-24-2025
08:57 AM
by
Content Cleaner
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.
11-11-2013 03:47 PM
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
11-12-2013 08:15 AM
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.