-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MicroBitSerial.cpp if(newHead != rxBuffTail) This may result in loss of received data. #460
Comments
Related? #441 |
This problem occurs when the queue is full.My computer has been sending this sentence(0xFF 0x01 0x02 0x03 0x04 0x05 0x06 0xAA) very quickly.Then microbit prints the received data as follows: And then I did some code masking (in void MicroBitSerial::dataReceived() function ):
Then the computer sends the same data(0xFF 0x01 0x02 0x03 0x04 0x05 0x06 0xAA),Then microbit prints the received data as follows: You can see that when the queue is full you lose data, but many frames of data are not cut off.The queue is full but the data is relatively complete. So Does this code( if(newHead != rxBuffTail) )in void MicroBitSerial::dataReceived() function cause the data to be cut off? |
When the buffer is full, incoming bytes are dropped. When read() removes one byte, the next incoming byte will be added. By commenting out the buffer full check, incoming bytes are overwriting old data. Data is still being lost. Reading will potentially get recent bytes followed by older ones. Try sending frames with a counter, then see how the data looks: Serial works one byte at a time. Maybe set the buffer size to be a multiple of 8, check the size of the data available and read in multiples of 8. |
My microbit code:
I take your advice,sending frames with a counter,My computer sent 255 frames of data,Then microbit prints the received data as follows: Then By commenting out the buffer full check,The microbit prints the received data as follows: By comparison, I prefer choice commenting out the buffer full check.Because even if the data is lost, the data frame is more complete.But I don't know what happens to the other code if I commenting out the buffer full check. |
Hi, martinwork. what happens to the other code if I commenting out the buffer full check? |
I'm sorry, I don't understand what you're asking. Did you reading 8 bytes at a time? |
I reading 1 bytes at a time (uBit.serial.read( &data ,1, ASYNC);). Because it was the computer communicating with microbit, When my microbit handle serial port data, I judge the header bytes(0xAA) first. then The data is read until the end of the byte(0xFF) is read , Read through one frame and then process one frame of data. If a byte is lost in transit,I might not be able to read the correct data frame by reading 8 bytes. So I can't read 8 bytes at a time. |
I found a problem during the serial communication test: My computer sends data continuously through the serial port ( baud(57600) ) and no delay. and then my microbit just do one thing:The data is read and sent to the computer:
Then,I found that a lot of the data sent to the computer had been lost. I see the source inside this sentence : if(newHead != rxBuffTail). that will cause my data to be lost. After I mask this code (if(newHead != rxBuffTail)), the data will still be lost, but much less.Is there any way to ensure the integrity of my data?Even if it overwrites the original data.
The text was updated successfully, but these errors were encountered: