From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Regular expression for middle of string

Solved!
Go to solution

Hello again,

 

I've been having computer problems and couldn't work on the project until now.

 

I appologize for my mistake in explaining what I needed. The thing is, I don't know how or what will be coming through the serial port, which is why I want to use delimiters. This means I could get partial messages.

My delimiters are: TEXTSTART, TEXTSTOP, DATASTART, DATASTOP.

 

 

 

Example:

"TEXTSTARTProgram Ready

Threshold: 127

Prescal"

 

(and in a diffrent message, the rest)

"er: 64

Baudrate: 115200TEXTSTOP"

 

Also, the messages don't always start and end with a delimiter. I could have two halves of different messages:

"9948573493DATASTOPTEXTSTARTThreshold: 127

Prescaler: 64

Baudr"

 

Which means I want to keep whatever isn't between it's two delimiters in a buffer string, and keep adding to it until the delimiters are found. At which point extract the string, process it according to it's type (which I'll know from it's delimiters) and delete it from the buffer.

 

Well, I hope I haven't missed anything this time, and that there's someone out there patient enough and can help.

 

My best regards.

0 Kudos
Message 11 of 19
(1,638 Views)

Hello Surreal,

 

The behavior of what you have is well defined.  You have many solutions to extract the string between delimiters, what is your question exactly, where are your difficulties?

 

Michel

0 Kudos
Message 12 of 19
(1,632 Views)

None of the proposed solutions account for partial strings.

 

Basically what I want is a way to extract what's between 2 specific delimiters and pass the rest of the string. If there aren't both delimiters present just pass the string.

0 Kudos
Message 13 of 19
(1,621 Views)

Just concatenate the received string in a shift register until you have start and end string. Once you have, parse the content and delete the parsed part from the string. repeat.

0 Kudos
Message 14 of 19
(1,612 Views)

Yes, but the problem is the parsing. 

The Scan string for tokens function which you used doesn't work if I have something outside ot the tokens, as you said.

And since I can have partial strings (just one or even no tokens) it won't work.

0 Kudos
Message 15 of 19
(1,604 Views)

This seems like it should work, but it give a "LabVIEW:  The maximum recursion limit was reached while attempting the regular expression match" error.

0 Kudos
Message 16 of 19
(1,590 Views)

Mike mentioned already earlier in this thread that it is better to use shorter text, respectively data delimiters. I would use just one character, for example:

  • 0x02 (STX, start of text) for instead of TEXTSTART
  • 0x03 (ETX, end of text) instead of TEXTSTOP)
  • something similar for the data start/stop, maybe 0x0E (SO, shift out) and 0x0F (SI, shift in)

Refer to an ASCII table to see all unprintable characters.

 

Then you could use something like that to parse the input (the input string is in code display mode):

parse_serial.png

 

The other two frames of the case structure are shown below

case_parse.png

 

This is just a simplified example of how it can work.

  • the program reads from the serial port and analyses each character
  • STX starts a new string
  • any other character adds to a temporary string butETX sends the string to a queue, for example.

Or you can read just from time to time and send the input to a parse subVI, etc....  Same with the DATA START/STOP

 

If you cannot use single characters as text delimiters, the the logic will be more complicated, but it could work similar to the example above.

 

Message 17 of 19
(1,571 Views)

Well, since my DATA is sent as bytes (0-255) could it not be interpreted as one of my delimiters?

This is why I used a more complex delimiter, so that the chances of having a succession of chars that match one of my delimiters are lower.

The DATA will be the value of a voltage, and on a ramp or sine wave there's a pretty high chance of hitting a single char delimiter.

0 Kudos
Message 18 of 19
(1,562 Views)

In this case it is maybe better to designate one or two bytes right after the STX for the length of the message. Then you know exactly how many characters/bytes to expect. No need to parse. I would add also checksum at the end of the message.

 

0 Kudos
Message 19 of 19
(1,554 Views)