LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Continuous serial data readout with checksum

Hello everybody!

 

I'm currently working on a program to continuously read serial data, which I then want to plot and analyse. 

The device I'm using has a "Fast Data" mode, which means it sends 9 bytes of data every 5 ms. The packet is valid, if the 8 bit sum of bytes 1 to 9 is zero.

 

I haven't worked with checksums in a continuous way like this before but recently found out about queues and thought maybe they would be appropriate here?

 

Basically I just took the queue example and adapted it to my needs. In the first loop I read out all bytes which are at the port each 5 ms and queue them up for my second loop. In the second loop I put all the data in an array and go through it in steps of 9 bytes to make sure the packet is valid. If it's valid, I increase the index by 9 and continue the same approach. If the packet is not valid I increase the index one by one until the packet is valid (sometimes there are some readout errors/false bytes which shift the packets) and then repeat the previous step. From the packet I only need the timestamp (byte 1+2) and the measured value (byte 3+4), which I convert into the correct type and then safe in an array.

 

My approach works, but as time progresses I guess the program can't keep up and falls more and more behind. I want to do some waveform analysis on the data and compare it to a second device, so it would be nice if the data doesn't fall too far behind. 

 

It's my first time working with data like this, so I would appreciate if you could give me some advice to improve the efficiency of my program. The VI is shown below and also attached. 

 

Thank you!

 

Best regards

ChecksumVI.png

 

 

 

 

0 Kudos
Message 1 of 4
(286 Views)

I don't have time to really dive into this, but my immediate issue with your code is that you are constantly building to an array, which is extremely inefficient. I was burned by this when I was new to the communications realm. Instead, use the communication protocol and let VISA do more of the work for you.

 

For more details, we need to know details on the protocol being used (how the frames for the messages are formatted). But my standard response is to go watch this: VIWeek 2020/Proper way to communicate over serial


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 2 of 4
(250 Views)

Can you do a save for previous (2020 or below) because I cannot open your code on my current computer.

 

Make sure to include a few typical received 9byte strings so we can simulate.

 

There is quite a bit of code smell here:

 

  • Why don't you use a string queue?
  • Index array is resizeable
  • Summing a U8 array will wrap automatically, no need to use Q&R
  • There is a +1 primitive
  • Your bit gymnastics could probably be replaced by a single operation
  • The correct way to append a scalar to a 1D array is "built array", not "insert into array".
  • Your build xy graph express VI could be replaced by a simple "Re/IM to complex".
  • Not sure why you keep an ever-growing array that is never really used except to process a few points? (lowest shift register). 
  • The lower loop timing is determined by the queue, no need for a wait.
  • You know that there are 9 bytes per message so why do you use "bytes at port"????
  • etc.
0 Kudos
Message 3 of 4
(247 Views)

Remove the lower loop wait operation.

In fact, remove both waits and read excatly 9 bytes from the serial port.

With bytes at port, you are creating more than one queue element per packet. Your lower loop can only process one queue element every 5ms and so it falls behind.

 

Also, when you parse a package, throw away the data that you just parsed. That means take the subset of the buffer array starting at your index position and wire it to the shift register, reset the index to zero. Oherwise, your array grows indefinitely. It is not a huge problem at this low data rate and low runtime, but it is still a memory leak.

 

0 Kudos
Message 4 of 4
(178 Views)