Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

c# VISANS serialsession.query() help

Hi all,

 

I'm writing a class library to facilitate communication with instruments utilizing the Modbus Application Protocol that are connected to a NI cRIO-9073 via a serial-to-ethernet cable plugged into an NI 9870 DAQ card that is plugged into the cRIO. The specific instrument I am talking to is an MKS Optitemp Heat Exchanger, which uses the RTU serial transmission mode (http://modbus.org/docs/PI_MBUS_300.pdf).

 

I've been successful in communicating with it, using the Query(byte[] dataToWrite, int countToRead) method of the SerialSession class inherited from the MessageBasedSession class. However, I need to specify the exact number of bytes to read, or else I get the following VisaException:

 

"Timeout expired before operation completed. VISA error code -1073807339 (0xBFFF0015), ErrorTimeout"

 

Unfortunately, I don't always know how many bytes the Query is going to return. Namely, if the slave returns an error, the response is fewer bytes than expected, throwing the above exception. I want to be able to recover the details of the error message for debugging purposes, but right now am stuck with just reporting a generic error message if a VisaException is caught.

 

I've tried the Query( byte[] dataToWrite) overload, but that results in a timeout every time. The Modbus RTU protocol specifies that each message be terminated with "a silent interval of at least 3.5 character time", and I don't know how to set the SerialSession.TerminationCharacter property to reflect this. 

 

Is there a way I can read the bytes already in the buffer, even if the buffer isn't completely filled?

The only workaround I can think of is sending the same query multiple times, each time incrementing the countToRead by 1, but there must be a better way.

 

Thanks,

Joe

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

Hi jgreenberg,

 

It may be better to communicate serially via VISA rather than creating a SerialSession. There are VISA examples available within Visual Studio that may be of assistance. To access these examples, from the top menu bar in Visual Studio, click Measurement Studio->Measurement Studio Examples.

 

Here is an example for how to perform basic serial writes and reads using NI-VISA.

 

David B.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 5
(7,257 Views)

David,

 

The example you provided me uses the exact same NationalInstruments.VisaNS.SerialSession class as mine. The only difference between the example in my code is that the SerialSession.ReadString(int countToRead) method (also inherited from the MessageBasedSession class) is used instead of the Query() method I described. I run into the same problem with this and every read method, each of which throws a timeout VisaException.

 

 

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

Hi,

 

Have you tried using a colon (:) as your termination character? I vaguely remeber using that a one time.

 

Curt

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

I just tried that, but as I expected it didn't work. In the Modbus ASCII protocol, ":" marks the beginning of the message, which is terminated by a carriage return-line feed pair. In the RTU protocol, which the instrument in question uses, the "termination character" is 3.5 character intervals of silence, the exact length of silence being determined by the baud rate.

0 Kudos
Message 5 of 5
(7,216 Views)