06-11-2020 06:55 AM
Hello,
i am using a stm32f407vgt6 discovery board and the virtual comport to transmit 16 bytes of data from my µC to the pc.
The read buffer contains eg. 08EF FFFF 08F4 FFFF 08EA FFFF.
Now i want to seperate them bytewise and save the single bytes to an array, but there is no delimiter i can use. I tried to use spreadsheet to array, but this didn't worked.
Maybe you can tell me, how to make it work.
Thank you.
Solved! Go to Solution.
06-11-2020 07:39 AM - edited 06-11-2020 07:39 AM
@FaDen93 wrote:
Now i want to seperate them bytewise and save the single bytes to an array, but there is no delimiter i can use. I tried to use spreadsheet to array, but this didn't worked.
Of course not. You are dealing with binary data. Spreadsheet String implies ASCII formatted data. So in order to get anything useful out of the data, we need to know the format of the data (ie what bytes mean what).
And a shameless plug: VIWeek 2020/Proper way to communicate over serial
06-11-2020 08:15 AM - edited 06-11-2020 08:15 AM
Hello,
thanks for your reply. The aim is to calculate the expansion of batterie cells. I use 12 bit ADCs to sample the data. So always 16 bit represent a strain gauges sensor value.
A string to byte array seems to work, but then I have to combine the bytes into words. Don't know if this is easier then other ways.
06-11-2020 08:56 AM
@FaDen93 wrote:
I use 12 bit ADCs to sample the data. So always 16 bit represent a strain gauges sensor value.
So your microcontroller is just spitting out 16-bit data? How would you know if you are aligned properly on your read? For example, your microcontroller spits out "00FF 55AA". These are 2 samples, right? Now let's say you open up the port on the computer side and the first thing you read is "FF55". This will happen because the port was opened after the first byte as sent, but before the second was received. So you will be mixing two samples together and get complete garbage for data. So you need to define a frame of data to ensure you are reading the data properly. This frame is typically set up something like the following:
1. A sync byte. This byte is typically 0x2, but I have seen all kinds of sync byte values.
2. A message ID and/or message length. This will tell you how much data is in the frame of data and/or how to interpret the data. Since you are just spitting out a constant 16-bit value, this could probably be omitted.
3. The data
4. A checksum or CRC. This is a data validity check If this fails, you either did not sync to the right byte (the sync byte could be in any other field) or there was an error in the transmission. The simplest checksum is to just perform a U8 addition on the data (Add Array Values).
With that said, go watch the video from my shameless plug to see how to use this setup. There is also a link to the example code in GitHub that you can use.
06-11-2020 09:00 AM - edited 06-11-2020 09:04 AM
After re-thinking it, maybe you need to read 32bit chunks and make sure word #2 is FFFF. If it's not, you're out of sync.
06-11-2020 11:34 AM - edited 06-11-2020 11:35 AM
@FaDen93 wrote:
Hello,
i am using a stm32f407vgt6 discovery board and the virtual comport to transmit 16 bytes of data from my µC to the pc.
Who is writing the code running on the Discovery Board?
I am guessing it is you, so you should add a delimiter or at least a Termination Character to the data you are sending.
06-14-2020 07:50 AM
Hello,
sorry for the late reply, i had to fix some bugs in my program first. Yes, i write the code for the µC myself. The problem by using a termination character is, that my readings can occur over the whole data range. The probability that I accidentally hit the termination character is very high then.
I decided to add a start byte (0x20) for sync and use a CRC to check if the recieved packet ist ok.
Now some more questions.
How do i implement to scan for the start byte
Is it still ok to use the VISA read function to read the data bytes? I saw some examples using the property node for this.
06-15-2020 05:54 AM - edited 06-15-2020 05:55 AM
@FaDen93 wrote:
Now some more questions.
How do i implement to scan for the start byte
Is it still ok to use the VISA read function to read the data bytes? I saw some examples using the property node for this.
You read 1 byte at a time until you read the start byte. Then you read the data and CRC and perform the CRC check. If the check passed, you have valid data. If it fails, throw it away. Either way, go back to reading 1 byte until the start byte is read.
Here is a guide from my presentation I linked to above. It is using a command byte as well, so you can remove that part.
06-17-2020 05:45 AM
Thank you very much. Now it's working perfectly.