I've got a problem with serial communication via the native port of an Arduino Due.
I'm using SAM core version: 1.6.12.
As discussed in https://forum.arduino.cc/index.php?topic=432148.0 a sketch halts completely once the SerialUSB write buffer is full. This is unacceptable for my use-case so I tried to prevent it using SerialUSB.availableForWrite() to check the fill level of the buffer and discarding the message.
This sketch runs fine until some connected device opens the serial port, reads a bit and closes it again. Then after a few seconds the sketch halts completely (led stops blinking).
The behaviour can be reproduced with this sketch:
unsigned long timer_ser = 0;
unsigned long timer_led = 0;
bool led_state = HIGH;
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, led_state);
SerialUSB.begin(230400);
}
void loop()
{
unsigned long ms = millis();
// print every 50 ms
if (ms - timer_ser > 50)
{
timer_ser = ms;
// print only if there is enough space in the buffer
if (SerialUSB.availableForWrite() > 80)
{
SerialUSB.print(ms);
SerialUSB.print(" ");
SerialUSB.print(SerialUSB.availableForWrite());
SerialUSB.println();
}
}
// toggle led every 500 ms
if (ms - timer_led > 500)
{
timer_led = ms;
led_state = !led_state;
digitalWrite(13, led_state);
}
}
Then, on your computer use minicom to read along for some time and close it. Alternatively you can run this python script (requires pyserial: pip3 install pyserial):
import serial
with serial.serial_for_url("/dev/ttyACM0", timeout=1) as ser:
print(ser.readline())
Then wait a few seconds and the led will stop blinking.
In the forum thread I linked above modifications to libsam are discussed. Is this the way to go?
I've got a problem with serial communication via the native port of an Arduino Due. I'm using SAM core version: 1.6.12.
As discussed in https://forum.arduino.cc/index.php?topic=432148.0 a sketch halts completely once the SerialUSB write buffer is full. This is unacceptable for my use-case so I tried to prevent it using
SerialUSB.availableForWrite()
to check the fill level of the buffer and discarding the message.This sketch runs fine until some connected device opens the serial port, reads a bit and closes it again. Then after a few seconds the sketch halts completely (led stops blinking).
The behaviour can be reproduced with this sketch:
Then, on your computer use minicom to read along for some time and close it. Alternatively you can run this python script (requires pyserial:
pip3 install pyserial
):Then wait a few seconds and the led will stop blinking.
In the forum thread I linked above modifications to libsam are discussed. Is this the way to go?