LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read serial port only when new data has been sent to it

I've written this very simple vi to read the serial port whenever the MKS Helium Detector sends a new value.  The timing of new data is controlled directly from the front panel of the MKS instrument.  I thought it was as simple as looking at the "bytes at port", and reading the port only if the value is not zero.  The problem I'm having is that the read function is occuring whether I want it to or not.  Consequently, the "Scan from String" function is returning an error (0) once in a while, because it's reading the serial port as it's being updated (I think).   Apparently, when the port is in the process of being updated, it is not zero.   Anyway, I'm assuming there is a simple fix, but I'm having no luck finding the Easter egg.  Any help would be, as always, very much appreciated
0 Kudos
Message 1 of 9
(5,463 Views)
Hi,
 
The are several ways to solve this depending on the data comming in.
 
- After you detect data at the com port you can wait till you are sure all data is send
 
- If the length is known you can wait for that number of bytes
 
- You can wait for a termination character(s) if there is any
 
- You can read all characters comming and pass the data trough if you don't receive more characters after a time-out
 
- and there are many more solutions possible
 
If this doesn't help tell us what the data looks like you're receiving.
Message 2 of 9
(5,426 Views)
Ok KC, here's what I know so far.  A typical output from the VISA READ function looks like the following: 4.5e-6\smbar-l/s\s\s\r\n.  I believe the \r is a carriage return, while the \n is a line feed.  So I know the last character is a line feed.  I'm not sure where to go from there.  I'm using the SCAN FROM STRING to parse out the string and convert the numeric part of the string to a Double. The numeric value is all I care about.  It seems as if I should be looking for a character to signal that new data is ready to be read, rather than looking for a character to indicate that all the data is in.  My intuitive approach is that I should be looking at the serial port for some indication that "Hey, there's new data here, read it".  I'm still learning a lot about serial communication, so any help you can provide would be wonderful.  My sense is that this is not at all a complex issue, but anything new can be difficult.  Please feel free to bail me out here.   
0 Kudos
Message 3 of 9
(5,415 Views)
KC, can I e-mail you directly?
0 Kudos
Message 4 of 9
(5,409 Views)
Hi,
 
Yes you can mail directly but then you will only get my answers and not from other (real)   Smiley Wink  Enthusiastic veterans
 
It is my home mail address, so I will only answer you in my evenings. Now it's 15:28h  (mail: keeswe@zonnet.nl)
0 Kudos
Message 5 of 9
(5,391 Views)
KC, anything you can think of right away, before you head home?  I'm in a bind trying to reslove this issue today.  I know I want to look for a carriage return, but what do I do: use two VISA reads, read the port to look for a carriage return, then if I see one, read the data again to get the value?  I'm lost...
0 Kudos
Message 6 of 9
(5,388 Views)
How is this. Didn't have time to test it. The VI wait for <cr><lf> and exit with the value. You also add a time-out if you want
0 Kudos
Message 7 of 9
(5,380 Views)

You can approach your problem in a couple of ways. The way you've got it written now, as soon as the number of bytes is non-zero, you read and convert. This means that when a partial string is there, you conversion is nor correct. One way is to wait until non-zero bytes like you do now, and then in another loop, read until the byte count is equal to zero and then do the conversion. You can also read until the cr\lf is detected and then do the conversion on the string. You can also enable the termination character for the read but then you'll have to increase the number of bytes to read to something other than just the number of bytes first available. If the instrument always returns the same number of bytes, you can use that as a constant for the VISA Read. Then the read will terminate whenever the byte count OR the termination character is detected.

I modified your VI to show you how you might put it in a loop and what for the CR/LF to be detected. You would want to add a timeout to this so that if the character does not show up, the loop will not run infinitely

Message 8 of 9
(5,372 Views)
I haven't played with this much, but have you looked at using the VISA Wait On Event VI ( found on the VISA Advanced|Events sub-pallette)?  You can set this VI to wait until VISA tells you that it has received the termination character.  By setting the termination character to \n (the last character in your string) and waiting until you receive the Serial TermChar event you can be sure that you have a complete response from your instrument.  Then you can do the normal Visa read passing bytes at port to the byte count parameter of VISA Read.  VISA Read will only pull in the bytes up to the termination character.
----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
0 Kudos
Message 9 of 9
(5,361 Views)