10-02-2024 09:10 AM
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
10-02-2024 11:27 AM
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
10-02-2024 11:31 AM - edited 10-02-2024 11:34 AM
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:
10-04-2024 01:31 AM
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.