Skip to content
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

availableForWrite() always returns 511, results in halting the sketch #105

Open
tfeldmann opened this issue Apr 30, 2020 · 0 comments
Open

Comments

@tfeldmann
Copy link

tfeldmann commented Apr 30, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant