arduino / arduino/ArduinoCore-sam
availableForWrite() always returns 511, results in halting the sketch
- Dominant language
- HTML
- Stars
- 91
- Forks
- 112
- PR merge metrics
- No merged PRs in 30d
Description
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](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:
```ino
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`):
```python3
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?
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with SerialUSB.availableForWrite() in the SAM core and review the libsam modifications discussed in the linked forum thread. Reproduce the halt with the provided Arduino Due sketch, using minicom or the supplied pyserial script to open, read, and close the port. Done means the sketch continues blinking and does not halt when the serial port closes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- arduino, cpp
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100