-
|
I am trying to incorporate USB serial communication into a more complex program that does several operations between serial reads. While working on this, I have noticed a strange issue where any delay before the #![no_std]
#![no_main]
use arduino_hal::delay_ms;
use arduino_hal::prelude::*;
use panic_halt as _;
use ufmt::uwriteln;
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 9600);
loop {
delay_ms(100);
if let Ok(byte) = serial.read() {
serial.write(byte).unwrap();
continue;
}
}
}This program is supposed to read all incoming bytes, and echo them back to the serial port. The issue is, that the If I recreate the same program in C++, everything works as expected: void setup() {
Serial.begin(9600);
}
void loop() {
delay(100);
if (Serial.available() > 0) {
Serial.print(String((char)Serial.read()));
}
}Example input: ABCDEFGH This version of the program does exactly what it's supposed to do, which is echo back every character read with a 100ms delay. I assume this is happening due to some internal hidden buffering that exists in the Arduino C++ framework which presumably isn't implemented in the Rust HAL. Is there a way to make this program function the same way it does in C++? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
The serial implementation here in There are two solutions:
|
Beta Was this translation helpful? Give feedback.
You can activate the relevant interrupt using the
listen()method. Check the interrupt examples for some code example of setting up an interrupt handler.