03-08-2015 11:07 AM - edited 03-08-2015 11:09 AM
I have A device that sends a message over blutooth (spp) once a second and i want to read the device and record the output with out missing messages. problem is it seems like the timing gets off and i miss messages or get half messages sometimes. cant find a way to read consitant. The device does not seem to have a termination char being sent so its hard to find end of message and the number of bytes sent can vary. Any ideas???
thanks
brian
03-08-2015 11:09 AM
example of message
A1A2 A3A5 0000 9903 0012 020C 0805 1036 1824 2019 2801 3000 3800 4000 0017 020D 0800 1000 1800 2000 2800 3000 3894 7348 0050 0058 0000 1B02 0D08 0110 9803 1898 0320 9803 2800 3000 3893 3C48 8B25 5000 5800 0018 020D 0802 1000 1802 2000 2800 3000 3894 6348 EC0E 5000 5800 0018 020D 0803 1000 1800 2000 2800 3000 3894 6348 A65E 5000 5800 0018 020D 0804 1000 1802 2000 2800 3000 3894 6348 FA32 5000 5800
03-08-2015 11:14 AM
Is there anything in the message stream that tells you how long a message is? Such as the first 1 or 2 or 4 bytes saying how many bytes there are to read? If so, read that many bytes, then immediately decode that to determine how many more bytes to read (thus getting rid of the the bytes at port method).
If not, how do you know where one message ends and another message begins?
You can read conitnuously and build your message stream up in a string shift register. Keep looking at the string and once you get a complete message, delete that out and process it, leaving the remaining bytes in the shift register.
03-08-2015 11:32 AM
there does not seem to be message lenght. The message does start the same way every time but the end can change
03-08-2015 11:36 AM
Okay, then you can do what I said in the last paragraph. Keep accumulating your reads in a shift register until you can determine that one message has ended and the next one has begun.
03-08-2015 12:57 PM
maybe im not fully getting your idea there is alot of time when there in nothing in the buffer if i read all the time since the device only sends message once a second
03-08-2015 02:01 PM
03-08-2015 02:10 PM
Then why do you have a .02 second delay in your loop? You are reading 50 times per second on something that is only being sent once per second?
Why are you flushing the receive buffer? If your message is incomplete, you are discarding any bytes that arrive arrive during that .02 seconds. What made you think it was a good idea to flush the buffer? Flushing the buffer rarely, if ever, needs to be used.
03-08-2015 03:26 PM
was flushing the buffer to make sure there was no parts of a old message on the next read
03-08-2015 04:20 PM
But in reality those are probably bytes that belong to your current message.
Flush a toilet. Don't flush a buffer.