LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VISA serial Issues

I am currently involved in a project where we are streaming data to serial
port from an instrument and also using labview VISA serial. I have a few
questions that maybe you can help with.

1) Currently, the instrument is writing a fixed number of bytes (10264) to
serial port on the computer. I set up a VISA serial read buffer of this size
and have this number also wired into the VISA serial read as the number of
bytes to read. I also have set up a property node (in addition to a property
node set up at initialization) to obtain the number of bytes at the serial
port. (see attached) I want this number because if it falls to 0, I want
to start a timer that will help me establish a timeout condition (egs. after
10 seconds, if still 0, ask the user if he wants to stop or reset the program).
this node and the VISA serial read are inside of a while loop. When I run
the VI, the number of bytes at the serial port always seems to equal 0 (maybe
i need to put a break on to see if in fact the operation is so fast that
I just cannot see other numbers?), even before the read. This seems strange
to me because I would expect that there would be a number of bytes accumulating
at the serial port until reaching 10264 and at that point being read and
the number then going back down to 0. When I look at the number of bytes
recieved (terminal off the VISA serial read), it always equals 10264 which
makes sense because my understanding is the read will only be performed when
the buffer is filled with 10264 bytes. So I am somewhat confused as to how
to perform the timeout test. Our timeout test needs to act based on whether
the instrument just stops sending bytes.

2) Should I be setting up a transmit buffer for the same port on the computer
that is recieving the transmission? I am just using this port to recieve
not transmit. Should one also set up a transmit buffer so that the instrument
sending the information to the port will actually put these in a buffer and
not send till reaching a certain size? (Our port is only recieving not transmitting.)

3) In the actual experiment, we will also be recieving data at a second port
at a different speed. In the simulation that I set up, I could only get everything
to work by setting up two separate while loops, one for each serial read
session. I could not have things set up in the same loop, either with two
independently strung together (thru error in/out and refnum in/out) sessions,
or by forcing sequential operation of one read before the other (by attaching
the two independed reads together thru error in/out). Have anyone run into
this situation before?

Sincerely,

Don
0 Kudos
Message 1 of 3
(3,156 Views)
don roth wrote:
>
> I am currently involved in a project where we are streaming data to serial
> port from an instrument and also using labview VISA serial. I have a few
> questions that maybe you can help with.
>
> 1) Currently, the instrument is writing a fixed number of bytes (10264) to
> serial port on the computer. I set up a VISA serial read buffer of this size
> and have this number also wired into the VISA serial read as the number of
> bytes to read. I also have set up a property node (in addition to a property
> node set up at initialization) to obtain the number of bytes at the serial
> port. (see attached) I want this number because if it falls to 0, I want
> to start a timer that will help me establish a timeout condition (egs. after
> 10 seconds, if still 0, ask the user if he wants to stop or reset the program).
> this node and the VISA serial read are inside of a while loop. When I run
> the VI, the number of bytes at the serial port always seems to equal 0 (maybe
> i need to put a break on to see if in fact the operation is so fast that
> I just cannot see other numbers?), even before the read. This seems strange
> to me because I would expect that there would be a number of bytes accumulating
> at the serial port until reaching 10264 and at that point being read and
> the number then going back down to 0. When I look at the number of bytes
> recieved (terminal off the VISA serial read), it always equals 10264 which
> makes sense because my understanding is the read will only be performed when
> the buffer is filled with 10264 bytes. So I am somewhat confused as to how
> to perform the timeout test. Our timeout test needs to act based on whether
> the instrument just stops sending bytes.
>
Don,
I'm guessing that you call the Bytes at Port property node, then the
VISA Read. The Bytes at Port will respond with 0 (assuming your loop
has caught up with any previous data), then the Read will hang in there
until all the requested bytes are available. What you need to do is
build your time-out loop based on the Bytes at Port, and put the Visa
Read inside a case structure, so that it is only activated when the
proper number of bytes are available. (Also add a small delay in the
loop to keep it from chewing up CPU time. Even just a ms helps.)
Also, I would suggest setting the buffer size to at least twice your
expected data size. There is no real reason to make the buffer match
the data size. The whole purpose of a buffer is to hold data until you
get a chance to read it.

> 2) Should I be setting up a transmit buffer for the same port on the computer
> that is recieving the transmission? I am just using this port to recieve
> not transmit. Should one also set up a transmit buffer so that the instrument
> sending the information to the port will actually put these in a buffer and
> not send till reaching a certain size? (Our port is only recieving not transmitting.)
>
I don't think you need to set up a transmit buffer in this case.
(Also, if you don't define the buffer, I think it defaults to 1024
anyway.)

> 3) In the actual experiment, we will also be recieving data at a second port
> at a different speed. In the simulation that I set up, I could only get everything
> to work by setting up two separate while loops, one for each serial read
> session. I could not have things set up in the same loop, either with two
> independently strung together (thru error in/out and refnum in/out) sessions,
> or by forcing sequential operation of one read before the other (by attaching
> the two independed reads together thru error in/out). Have anyone run into
> this situation before?
>
> Sincerely,
>
> Don

If you really need to sample the two ports at different rates, the
two loops are the obvious solution. One way to put them in the same
loop would be to restructure your serial reading routines: On each
iteration of the loop, find out how many Bytes at Port are available,
then just read that many. Save this string on a shift register,
appended to any previously read data already on the shift register.
Then you need to add some logic to handle the case where you finally get
a full data set on the shift register. You could do this on two
parallel lines of Visa operations in the same loop with two shift
registers. You would probably have to put the complete data segments in
a (heaven forbid) global or local or perhaps a queue so that another
part of your program could access them while the loop continues on.
Let me know if you have any other questions.

Regards,
Dave Thomson

-------------------------------------------------------------
David Thomson 303-499-1973 (voice and fax)
Original Code Consulting dthomson@originalcode.com
www.originalcode.com
National Instruments Alliance Program Member
-------------------------------------------------------------
Research Scientist 303-497-3470 (voice)
NOAA Aeronomy Laboratory 303-497-5373 (fax)
Boulder, Colorado dthomson@al.noaa.gov
-------------------------------------------------------------
0 Kudos
Message 2 of 3
(3,156 Views)
Getting bytes from the Serial port can be tricky. Based on a few things.
1. How complex is the protocol of the communications.
2. The data word size in bytes.
3. Does the instrument send "unsolicited" data to you.

I've worked on serial projects where there is quite a bit to handshaking between the serial devices coupled with 'unsolicited' data comming from the instrument that required not only to read the string - but in fact force the code to interact with the instrument until the interaction was complete.

For simpler instruments, you can simply ignore them until you need to request data, then check for errors process them and command the instrument.


For more complex Interactions.
I would suggest using 2 Queues in one COM Loop: 1. for sending data one for receivi
ng data. The "Send Command" code would read the input Queue and send data if there are items in the Queue. When NOT sending data the Loop would 'listen' to the port and load any incomming data into the outgoing Queue.

This works very , very well and makes subroutine function code simple Queue read and writes.

It's hard to say until you actually start talking to the instrument. I've had 4800 baud instrument COM code be very, very, complex compared to 19200 baud command sets! It really depends in how intense the communication is.
0 Kudos
Message 3 of 3
(3,156 Views)