Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with Serial VISA Communications LV 6.1

Hello,

I'm having the following problem: I am trying to communicate via serial connection to a device using VISA in LabView 6.1. I have explicitly set the following using a property node:

baud = 9600
data bits = 8
flow control, parity, ASRL End In = 0
stop bits = 10
timeout = 5000

I write commands to the instrument, and then try to read them back. It wasn't reading at all till I set the ASRL End In = 0, a fix which I found here on the message boards. Now, it reads everything, seemingly correctly, but it still returns a timeout error (-1073807339), regardless of the timeout value I use (and I've tried a lot of them).

Ordinarily, I wouldn't care that there's an error since, other than that, it does re
ad the data, except that when I do two commands in a row, if the buffer I allocate in the first VISA read is big enough for both the first command's response and second command's response, it seems to put them both in there, leaving the second VISA read empty, which is a problem. This would seem to indicate to me that the timeout error is valid, and the first read hasn't finished yet, so it's still waiting for data. That's my guess at least, but I don't know what to do about it 😞

Does anyone have any ideas?

Thanks very much!

Steve
0 Kudos
Message 1 of 4
(3,621 Views)
Steve:

Almost all the behavior you describe is as expected.

When you do a VISA Read it can complete successfully for 1 of several conditions. In Serial it is because either 1) it reads the number of bytes you asked for, or 2) termchar is enabled (which it is by default) and the specified termination character (\n by default) is encountered in the input data stream. If neither of these conditions is met, it will continue to read until it times out (2 seconds by default).

The way serial works everywhere is that all data that comes in on the port (at whatever time the instrument sends it) is all put into 1 big buffer by the OS until you read it out. VISA will read up to the amount you ask for, or up to a given termination character.

The defau
lt in VISA is to have a VISA Read end its input when it sees a termination character because most serial devices will end their response with a \n or \r\n (which still ends in \n). Therefore you can read back 1 response at a time. If this is not the case with your instrument, then you need to do at least one of the following:
1) know how many bytes your instrument will send back and call VISA Read with exactly that many bytes
2) wait for a short while, until you know the instrument has sent the data back, query the number of bytes available at the port, and then use that number to pass to VISA Read
3) set your timeout low enough so you don't wait too long, do a VISA Read requesting more bytes than the instrument will send back, and expect a timeout

The only part about your post that confuses me is that you said it wouldn't read at all until you set the end mode to 0. Is that because your instrument sends back \n within a single response?

Hope this helps,
Dan Mondrik
Senio
r Software Engineer, NI-VISA
National Instruments
Message 2 of 4
(3,621 Views)
Dan,

Thanks for your reply. I don't know what setting ASRL End In to 0 really does. The response from my instrument does contain a few \n's in a single response as you mentioned.

I had a look at the buffer, and there are three \n's, and one CRLF, but the thing that really marks the end of the response is a period. So as you suggested, I set the termination character to this.

Additionally, since message lengths aren't always known to me, what I did is I have it wait in a loop until some minimum amount of bytes are available in the buffer, then I delay 200ms, then I read the number of bytes in the buffer (assuming that it's all of them), and then read exactly that amount of bytes.

And it does appear to be working, so thank you very much!

S
teve
0 Kudos
Message 3 of 4
(3,621 Views)
Steve:

I'm glad you've got a working solution. But if *every* response ends in a period, then it's even easier.

Leave ASRL End In to its default value (termchar). Set the termchar value to a period (as you are doing). Then there is no need to wait. Just set the timeout to be higher than any amount of time you would expect, and just call VISA Read with a higher count than you would expect.

It will read however much data arrives until the termchar you specified. That way you don't have to worry about the number of bytes it actually sends.

Dan Mondrik
0 Kudos
Message 4 of 4
(3,621 Views)