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

Serial.available() and Serial.read() not working with USB CDC virtual COM port #2055

Closed
vibhoregit opened this issue Jul 8, 2023 · 5 comments

Comments

@vibhoregit
Copy link

vibhoregit commented Jul 8, 2023

Describe the bug
Serial loopback with USB virtual COM port is not working as Serial.available() returns 0 always.

To Reproduce

  // Initialize the USB serial port
  Serial.begin(9600);
  pinMode(PC13, OUTPUT);
  digitalWrite(PC13, HIGH);
  while (!Serial);
  Serial.println(F("USB Serial Initialised"));
}

void loop() {
  // Check if any characters are available to read
  if (Serial.available()) {
    char ch = Serial.read();  // Read one character from serial buffer
    Serial.print(ch);         // Print the character back to serial monitor
  }
}

Steps to reproduce the behavior:

  1. Upload to blue pill board with either FTDI/Serial adapter or rogerclarke's bootloader
  2. Open COM port of the blue pill board which appears on connecting the board via its USB port
  3. Observe "USB Serial Initialised" to get printed but any character sent from serial monitor is not echoed back.

Expected behavior
"USB Serial Initialised" gets printed and whatever characters are entered in the serial monitor should echo back

Screenshots

Screenshot (133)

Desktop (please complete the following information):

  • OS: Windows 10
  • Arduino IDE version: 2.1.0
  • STM32 core version: 2.6.0
  • Tools menu settings if not the default: Available in screenshot
  • Upload method: Tried STM32duino (Serial) and Maple bootloader 2.0

Board: Bluepill

  • Name: Generic STM32F1 Series
  • Hardware Revision: Latest Revision (with correct resistor (1.5k) on D+ pin)
  • Extra hardware used if any: No extra hardware

Additional context
I tried a bunch of things to narrow down the problem so that actual problem can be pin pointed

  1. Uploaded CDC example code compiled on Keil microvision to test if windows drivers are correct: The board is able to transmit and receive data over USB and COM port driver seems to work fine.
  2. Tested Serial library with Serial1 to test if hardware serial works: The Arduino sketch works with Serial1 as expected and board echos back the data sent over serial adapter to its UART1.
  3. Tested the board with android device: CDC device is recognized and Serial USB Terminal app is able to connect to the COM port but as soon as a character is sent, the app hangs for a second and the COM port disconnects.
  4. Printing Serial.available() returns 0 to Serial monitor no matter how many characters are sent.
@fpistm
Copy link
Member

fpistm commented Jul 10, 2023

Hi @vibhoregit
I've tested with your sketch and my BluePill F103C8 and it works as expected. It fails only if it is large amount of data as stated in #1399.
Ensure you have a legacy mcu and a correct cable.

@bmo
Copy link

bmo commented Apr 21, 2024

I am also able to reproduce this issue, even when the "SerialUSB" device is used.
Serial writes work, however serial reads do NOT work. No characters are ever available. I've tried both with USB being the generic serial, and with it NOT being generic serial.

Desktop (please complete the following information):

OS: MacOs
Arduino IDE version: 2.3.2
STM32 core version: 2.7.1
Tools menu settings if not the default: Available in screenshot
Upload method: ST-Link clone on HW port

@zougloub
Copy link

I purchased some Blue pills and recently noticed too late that they contain STM32F103 clones, and exhibit this issue with Serial.available() in the Arduino core. I noticed that a workaround using Serial.read() == -1 would allow reads...

@Hackin7
Copy link

Hackin7 commented Jun 16, 2024

I'm having a similar issue too, serial reads do not work

@Yurisu
Copy link

Yurisu commented Jun 29, 2024

Whatever character sending Serial.read() is reads -1, and Serial.available() does not work

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

No branches or pull requests

6 participants