LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Port, Receive 250 bytes with start byte as 'x'

1. I have to receive data on labview. Start byte is 'x', to indicate its starting. Data is coming continuously, jsut always data is coming marking 'x' as start byte

 

2. Then every next 5 bytes are like '1','2','3','4','5', I can then make up number like 12345 from it.

 

3. Currently what i am doing is I read 502 bytes & search for 'x' byte & from there split the string & read 250 bytes for data.

 

4. Reason for reading 502 bytes is , suppose I read 250 bytes only then suppose x comes at 130th place, then I dont have full 250 bytes of useful data. By reading 502 bytes I make sure alteast once 'x' is received with complete 250 bytes afterwards.

 

5. problem with this is one sample 250 bytes always get lost.

 

6. Can i program labview such that, it keep on waiting 'x' & as soon as 'x' is received then read 250 bytes & keep on looping.

 

7. Attached is vi.

 

 

0 Kudos
Message 1 of 6
(733 Views)

Make your termination character an "x" and read 251 bytes each time.

 

Toss your first read but from there on, the data preceding the "X" should be the previous message.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 2 of 6
(721 Views)

@Vindhyachal.Takniki wrote:

 

6. Can i program labview such that, it keep on waiting 'x' & as soon as 'x' is received then read 250 bytes & keep on looping.

 

 


Of course you can, you just need to program it.

 

First, does your data stream have any type of termination character?  If not, why not?  Can you control what is being sent so that the incoming stream has a termination character on the end?  It is odd that you set the termination character to 13 decimal, carriage return, on our Serial Configure, but you wrote False to disable it.  If it is False, no need to wire any constant into the setting for the character.

 

This is a rare case where bytes at port might actually be useful.  Read bytes at Port and read that many characters.  Search for the x.  If you get it, then you can read 250 characters after it,  (maybe less depending on where in your string the x maybe.).

 

OR, you can just always read what is in the port and append it to a shift register.  When you see the X and 250 characters after it, split out of the string and process it.  Put the remainder of the string pack in the shift register.  There should never be a reason for you to lose data.

 

As for your inner For Loop.  Get rid of all those +1 increments going to the index array.  They aren't needed.  Index Array automatically gives you successive indices when nothing is wired to an index.  And if nothing is wired to the top index, it will start at index zero by default.

 

I think your code in there is Rube Goldberg anyway.  Just take 5 characters of the string and use Scan From String with a %5d format string to get your integer value.

 

 

0 Kudos
Message 3 of 6
(715 Views)

Where did this protocol come from?  You are actually using an ASCII format, so I am going to go ahead and say that you have a bad protocol.  How many numbers are supposed to be read before the next 'x' comes in?  Your code implies 50.  But are they all for the same signal?

 

Assuming you are getting a single signal, you should just let your protocol be the 5 numeric characters followed by a Line Feed (LF, 0xA).  Then you just use the termination character to read each value individually.

 

If you are getting many signals at the same time, then I recommend putting a comma or space or tab between the signal values to mark where exactly a new value has started.  You can still use the termination character to stop the read.  Then you can use Spreadsheet String To Array to get an array of values for your signals.

 

Both of these avoid all of the mess that you are currently running into with minimal coding.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 4 of 6
(710 Views)

@RavensFan wrote:

This is a rare case where bytes at port might actually be useful.  Read bytes at Port and read that many characters.  Search for the x.  If you get it, then you can read 250 characters after it,  (maybe less depending on where in your string the x maybe.).


BAD BILL!  If nothing else, read 1 byte at a time until you read the 'x' and then read the other 250 bytes.  Simple loop with a Case Structure in it.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 5 of 6
(707 Views)
0 Kudos
Message 6 of 6
(650 Views)