Missing data when reading from USART on Arduino Uno
I'm trying to buffer USART data before processing it on Arduino Uno. The modified test can be found here. The test reads the input in 10-byte chunks (or until EOL) and prints the chunks back. Somehow, the data gets missing after the first chunk. For instance, here are two inputs and the corresponding results.
Hello from Arduino!
01234567890123456789
Got 0123456789
Got 01
abcdefghijklmnopqrstuvwxyz
Got abcdefghij
Got klxyz
Got
I translated the test into an Arduino sketch, and it seems to work just as expected - the device prints 10-byte chunks plus \r, \n.
Hello from Arduino!
Got 0123456789
Got 0123456789
Got
Got
Got abcdefghij
Got klmnopqrst
Got uvwxyz
Got
I can imagine that my Rust code is not correct. Will highly appreciate any help.
since avr-hal doesn't have any buffer to receive uart while you are not reading it will drop bytes that received while you weren't reading
writing is just as fast as reading, if you wrote 10bytes, you can't read while there's 10bytes being received,
but you seem to get two bytes first (01 and kl), i think that's due to how UART hardware works
I'm trying to translate some C++ arduino code to rust and running into the same thing.
Is there an example somewhere on how to do this correctly? With buffering incoming data while it's being processed or data is written back?
i see there's listen that says it's enables an interrupt but i don't see how to actually receive the interrupt?
@bdew https://docs.rs/avr-device/latest/avr_device/attr.interrupt.html
So it's not something provided by the hal at all?
That makes the whole serial interface not very useful if i have to deal with low level register stuff just to be able to receive more than a single byte
Edit: To clarify what i mean, and please correct me if i'm wrong
The options i see if i want reliable duplex serial communication is
- Poll the read method in a tight loop and never block on accessing any other peripheral for longer than a single character takes to transmit at the chosen baud rate
- Implement my own ISRs and ring buffers and completely forego the HAL for serial - which would require a bunch of unsafe code and global mutable state
- Implement some kind of software flow control?