BluePill Serial communication problem (data not received)

Hi everyone!
I’m trying to use BluePill to communicate over built-in USB using Serial mode - and cannot get 2-way communication working…

Data from BluePill → desktop is transmitted, but nothing is received.
Tried another board - same result
If I use some sample program in Cube32IDE - both receiving and transmitting works, so that’s not a hardware issue.

here’s my environment:

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
lib_deps = thijse/CmdMessenger@^4.1.0
monitor_speed = 9600
upload_protocol = stlink
upload_flags = -c set CPUTAPID 0x2ba01477
build_flags = 
    -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
    -D USBCON
    -D USBD_USE_CDC
    -D ENABLE_USB_SERIAL
    -D USB_MANUFACTURER="UnknownMFG"
    -D USB_PRODUCT="BluePill"
    -D USBD_VID=0x0483
    -D USBD_PID=0x5740
    -D HAL_PCD_MODULE_ENABLED     
;monitor_dtr = 0
debug_tool = stlink

and here’s the code itself:

#include <Arduino.h>
const uint8_t kbStdLed = PC13;        // Pin of Built-in LED
unsigned long lastTransmission;

void setup()
{
    Serial.begin(9600);
    while (!Serial);
    pinMode(kbStdLed, OUTPUT);
}
void loop()
{
    uint8_t incomingByte;
    if ((millis() - lastTransmission) >= 2000)
    {
        Serial.println("Bbbnbnbas");   //<- this works
        lastTransmission = millis();
    }
    if (Serial.available() > 0)
    {
        digitalWrite(kbStdLed, digitalRead(kbStdLed) ^ 1);    //<- this doesn't work
        incomingByte = Serial.available();  
        Serial.print("I received: ");                 //<- this doesn't work
        Serial.println(incomingByte, DEC);   //<- this doesn't work
    }
}

Upon uploading the code, LED is off

After I connect via terminal program, it goes on - and stays on even if I send something

Serial.available() returns the number of bytes available to read.

Serial.read() reads an available byte, or returns -1 of none can be read.

Since in this case, you have already guarded executed with if(Serial.available() > 0), you can just

        incomingByte = (char) Serial.read();
        Serial.print("I received: ");
        Serial.println(incomingByte);

You might also have to assert DTR = 1 in the platformio.ini, it it might ignore all input.

can you clarify please?
I tried like that:
-D DTR=1 in Build Flags - no changes

if I add to environment section - there’s a warning:
Warning! Ignore unknown configuration option dtr in section [env:genericSTM32F103C8]

Correct, I was trying to get at least some feedback (number of bytes sent in this case) - but no luck so far

First line (LED changing status) is to indicate if “IF” statement being fired - which apparently didn’t

Serial.println("Bbbnbnbas");   //<- this works
Serial.print("I received: ");                 //<- this doesn't work

You already have the DTR I meant, just commented out. Comment it in, and try 0 as well as 1.

Then Serial.flush();

didn’t help

tried a) after printing something
b) before checking if any incoming bytes available

no apparent changes…

  • when I check “DTR” in my terminal program - transmission starts (“Bbbnbnbas”),
  • when uncheck - stops
    but incoming data is not read in both cases

I also noticed something strange: if I try to send something, this output “freezes” for ~2 seconds and then prints out 3 lines at once

Serial.println("Bbbnbnbas");

This is working just fine-

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
framework = arduino
build_flags = 
    -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
    -D USBCON
    -D USBD_USE_CDC
    -D ENABLE_USB_SERIAL
    '-D USB_MANUFACTURER="UnknownMFG"'
    '-D USB_PRODUCT="BluePill"'
    -D USBD_VID=0x0483
    -D USBD_PID=0x5740
    -D HAL_PCD_MODULE_ENABLED
upload_protocol = cmsis-dap
monitor_dtr = 1
#include <Arduino.h>
const uint8_t kbStdLed = PC13;        // Pin of Built-in LED
unsigned long lastTransmission;

void setup()
{
    Serial.begin(9600);
    while (!Serial);
    pinMode(kbStdLed, OUTPUT);
}
void loop()
{
    char incomingByte;
    if ((millis() - lastTransmission) >= 2000)
    {
        Serial.println("Bbbnbnbas");   //<- this works
        lastTransmission = millis();
    }
    if (Serial.available() > 0)
    {
        digitalWrite(kbStdLed, digitalRead(kbStdLed) ^ 1);    //<- this doesn't work
        incomingByte = (char) Serial.read();
        Serial.print("I received: ");
        Serial.println(incomingByte);
    }
}

Same in hterm

Thanks a lot for checking!
It looks like I’m out of luck with those clone boards then…

How did you manage to find original one?

I remember someone mentioning “wrong USB pullup resistor” during my searches - could this be the case here?

Can you make anything out of this picture?
R10 is 1.5K - looks correct
R9/R11 are 22 Ohm - close enough as well

image

If you can get the board to be recognized via USB and send this data, then there is no problem with the USB pullup resistors.

Status update: I finally got USB communication working both ways.
Cheap clone board was the culprit… As soon as I got original one - the same code started working just fine

image