LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial, read until char == X

Hi,

I'm currently using the serial.vi to read characters from the serial port until the timeout occurs or I've read the required number of bytes. However, I'd like to change this so that it reads until a particular character is received, say a 'p'. Can anybody help me do this please?

Thanks,

Martin
0 Kudos
Message 1 of 9
(4,158 Views)
You can change the termination character with a Property Node of VISA. Go to Instrument I/O>>VISA>>VISA Advanced and select Property Node. Change the node to Write and select the property Message base settings>>Termination Char. Wire the numeric value of the character to the input.

This value is normally set to the character or 10.

Message Edited by waldemar.hersacher on 05-24-2005 01:29 PM

Waldemar

Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
Don't forget to give Kudos to good answers and/or questions
0 Kudos
Message 2 of 9
(4,158 Views)
Thanks,
I'm trying that aw we speak. The only thing is, my message will be like this:
pppp0C,01,02,03pppp
I don't want to terminate on the initial p's just the final ones. Any thoughts please?
Cheers,
Martin
0 Kudos
Message 3 of 9
(4,152 Views)
Trying to terminate on a character which can occur several times in your message will be difficult. The software needs some way of knowing WHEN the 'p' to terminate the read has arrived. If you read 'pppp', should you terminate, or wait to see if the full string is 'ppppppp' but part of it hasn't been read yet?

The standard way of doing this is to use a unique character (or string) which will ONLY appear at the end of a message as a termination character. What you want won't work this way.

If your message isn't fixed-length ot fixed-format it'll be very difficult to find a way of really deciding when the message is fully received.

Do you have any constant specifications for the messages you'll be receiving? This is kind of the key to being able to solve this problem.

Shane.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 4 of 9
(4,151 Views)
Thanks Shane,
All messages I receive are of a set format. 'p' denotes the message decoding hardware is in an idle state. So a message is always in the format:
ppp~s0C,01,02,03ppp

0C is the length of the message to follow, so in this case I received fewer chars than I was expecting. Therefore, I want to stop monitoring once I get a 'p' after the start of the message as either 1)the message is complete or 2)the message has failed.
Messages always start with a '~s'.

The condition I want to check for is this: If I have started receiving a message (i.e. had '~s') and I get a 'p', stop monitoring the port. The problem I have is that as far I can see, all I can do is set the timeout and the number of bytes that I'm looking for. This does not work because 'p' characters are only transmitted infrequently, so I can't just wait until I have say 50 bytes before checking how many of them were 'p'. I need to be able to look at the received byte array as it is formed so that I can control serial reception.

Thanks again for any thoughts, it's something I did in Matlab but I'm struggling to get my head around LabView.

Regards,

Martin
0 Kudos
Message 5 of 9
(4,145 Views)
I think you have three options but maybe someone else has some others.

1) You can read in lots of data and parse it once you get it.
2) You can read in one character at a time and assemble the characters as you recieve them.
3) You can use the unique (atleast from this very small example) character of ~ as your stop bit and rearrange it all later.

I think that your best bet would be #2 if you can do it fast enough.

Hope that this helps,
Bob
0 Kudos
Message 6 of 9
(4,136 Views)
Aha,

There is a way to do this, but it requires a two-tier approach to reading the data from the serial port.

Basically you need a while loop (running slowly if you wish) which reads a single byte at a time (make sure not to set the timeout to infinity!). If the character is NOT 'p', then quit the loop and send the acquired character to the next loop which then reads in byte for byte (and appends the characters to the original one from the first loop) until the character IS equal to 'p'.

This way your second loop should be able to give back the response from the instrument WITHOUT any 'p's. If, however you need to be able to receive two messages WITHOUT any 'p' between them, this method fails also and reading in lots of data and processing them later is your only choice really.....

hope this helps

Shane.

PS here's a (non-executable) example.
Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 7 of 9
(4,128 Views)
>bump< 😛

I'm sorry, I'm old and have just worked out why people always write "bump" in forums.

Ah, the joys of no longer being savvy.

The real reason for the post:

Any luck in solving the problem? The thread kind of died a quiet death.....

Shane.

Edit: My >bump< wasn't shown - brackets the wrong way round...... False HTML.....

Message Edited by shoneill on 05-27-2005 03:02 PM

Using LV 6.1 and 8.2.1 on W2k (SP4) and WXP (SP2)
0 Kudos
Message 8 of 9
(4,105 Views)
Hi Shane,
Apologies for the delay in replying, I was called away from the office unexpectedly. Thanks very much for your help & the .vi file. I'm just having a go at implementing the idea this afternoon & will let you/the forum know how I get on.
Thanks again,
Martin
0 Kudos
Message 9 of 9
(4,097 Views)