VISA Read on a Serial Device Does not Return the Requested Number of Bytes

Updated May 13, 2024

Reported In

Software

  • LabVIEW

Driver

  • NI-VISA

Issue Details

I'm trying to receive binary data over my serial port using NI-VISA. For instance, I use the VISA Read VI or the viRead() call to request 100 bytes. However, upon inspecting the received data, I notice that I only receive the first 63 bytes. If I perform another read, I can obtain the remaining 37 bytes. Similarly, I might use the VISA Read VI again and receive 54 bytes in the first read, with the remaining 46 bytes obtained during the second read. What’s causing this behavior?

Solution

When using VISA Read for serial communication, the function returns a number of bytes from the serial port buffer to your program. However, the amount of data returned depends on three important factors related to your settings and the serial data: the termination character, the requested data size, and the timeout period.
 

Serial VISA Read will stop reading when any of the following conditions are true:

  1. The number of requested bytes is equal to or less than the number of bytes waiting to be read on the port.
  2. The termination character has been received.
  3. No more data is available from the serial port, and the timeout period has elapsed without receiving any data.

Additional Information

When examining LabVIEW’s serial communication example (located in NI Example Finder under Hardware Input and Output > Serial > Simple Serial.vi), you’ll notice that it begins with a VISA Configure Serial Port node. This node sets up both the termination character (which defaults to hex 0A, representing “linefeed”) and the timeout (which defaults to 10 seconds).



 

If you’re reading serial data and encounter a byte that matches the configured termination character, the VISA Read VI assumes that this marks the end of the requested data and stops the reading operation. This occurs even if condition 1 (receiving all requested bytes) hasn’t been met.

By default, using the termination character usually doesn’t cause issues in normal text communications. However, binary data doesn’t assign any special meaning to the 0x0A byte. In the example you provided, this is why the first read operation stops after 63 bytes, leaving the remaining 37 bytes in the software buffer waiting to be read. If you’re dealing with binary data transfers over serial, it’s advisable to disable the termination character in the VISA Configure Serial Port VI. To do so, set the input Enable Termination Char to False