Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

reading serial port

Hello ya'll  I'm new to Labview but I'm learning everyday
I'm trying to read a string from a com port. In the following format
11:23:44 7600 7700 45.3 44.7 .50 1.02 2.05 3.05
the string sometimes well have more data in it.  I only care about the first 5 after the timestamp.  The string uses space delimiters and a LF + CR at the end of the string.  The first two pieces of data can range from -3700 to 1500 and all the other can range from 0 to 100.  I'm just pulling the first 5 to display on the screen.  In this string I'm just showing 8 pieces of data but there could be up to 15 or so.  Also if there is no data comeing in I really don't care. I think I have that part worked out with a byte count (no bytes - display NaN)  I have that writen in a case structure.  But when I have data comeing it stops my program with "The input string does not contain data in the expected format"  I'm using "scan for string" to try to pull off the first 5 pieces of data from the string.  How can I do this defferntly to achieve what I want to do?
 
0 Kudos
Message 1 of 11
(4,404 Views)

What are you using for the format string? Do you ever get less than 5 data items after the time stamp?

Instead of the Scan From String, you could also use the Spreadsheet String to Array function. The delimiter is a space, format of %f, and data type is a 1D DBL array. If you need the timestamp, you would convert to a string array and after using the Index Array function, get the numerics and convert those separately.



Message Edited by Dennis Knutson on 05-21-2008 12:06 PM
0 Kudos
Message 2 of 11
(4,402 Views)
No never less then 5.  The format I'm using is "%s %.2f %.2f %.2f %.2f %.2f "  I don't need the time stamp.
I will give this a try.  Now to learn more about arrays and pulling the data out of them
0 Kudos
Message 3 of 11
(4,392 Views)
I don't get an error with that format string. The only way I get an error is if there are less than 6 fields to parse.
0 Kudos
Message 4 of 11
(4,387 Views)
It could be the way I'm trying to read the string.  I'm not sure how to read the 2 termination characters so it know when the string ends.  I do notice my byte count increasing even though my string does not change.  I tried using the example you showed but mine always comes out as a 2D.  When I change when I change the array type input to 0 like you showed   my box turns blues and the string to array says it a 2D.
I tried to post a pic of what I'm working with but couldn't figure out how to do it
0 Kudos
Message 5 of 11
(4,374 Views)
Posting the VI would be better but to do either, just click the browse button next to the 'Attachment' box below the message body.
0 Kudos
Message 6 of 11
(4,366 Views)
Ok i will give that a try
0 Kudos
Message 7 of 11
(4,363 Views)
What is a bit confusing is your local variable called 'Stopping write'. Do you have some other code that does a write to the serial port? How do you stop this VI? A separate VI that does the writes could be a problem. One thing you could do is remove the Bytes at Port function. You have the VISA Configure Serial Port set with the termination character enabled. What that means is that as soon as the term character (default of a LF) is detected, the VISA Read will stop. You can assure that you always get a complete response. For example, if the instrument could never send more than 250 bytes, you could specify a byte count of 300 and the VISA Read will terminate on the LF and not the byte count. With the way you have it, you have the possibility to read partial results.
0 Kudos
Message 8 of 11
(4,335 Views)

Yes there is another part of the code that writes, but it does it on com 1 and I read the data in from com 2.  The byte count part is for when not data is coming in (not hooked up) and a NaN displays in the proper places.  Thats the part of my case structure to either send out NaN to my displays or if there is a byte count to send to data off the string down the line to the "scan from string".  I'm not useing the byte count to terminate or at least I don't want to.  I want it to aways read if no data or data. If data is comeing in it comes over at once a second.  On the term char can you have the LF + CR?  I know on are old software writen years ago by someone who knew what they were doing had that term char to pick from.  I'm trying to write a scaled down version that doesn't give the user that option.

so would adding term enable and term char below byte count work?  right now I think its useing LF as default because when I look and the data when it is running the first time it is at the top of the probe and from there on out there is a blank line as if someone hit the CR.  and would I end it on just the CR or LF + CR?

The program runs for awhile then stops with the error "input string does not contain data in the expected format"  if you hit continue it may or maynot run for awhile longer.

0 Kudos
Message 9 of 11
(4,327 Views)
Yes, you are using the byte count to terminate the read. By definition, the VISA read will terminate when the number of bytes specified is read or when the termination character is detected. Since your write is happening asynchronously, what could be happening  is the the read is detecting when a small number of bytes is available and not the complete write string. This will mean the VISA read will return a partial string and this will cause the Scan From String to report a failure. If you want to eiher read or report the NaN, then I think what will work is placing a high byte count constant in the case with the VISA Read and not wiring up the results from the Bytes at Port function. What this should accomplish is that as soon as there are bytes available, the read will start and then not stop until you get a complete string.
0 Kudos
Message 10 of 11
(4,323 Views)