LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Find if string contains valid float

I have attached a VI.

A separate MCU board, sends data on computer COM port, in ascii format as below:

 

$-999.999;-999.999?

 

Total length of string is 19 max. It can be less than 19 also, for example number is small like  $9.999;9.999?

VI, continuously scans for 38 bytes of string, Then serach for $ & then take out two numbers in between from them. In VI,number separted are in string Voltage & Current.

 

Now sometimes, I read incorrect string also. So is there anyway to check the Voltage & Current strings, if they contain valid number between -999.999 to 999.999

 

 

0 Kudos
Message 1 of 3
(2,593 Views)

So how does the received string look like when you get an error?

 

To analyze the problem, that's where you need to start. Could it be that sometimes you receive incomplete strings or have frame shifts where the numbers are spread over multiple reads? Why not define a termination character (e.g. ?) for the communication?

How fast does the data arrive at the serial port? Waiting 1s between reads seems like a long time.

 

I assume that you are aware that your "VISA close" can never execute.

 

To identify the problem: As a first step log the received strings into an array, wait until the problem happens, stop the program (where is your stop button???) and then analyze the strings. It should be obvious and is probably one of the above mentioned issues. 

0 Kudos
Message 2 of 3
(2,556 Views)

I have two suggestions.

  1. Most VISA devices sending Ascii Strings send a Termination Character (typically \n, as in \r\n) at the end of the string.  I recommend initializing your VISA with Enable Termination Character set to True, and the appropriate (default = \n) character wired in.  Then do a VISA Read with, say, 1024 characters -- if your device is sending a string of 38 (terminated by \n), the read will stop when the \n is received.
  2. I like to process String data using Scan from String.  In your case, you expect data in the form $%f;%f? (and maybe other things).  If you use this as the format specification, the one Scan from String function will parse your string, return two Dbls that you can call Voltage and Current, and will return with the Error Line "clear" unless the String did not conform to the format.  This one function replaces a half-dozen functions in your code and immediately gives you the Dbl values for your graph.
  3. [OK, so I have three points to make ...]  You should also take a look at the inverse of Scan from String, namely Format Into String, (in my opinion) does a better, clearer job of combining multiple pieces of data into a String for writing, say, to a CSV file than Concatenate.  If you want the TimeStamp as two strings, you'll need to use the functions (as you have) from the Time Palette, but then you could input the Date String, Time String, Voltage (Dbl), and Current (Dbl) using a format of %s,%s,%f,%f.  Finally, if you save all of your strings in a 1D array, you don't need the \r\n separator at the end.  Finally, to get your header "on top", just add it to the front of the array of data strings using Build Array.

Bob Schor

0 Kudos
Message 3 of 3
(2,546 Views)