Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing a string to determin the end

I have an instrument that responds with a 73 byte or longer hex string. Starting with 02 (STX), ending with 03 (ETX). If the values 02, 03 and 10 occure in the dataframe they are preseded by a 10 (DLE). Theoretically the string can then be from 73 until 141 byte long. So I have to detect the last 03 to determin the end of transmission so I can start the real parsing.
 
The whole string syntax looks like this:
1 start byte + 2 controll bytes + 68 byte data-frame + 1 byte checksum + 1 stop byte
 
HEX: 02 0D 08 [data frame] [check] 03
 
Quasicode will look like this (??):
Scan through all bytes as they arrive and write to array/string
If byte = 10 and next byte is 02, 03 or 10, extract 10 from array/string and keep on scanning
If byte = 03 and predecessor is not 10, end of transmission.
 
How can I accomplish this in an elegant way in LV?
 
I am still rusty ....
 
Regards
 
RunnerBoy
0 Kudos
Message 1 of 4
(3,320 Views)

The "elegant" LabVIEW solution is a state machine reading each byte in succession and parsing as required.

Depending on your hardware (you didn't say whether it is GPIB or serial, but I'm guessing serial) you might be able to configure it to detect an "end of message" character or condition.  This is easily done with GPIB, not always possible with serial devices.  If the hardware can be configured to terminate the read when the string is complete then finding the ETX is simple.

For some instruments a character count value is provided in the control bytes or at the beginning of the data frame which simplifies the parsing.  Otherwise each character must be tested as they occur.

Rich

Message 2 of 4
(3,295 Views)
RunnerBoy,

assuming you have a response from your instrument (it is OK to read whats there and append it to what was there before), I'd match the appended string with the regular expression '~[\10]\03' (remove trhe framing ''s), using the Match Pattern node. This regular expression should be found whenever a 0x03 was found that was not preceeded by a 0x10. Repeat the reading, appending and matching until Match Pattern actually matches. Use the Before Substring to process your data. Use the After Substring as the statrt string for the next message.:
* Create a while loop.
* Create a shift register; initialise it (from left border) with an empty string.
* Read whatever bytes are there, append 'em to the content of the shift register
* match the appended string
* feed the After Substring to the right shift connector of the register
* Compare Offset Past Match to be >=0, use the comaprision result to finish the While loop.
* Put a fitting wait node in your while loop if you do not use events to read your response data!

I'd replace any preseding 0x10 afterwards in the found device response.

HTH   and
Greetings from Germany!<br>-- <br>Uwe
Message 3 of 4
(3,283 Views)

Thank you both RLD and LuL.

Serial port: YES, HW handshake: NO

But RLD has a point:
"For some instruments a character count value is provided in the control bytes or at the beginning of the data frame which simplifies the parsing.  Otherwise each character must be tested as they occur"

By determined reading the manual I found that my "datastring" actually consist of 3 control bytes (no \10's) and a DATA FRAME LENGTH byte. But the lenght dont include the \10's. So my problem stil persist but in a different form:

Now I only have to find the count value, count all bytes except the \10's, the double tens count as one. And put valid databytes in a string or queue.

What will the elegant LV way look like ?

Thanks

RunnerBoy

0 Kudos
Message 4 of 4
(3,273 Views)