07-20-2022 05:01 AM - edited 07-20-2022 05:05 AM
Hello!
I'm new to LabWindows and currently trying to write a code for a sensor.
The sensor is connected by COM-Port and delivers a sequence of 63 14-byte messages. So for example:
02 00 00 00 00 00 00 00 00 00 00 00 00 03
02 01 00 00 00 00 00 00 00 00 00 00 01 03
...
02 3E 00 00 00 00 00 00 00 00 00 00 3E 03
With 02 as start byte and 03 as end byte. The second byte (00...3E) determines the parameter that is described by the following bytes.
After reading the data it is saved to an array which I want to analyze for the parameters.
Now to my problem: The sensor has to keep running the whole time since some parameters have an averaging time.
So when I take a measurement (press "Acquire") the data starts at a random sequence and might be saved as:
03 02 38 00 00 00 00 00 00 00 00 00 00 38
03 02 39 00 00 00 00 00 00 00 00 00 00 39
...
Right now I'm using a (probably) rather unusual approach with
ComToFile(COM_PORT, fileHandle, 1000, 0x3E); //Write to a file until reaching the termination byte
ComRd(COM_PORT, buffer, 1000); //Start reading after termination byte
for (i=12; i<900; i++) // Transfer data from buffer to array, "delete" 12 bytes after termination byte so it starts with sequence 0
{
array[i-12] = buffer[i];
}
For now it lets me keep working but it's obviously not a good solution to keep in.
Thanks!
07-23-2022 03:00 AM
I don’t know what your sensor is but I don’t think it is safe to assume that 0x3F is not possibly contained in your binary data, so your method can be getting confused then. What usually should be done with a device like yours with binary data transfer is that your 0x02 (Start of Text, SOT) and 0x03 (End of Text, EOT) data bytes stand on their own or are getting escaped if they happen to occur in your binary data. So your data messages could potentially contain more than 14 bytes. I would use 0x03 as termination character and detect the escaping sequence to keep reading more data until the real EOT is received and then analyse the messages for the starting sequence number.