can USB be used to create a virtual com port?

Post here first, or if you can't find a relevant section!
STM32_Newbbe
Posts: 50
Joined: Thu Nov 05, 2020 10:26 am

Re: can USB be used to create a virtual com port?

Post by STM32_Newbbe »

But why do I get no output on Serial / LPUART1 as soon as CDC / SerialUSB is activated?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: can USB be used to create a virtual com port?

Post by fpiSTM »

What is your code?
STM32_Newbbe
Posts: 50
Joined: Thu Nov 05, 2020 10:26 am

Re: can USB be used to create a virtual com port?

Post by STM32_Newbbe »

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

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);
}
STM32_Newbbe
Posts: 50
Joined: Thu Nov 05, 2020 10:26 am

Re: can USB be used to create a virtual com port?

Post by STM32_Newbbe »

and this are the settings for the board:

Image
STM32_Newbbe
Posts: 50
Joined: Thu Nov 05, 2020 10:26 am

Re: can USB be used to create a virtual com port?

Post by STM32_Newbbe »

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

Code: Select all

	SerialLP1.setRx(PG8);
	SerialLP1.setTx(PG7);
before

Code: Select all

	SerialLP1.begin(115200);
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?
STM32_Newbbe
Posts: 50
Joined: Thu Nov 05, 2020 10:26 am

Re: can USB be used to create a virtual com port?

Post by STM32_Newbbe »

Now I noticed that output via the VCP is not happening in the first few moments...

The above code should output a startup message

Code: Select all

	Serial.println("L4R5 Test starting...");
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?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: can USB be used to create a virtual com port?

Post by fpiSTM »

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...
STM32_Newbbe
Posts: 50
Joined: Thu Nov 05, 2020 10:26 am

Re: can USB be used to create a virtual com port?

Post by STM32_Newbbe »

Maybe Sloeber issue...
That thought occured to me as well, therefore I tried it in Arduino IDE with the same result.
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 :)
STM32_Newbbe
Posts: 50
Joined: Thu Nov 05, 2020 10:26 am

Re: can USB be used to create a virtual com port?

Post by STM32_Newbbe »

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?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: can USB be used to create a virtual com port?

Post by fpiSTM »

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.
Post Reply

Return to “General discussion”