can USB be used to create a virtual com port?
-
- Posts: 51
- Joined: Thu Nov 05, 2020 10:26 am
Re: can USB be used to create a virtual com port?
But why do I get no output on Serial / LPUART1 as soon as CDC / SerialUSB is activated?
Re: can USB be used to create a virtual com port?
What is your code?
-
- Posts: 51
- Joined: Thu Nov 05, 2020 10:26 am
Re: can USB be used to create a virtual com port?
It is a quickly written test for playing with optocouplers
I added a bit to test different Serials/UARTs
done in sloeber, thats why there is the first include satement
I added a bit to test different Serials/UARTs
done in sloeber, thats why there is the first include satement
Code: Select all
// Do not remove the include below
#include "Test_L4R5.h"
long unsigned int currentmillis = 0;
long unsigned int pastmillis = 0;
HardwareSerial Serial3(PC11, PC10);
//HardwareSerial SerialLP1(PG8, PG7);
void setup()
{
Serial.begin(115200);
Serial3.begin(115200);
SerialLP1.begin(115200);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.println("L4R5 Test starting...");
}
void loop()
{
char SerialBuffer[128];
memset(SerialBuffer, 0, sizeof(SerialBuffer));
unsigned char offset = 0;
currentmillis = millis();
if((currentmillis - pastmillis) >= 1000)
{
pastmillis = currentmillis;
offset = snprintf(SerialBuffer, sizeof(SerialBuffer), "%6d ", int(currentmillis / 1000));
}
strncat(SerialBuffer, "Input 3 is ", sizeof("Input 3 is "));
if(digitalRead(3) == HIGH)
{
strncat(SerialBuffer, "High", sizeof("High"));
digitalWrite(5, HIGH);
}
else
{
strncat(SerialBuffer, "Low", sizeof("Low"));
digitalWrite(5, LOW);
}
strncat(SerialBuffer, ", Input 4 is ", sizeof(", Input 4 is "));
if(digitalRead(4) == HIGH)
{
strncat(SerialBuffer, "High", sizeof("High"));
digitalWrite(6, HIGH);
}
else
{
strncat(SerialBuffer, "Low\n", sizeof("Low\n"));
digitalWrite(6, LOW);
}
Serial.println(SerialBuffer);
Serial3.println(SerialBuffer);
SerialLP1.println(SerialBuffer);
delay(1000);
}
-
- Posts: 51
- Joined: Thu Nov 05, 2020 10:26 am
Re: can USB be used to create a virtual com port?
and this are the settings for the board:


-
- Posts: 51
- Joined: Thu Nov 05, 2020 10:26 am
Re: can USB be used to create a virtual com port?
Hah! Seems like I solved it 
I have absolutely no idea why, but redefining the Rx and Tx pins has fixed it!
I only added
before
and now I have output on all serial channels !
Although I am quite happy that it works now, I really would like to know about the "why"...
So if anyone can shed some light on this topic, that would be great!
I could not find out where - besides the variant_NUCLEO_L4R5ZI.cpp file the pins are set. Maybe its part of the magic that happens with the settings in the Arduino/sloeber IDE?

I have absolutely no idea why, but redefining the Rx and Tx pins has fixed it!
I only added
Code: Select all
SerialLP1.setRx(PG8);
SerialLP1.setTx(PG7);
Code: Select all
SerialLP1.begin(115200);
Although I am quite happy that it works now, I really would like to know about the "why"...
So if anyone can shed some light on this topic, that would be great!
I could not find out where - besides the variant_NUCLEO_L4R5ZI.cpp file the pins are set. Maybe its part of the magic that happens with the settings in the Arduino/sloeber IDE?
-
- Posts: 51
- Joined: Thu Nov 05, 2020 10:26 am
Re: can USB be used to create a virtual com port?
Now I noticed that output via the VCP is not happening in the first few moments...
The above code should output a startup message
but it appears only on th STLink Serial and not on the USB VCP Serial
the next loop, 1 second later is output on both channels
any idea why?
The above code should output a startup message
Code: Select all
Serial.println("L4R5 Test starting...");
the next loop, 1 second later is output on both channels
any idea why?
Re: can USB be used to create a virtual com port?
Add a while(!Serial); four USB issue. About the other one I will have a look next week anyway I see no reason why it does not work. Maybe Sloeber issue...
-
- Posts: 51
- Joined: Thu Nov 05, 2020 10:26 am
Re: can USB be used to create a virtual com port?
That thought occured to me as well, therefore I tried it in Arduino IDE with the same result.Maybe Sloeber issue...
No output on LPUART1 until I redefine the pins.
Thanks for the while(!Serial); idea!
Is this non-blocking, or do I have to add a timeout some way or another?
It takes about 350ms, so not tooooo long

-
- Posts: 51
- Joined: Thu Nov 05, 2020 10:26 am
Re: can USB be used to create a virtual com port?
Alright, found out myself about if(Serial) and while(!Serial)
I indeed have to assure that something is connected or have to implement some kind of timeout
Another thought:
can I check at runtime (i.e. in my code) whether CDC USB Serial is activated?
I indeed have to assure that something is connected or have to implement some kind of timeout
Another thought:
can I check at runtime (i.e. in my code) whether CDC USB Serial is activated?
Re: can USB be used to create a virtual com port?
OK I found why and it is normal (at least with the current implementation).
As Serial is no more mapped to SerialLP1 then the default pins defined in the variant_NUCLEO_L4R5.h (PIN_SERIAL_RX/PIN_SERIAL_TX) are not used as they are defined for the "Serial" instance.
So the correct way is to use setRX/TX.
I will check if I can use those defined pins when Serial is mapped to USB.
As Serial is no more mapped to SerialLP1 then the default pins defined in the variant_NUCLEO_L4R5.h (PIN_SERIAL_RX/PIN_SERIAL_TX) are not used as they are defined for the "Serial" instance.
So the correct way is to use setRX/TX.
I will check if I can use those defined pins when Serial is mapped to USB.