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: 

Serial Read Mistiming

I'm having some trouble with a VI I wrote.

It communicates with a device via Serial Port.

 

The device automatically sends the recorded data once per second.
I wrote some code to obtain, then parse out that data.

I then do different things with the different parts.... but that part isnt important.

 


Sometimes the data parses out correctly, and sometimes, it is in the wrong order.
I tried to put a check in the code to make sure it is in the correct order, but it isnt working.

The check i used was to check the string vs a known string at a known position.

If the string is where it is supposed to be, then continue parsing, if not, I would use some other logic.

 

Basically, the problem is that I can read in the data, it just gets placed in the wrong index of the string array.

 

I attached some example code to explain what I am talking about.

Does anyone see why the code isn't working?
Or have recommendations on a better way to parse out the data.

 

code.PNG

Cory K
0 Kudos
Message 1 of 5
(2,915 Views)

The device automatically sends data, correct?

 

Does it send any termination character such as line feed or carriage return?

 

You are reading the serial port 5 times very rapidly grabbing however many bytes are available at the instant and building it into an array.

 

First, are you sure you are opening up the serial before an entire message comes in?  If you open up the port in the middle of an incoming message, you are going to wind up with only the last half of the message.

 

Second,  you are indexing out the array like you are getting an entire complete word each time you do a VISA read.  Depending on the number of bytes available at the instant you try to read, you might only get a fraction of a word, or you might get more than one word.  With your setup, you have no correlation between a given read statement and the number of bytes you are reading corresponding to a given word.  Now you might have a chance if you were able to set a termination character to a space, and every word ended in a space including "sentence " (notice the correct spelling hereSmiley Wink)  You have a termination character enabled, but it is at its default of being a new line character.

 

With reading a serial port, there is no good reason to create an array of strings.  Since a Serial Read is just a string of characters, it makes more sense if you concatenate all your individual reads into a single string value that you maintain from one loop to the next through a shift register.  Also, why read 5 times?  Why not read a sufficient number of characters in a single read.

 

Anytime you want to read from a device that sends data continuously, it helps if the message has some sort of format you can rely on.  Either a fixed number of characters, or always ends in a termination character that is never a part of the regular data, or has a preamble of lets say a byte or two that tells how many more bytes are left to read in the message.  If you have a clear message of exactly what is sent, then it might be easier to help you parse it.  As it is now, it seems like your device is just sending something something meaningless just to play around concepts.

 

I had to look up what the VI you were using was.  I had never seen Index String Array used before.  It seems you could have more easily used the very common Index Array function.

Message 2 of 5
(2,897 Views)

I'm not 100% positive, but I think the termination character is /n.

The device automatically sends data in the following format, once per second:

 

There are exactly 21 lines of data.

The first line (row0) is always '-->'

after that: 

 

--> 

line1 /n

line2 /n

line3 /n

etc

 

 

I'm not entirely sure why I build an array of the strings, I just figured it would be the easiest thing to work with since each line is a different piece of data. That way I can just index the line I want, without having to work with indexing out of one long string by length.

The code I have now works (somehow), its just that every once in a while (say once  every 5 or 10 min) it will mess up the array, having all the data at the wrong indexes.

 

Maybe the code I wrote was just a fluke Smiley Surprised

Cory K
0 Kudos
Message 3 of 5
(2,888 Views)

Does the --> line also end in a \n?

 

You have the termination character enabled, so use it.

 

Read a sufficiently large number of bytes (don't use bytes at port) that is larger than any message line you expect.  The VISA read will stop when it sees the termination character.

 

If you want, then you can do that in the For loop 21 times.  Go ahead and build into an array or concatenate to a long string.

 

Search the string or array for the -->.  If it's not where it should be, reindex your array, or break anything before it out of the long string.

 

I'm surprised your code only messes up once every 5-10 minutes.  I think the error occurs if you have let's say a 10 character string (ending with \n) but only 5 bytes have arrived by the time you start the VISA read.  Since 5 bytes are available, you read 5 bytes, but it is only the first half of the message.  Read again and get the remaining 5 bytes and the end of the message.  But now the 2nd half is indexed down one element and all following elements are off from where they should be.

0 Kudos
Message 4 of 5
(2,884 Views)

Ravens Fan wrote:

 

I'm surprised your code only messes up once every 5-10 minutes.  I think the error occurs if you have let's say a 10 character string (ending with \n) but only 5 bytes have arrived by the time you start the VISA read.  Since 5 bytes are available, you read 5 bytes, but it is only the first half of the message.  Read again and get the remaining 5 bytes and the end of the message.  But now the 2nd half is indexed down one element and all following elements are off from where they should be.


Yeh, that is exactly the error that is occuring. But somehow, it doesnt happen as frequently as I would have thought... luckily.

 

I will try your advice and let you know how it works.

Cory K
0 Kudos
Message 5 of 5
(2,858 Views)