LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serial Packets: how to receive them correctly

hi to all, i need to receive serial packets in ordered manner, and after some minutes the packets start to enter mixed with previous packet.

 

this is an example packet i need to receive: A509 0C00 3F71 4ED8 1002 0000 0000 05

first 2 bytes are constant, never change. Last byte is CRC.

Its always a 15byte packet.

 

after some time labview is showing: 3F71 4ED8 1002 0000 0000 05A5 09 

thats only an example, but it moves around the packet, i assume because it is mixing the last packet with the previous packet.

 

what can i use in order to tell labview to always read the 15bytes starting at A509 ?

 

im just opening the port and reading the contents of the port with serial read ...

 

thanks!

0 Kudos
Message 1 of 15
(6,754 Views)

You are going to have to provide the synchronization code yourself. For example, in a loop, read 2 bytes. If the bytes are A509, then exit and read the next 13. After that, you can just keep reading 15 bytes. Don't use the VISA Bytes at Serial Port. Another method is to initially read 15 bytes and determine where the prefix is. Then calculate how many more bytes to read to get to the end of the packet.

Message 2 of 15
(6,749 Views)

thanks dennis, 

can you provide me a simple example to do this? i dont want to bother you, but if you can it will be of good help, im a newby trying to understand different techniques in labview.

 

thanks!

0 Kudos
Message 3 of 15
(6,746 Views)

@Dennis Knutson wrote:

You are going to have to provide the synchronization code yourself. For example, in a loop, read 2 bytes. If the bytes are A509, then exit and read the next 13. After that, you can just keep reading 15 bytes. Don't use the VISA Bytes at Serial Port. Another method is to initially read 15 bytes and determine where the prefix is. Then calculate how many more bytes to read to get to the end of the packet.




If I am not mistaken the first 3 bytes are a header and it appears the thrid byte is the packet length. Therefore the read algorithm can be modified to read 3 bytes. Validate the intial header value of A509 and then get th epacket length. If you have a valid header read the next n bytes where n is the length from the header.

 

The code to get yourself back in synch is a bit more challenging but doable.

 

BTW, since the OP had asked questions about this data in other threads one thing he should look at is how the header is defined. Given this data has a CRC it is reasonable to assume the header is well defined and meaningful. Therefore, use the information provided.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 4 of 15
(6,740 Views)

A way to handle streaming data is to have one loop that always reads all Bytes on the port, then enqueues them for processing in another loop. If the Header1 is good, go to Header2. If that's good, go to length and iterate over that. Save your CRC as you go.

0 Kudos
Message 5 of 15
(6,717 Views)

Todd, thanks for your diagram, can i please ask you to send a vi example so i can further understand the meaning of the different icons?

thank you very much, im using Labview 2010

 

thanks again.

0 Kudos
Message 6 of 15
(6,688 Views)

Drat, I can't find this sample code. The top half has:

 

Obtain Queue

VISA Bytes at Serial Port

VISA Read

String to Byte Array

Enqueue Element

 

 

The bottom half has:

 

Dequeue Element

Select

Increment

 

 

Make the enum (Header1, Header2, etc) a type def.

 

 

Either search the pallette, or (on a block diagram) press <ctrl>-spacebar and start typing the names above. This is called Quick Drop.

0 Kudos
Message 7 of 15
(6,665 Views)

thanks Todd again!

im attaching what i have. 

everything is working smoothly without errors, but now a dumb question... how do i get the results? i mean how can i get the packet starting at A5 in a string indicator or byte array?

 

thanks thanks!

0 Kudos
Message 8 of 15
(6,650 Views)

No need to overcomplicate things here with queues. The serial driver will take care of this. And it will also provide a 1024 bytes FIFO for the serial data by default. What you need to do is to get in synch with your serial data. Then you can just pull 15 bytes from the serial driver each time you read it. Your example is not good for much. So just drop that idea. And then you post code examples. PLEASE do not just post pictures. One more thing. you must also set up your serial port correct. Like baud-rate, stopbit(s), handshake and so on. What type of instrument are you using. Any link to documentation? 



Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
0 Kudos
Message 9 of 15
(6,642 Views)

Thanks Coq for your response.

I dont have a serial driver, this is a sensor that constantly transmits 15 byte packets, but sometimes i get out of sync, i need to read the packet always from the starting byte.

Here is the packet definition:

 

D0 D1     Header fix at A5 09

D2          Data Length (its fixed at 0C)

D3 D4    First Sensor Id

D5         Data Type:60=Sensor Reset, 61=Periodical signal, 62=Status Change Signal

D6 D7    Second Sensor Id

D8         Second Sensor Status11= Closed   10= Opened

D9         Battery StatusA0= Battery Charged   A1=Battery Discharged

D10 D11 First Sensor Value

D12 D13 Second Sensor Value

D14        CRC

 

so for example a correct packet will be: 

A5 09 0C 00 3F 61 05 1E 10 A0 00 05 00 04 05

A5 09 0C 00 3F 61 05 1E 10 A0 00 05 0C 2F 03 

A5 09 0C 00 3F 61 05 1E 10 A0 09 05 09 04 09

 

but after a while from reading the port, i start receiving in different order, for example: 1E 10 A0 00 05 00 04 05 A5 09 0C 00 3F 61 05

 

i need one simple example on how to sync this. i think it must be really simple, just i cant figure it out.

 

regards!

nico.

0 Kudos
Message 10 of 15
(6,630 Views)