Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Extracting numbers from a serially transmitted/received string

I have a problem extracting numbers from a serially transmitted string, I am basically sending 5 floating point numbers together with 3dp of precision at random intervals to the serial port and collecting them at the other end, they are sperated with a carriage return, however I am having trouble working out how to extract the numbers at the other end as the list of numbers keeps updating and because the number length varies ie. from 13.456 to 345.675, I can't just get a number of characters. I am currrently using a while loop with a 100ms delay and reading however many bytes are available at the port. If anyone could suggest a way to get these numbers individually regardless of length that would be bril
liant, alternatively I was trying to format the numbers so that they are always the same length by add 0's as padding as in 6.768 become 006.768 but I couldn't find a way of doing this.

Thanks in anticipation


Em"
0 Kudos
Message 1 of 6
(3,817 Views)
Well, the CR at the end of the numbers makes it reasonably easy. There are a number of methods of doing this.

Split the string at the CR. This will split the sting into everything before the CR and everything after. You could put this in a loop and build an array with all of the numbers that you find.

If you are using LabVIEW, a faster way would be to use the Spreadsheet String to Array.vi from the String functions. Use the CR as the delimiter and the VI will return an array of numbers.

If you want to use the constant length routine - since the numbers are converted to strings anyway - take each number and find the length of the string and then concatenate (length wanted - length of string) 0's onto the front of the number string. I have a LabVIEW VI t
hat pads strings to length already if you want it.

I'm sure that there are other solutions that will do the trick. These are just what I came up with off the top of my head.

Rob
0 Kudos
Message 2 of 6
(3,817 Views)
It would be great if you could let me have a copy of that vi, I tried using the string to array function but it didn't appear - to work or perhaps I just didn't use it properly!
0 Kudos
Message 3 of 6
(3,817 Views)
Reading your answer again I don't think I made myself too clear, there is a CR after each number, and the problem I was having was knowing which number was the first number through to which was the last as the numbers are sent at random intervals.
0 Kudos
Message 4 of 6
(3,817 Views)
Sounds like you may have to mark each number (maybe append an A, B, C, etc.) in order to know which number is which. If the numbers always come in the same order (just at random intervals), then I would suggest appending an EOL (under Serial/End of Line) because the Serial Read VI reads all characters or to the first EOL (whichever comes first). Then each number will be seperate. If there are more characters in the buffer, then you just read again. If you don't get an EOL (you read before all characters were in), then add the next read onto the end of this one.

I have attached my Pad String VI as well. It's not the most elegant (and probably not how I would make it today), but it does work.

Rob
0 Kudos
Message 5 of 6
(3,817 Views)
Thanks very much for all your help -I've finally got it all working ;o)

Cheers
Em
0 Kudos
Message 6 of 6
(3,817 Views)