LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Correct timing/parsing

Solved!
Go to solution

I have an instrument that communicates with my computer via RS232.

The instrument is programmed to send data once every second, in ASCII format.

 

My VI reads this data using "VISA read", and then I have some logic to parse out the data and work from there.

The entire process is in a while loop with a 1000ms (1 sec) wait.

 

99% of the time, this works fine, however every once in a while, the data is parsed out incorrectly.

I believe it has something to do with a mistiming between my VI and the hardware.


Does anyone know a better way to ensure they are synchronized? 

Message Edited by Cory K on 03-27-2009 03:49 PM
Cory K
0 Kudos
Message 1 of 14
(3,656 Views)

I had thought of one thing, but I have NO IDEA how to implement it.


I wanted to create a buffer / queue / whatever you want to call it.

This buffer will collect all data sent to the port and hold it.
I will then dequeue X bytes of data every 1 second.

This isnt really communicating in real-time, but it would be simulating real time more or less.

Would that be an option?

Cory K
0 Kudos
Message 2 of 14
(3,653 Views)

Cory,

 

1. Does the data sent from the instrument have any termination character or formatting, such as a fixed or maximum number of characters, which would allow you to precisely identify the end of a data set?

 

2. How about reading all the characters present every 100 ms and appending them to a single string to be parsed later?  When you get a 100 ms period which received no characters, the message is complete and ready to be passed to the parser.  This only works it the longest message has a duration of less than 900 ms.

 

Buffering in an Action Engine or queue is probably a good idea.

 

Lynn 

Message 3 of 14
(3,646 Views)

I'm not sure if there is a termination character signifying the end of the transfer,

however there are termination characters for end of line (/n).

 

Hmm, now that I think about it, I could collect data for X number of line terminations,

for example:

data /n

data /n

data /n

data /n

 

So every 4th /n I would collect then parse that data.

 

If data is sent to the port by the instrument, will it wait until it is read by the computer?

Or will it clear as soon as the next set of data is sent?

 

Also, if I decrease the wait to 100ms, what happens if I do a VISA read, and there is nothing at the port?
Does that throw an error?

Cory K
0 Kudos
Message 4 of 14
(3,640 Views)

"So every 4th /n I would collect then parse that data."

If the data are always 4 lines , you can do that. It is a better way than the one you use now.

 

"will it wait until it is read by the computer?

Or will it clear as soon as the next set of data is sent?"

They will not be cleared. They are stored in the VISA buffer. The default size of that buffer is a few thousand bytes. I assume that your data are much less, so you will have no problem

 

"Does that throw an error?"

It will throw an error if a timeout occurs. VISA will wait and read data as long as

1) a termination char is not present (if it is enabled)

2) there are less bytes than those you wired

3) the timeout is not expired

Message 5 of 14
(3,626 Views)

As Pnt said, the VISA Read will generate a timeout error if no data is present.  You could use that timeout error as the indication that the transmission is complete.  You have not specified how long it takes for the instrument to send one set of data or how long it pauses between data sets, only that it starts a new set each second.  If the pause between data sets is much longer than the pause between characters, then you can use the timeout method to detect the end of the data set.

 

You can also use the Bytes at Port property to read just the data which is currently available.

 

If the instrument's behavior is not well documented, you may need to run some experiments. 

 

Lynn 

0 Kudos
Message 6 of 14
(3,591 Views)

johnsold wrote:

As Pnt said, the VISA Read will generate a timeout error if no data is present.  You could use that timeout error as the indication that the transmission is complete.  You have not specified how long it takes for the instrument to send one set of data or how long it pauses between data sets, only that it starts a new set each second.  If the pause between data sets is much longer than the pause between characters, then you can use the timeout method to detect the end of the data set.

 

You can also use the Bytes at Port property to read just the data which is currently available.

 

If the instrument's behavior is not well documented, you may need to run some experiments. 

 

Lynn 


I'm not too familiar with how the instrument was programmed. The instrument came with its own software, which I didnt like, so I reverse engineered the program, then added more stuff that I wanted it to do.

 

All I know for sure about the instrument is that it sends data every 1 second, and there is a constant format the data is sent, which after some testing, figured out what that format was.

 

I use the 'bytes at port' function, however if the timing is off a little, the data gets parsed incorrectly.

For example, I may get a reading for Temperature of "3/19/09" and a date of "59 deg", which is obviously not correct.

Cory K
0 Kudos
Message 7 of 14
(3,576 Views)

Do you know the maximum number of lines or bytes which the instrument will send in a 1-second data set?

 

What is the communications speed (baud)?

 

Lynn 

0 Kudos
Message 8 of 14
(3,570 Views)

Heres how I have the port configured right now:
Baud - 9600

Bits - 8

Parity - None

Stop Bits - 2

Flow Control - None

 

The data comes in 21 lines, each of which end in /n

There is no set length for each line, since the number of digits, etc may change

Cory K
0 Kudos
Message 9 of 14
(3,555 Views)

Thanks for your help everyone. I think I figured it out.

When I do the port configuration, I set the termination character to 'True'.

Then when I did a serial read (within a for loop) I had an array to work with,

so I could index whatever lines I wanted.

It doesnt seem to be giving me any trouble yet, but I'll keep my fingers crossed

Message Edited by Cory K on 03-31-2009 11:49 AM
Cory K
0 Kudos
Message 10 of 14
(3,542 Views)