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

CDCACM_::available() returns the count of what's already been read #56

Open
algernon opened this issue Dec 1, 2021 · 0 comments
Open
Labels
bug Something isn't working Component: Serial Regarding UART / Serial component

Comments

@algernon
Copy link
Contributor

algernon commented Dec 1, 2021

This is not an issue in the current core, but in @bjc's bjc/gd32usb-test branch. As issues aren't available in the fork, and the feature will - hopefully - make it back here in the not too distant future, I'm opening the bug here.

The core of the issue is that CDCACM_::available() returns the count of what's already been read into the buffers, rather than what's really available, rendering the function almost useless.

To showcase the problem, use the following sketch:

#include "Arduino.h"

void setup() {
  Serial.begin(9600);
};

void loop() {
  if (Serial.available()) {
    uint8_t b = Serial.read();
    Serial.println(b);
  }
}

Feeding data onto the serial port from one terminal, and reading from the other, this currently never outputs anything.

If we change the sketch, and do a read before calling .available():

#include "Arduino.h"

void setup() {
  Serial.begin(9600);
};

void loop() {
  uint8_t b = Serial.read();
  if (Serial.available()) {
    b = Serial.read();
    Serial.println(b);
  }
}

And then feed the port with two bytes written at a time (eg, echo a >/dev/ttyACM1, which will send a newline along), we'll quickly discover that the first byte is read, and because we read more than one byte at a time into an internal buffer, .available() will return 1 too, and we'll read the second byte too.

@algernon algernon added bug Something isn't working Component: Serial Regarding UART / Serial component labels Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Component: Serial Regarding UART / Serial component
Projects
None yet
Development

No branches or pull requests

1 participant