LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a limit to the number of bytes that a VISA read can perform?

I am working with some serial devices, reading from them and such, I have previously discoverd that when you use the simple serial read there was a limit to the number of characters that the read vi would perform(2K bytes), but is the same true with the visa read, or will it read all of the data that is there regardless of size?
0 Kudos
Message 1 of 4
(5,440 Views)
I found that the best way to read from the serial port with either the old serial or VISA is read the bytes as they become available in the serial port buffer and not try to read a large chunk all at once. Use the Bytes at Serial Port or VISA Bytes at Serial Port functions and use the output to determine how many bytes to read. Keep reading and appending the output until there are zero bytes to be read or until the termination character is read.
0 Kudos
Message 2 of 4
(5,440 Views)
This is exactly the way that I have been reading from the serial port, I was just wondering if there was a better way to be doing it. Thanks.
0 Kudos
Message 4 of 4
(5,440 Views)
Marshall:

Technically speaking, viRead takes a ViUInt32, which means the max size should be 0xFFFFFFFF. In reality, many lower level drivers (and also the COM API, that's Microsoft COM not Serial COM) accept only a ViInt32, which means that the max is 0x7FFFFFFF.

But you'll never get anything close to either of these numbers over Serial. Why? Because the OS maintains a locked kernel buffer for each port to hold the data as it comes in off the UART. In VISA you set the size of this buffer with viSetBuf (in LabVIEW it's VISA Set Buffer Size). The default size is a smaller buffer than most apps need, but you can usually set it as high as 32KB-1 without a problem. (This is the max size on Mac OS 8/9, for example.) Windows does not specify a maximum
size but I've seen requests for buffers greater than 32KB fail.

The best way is to always preset the buffer size to a little more than the max you ever expect to read. Then, to read data, do as Dennis suggested and always query the number of bytes available, or just do a read operation with the maximum number of bytes you expect. If you're reading ASCII data, you can make the read request be a larger size than you need and VISA will automatically stop reading (by default) when it gets to the specified termchar (\n by default).

Good luck,
Dan Mondrik
Senior Software Engineer, NI-VISA
National Instruments
Message 3 of 4
(5,440 Views)