can USB be used to create a virtual com port?

Post here first, or if you can't find a relevant section!
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

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

Post by GonzoG »

STM32_Newbbe wrote: Thu Dec 01, 2022 10:25 am Another thought:
can I check at runtime (i.e. in my code) whether CDC USB Serial is activated?
Yes.

Code: Select all

if(Serial){..}
Will execute only if USB connection is active. You can check it with on board LED

Code: Select all

if(Serial)
	digitalWrite(LED_BUILTIN,0);
else
	digitalWrite(LED_BUILTIN,1);
LED state should change as soon as USB connection state changes. Just open/close serial monitor.
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 »

GonzoG wrote: Fri Dec 02, 2022 12:33 pm Yes.

Code: Select all

if(Serial){..}
well this works only for Serial Monitor open/closed, but not for a missing cable!
If I unplug the cable, the LED remains dark.
Only when I plug it back in, the LED blinks for e few ms and goes off again.

Not quite what I wanted
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

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

Post by GonzoG »

It won't detect missing cable. It just detects if connection with host have been opened or closed.
If it was broken, you'll have to check by sending data to host and wait for response. Without response, connection was broken.
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, I misinterpreted "open connection" then.
I assumed that a missing cable will be treated the same as a closed / no connection

Thanks for clarifying!
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

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

Post by GonzoG »

STM32_Newbbe wrote: Fri Dec 02, 2022 1:27 pm alright, I misinterpreted "open connection" then.
I assumed that a missing cable will be treated the same as a closed / no connection
Not how USB works. It's a host controlled interface. Lack of data from host doesn't necessary mean broken connection. Host might be busy with other devices.

The only way to detect if cable was removed is to connect Vbus (5V line) from USB to GPIO. Unfortunately no some boards it's not easy as USB 5V and 5V pin are not separated and if you use 5V to power board, you need to modify board.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

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

Post by ag123 »

design a protocol such that if it is not talking to a serial monitor, then simply move on and process other things.
that

Code: Select all

if(Serial){..}
should help in those designs.
in fact, from the host side, i often send a (single character) command, lets just say Identity - and it should respond with a word and maybe version.
this is to identify what device is connected from the host side.
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 »

GonzoG wrote: Fri Dec 02, 2022 2:09 pmNot how USB works.
Allright then, thats where my main mistake lies! I was too focused on this being a "serial" connection!

Thanks for explaining and clarifying. Up to now I hadn't dived into the depths of a real USB connection...
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

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

Post by ag123 »

For the Nucleo boards, I think one of the uart is routed to the on board st-link chip, and it presents a virtual com port to the host/pc.

This would normally be your Serial interface.
if what you really want is to bridge USB to your (LP)Uart pheriperial, a way is to write a little sketch to do something like such.
e.g.

Code: Select all

HardwareSerial mySerial = SerialLP1(PA10, PA9); // RX, TX, use the correct pins/ports

void setup() {
	Serial.begin(115200); 
	mySerial.begin(115200);	
}

void loop() {

	if (Serial.available()) {
		int16_t c = Serial.read();
		mySerial.write(c);
	}
	
	if (mySerial.available()) {
		int16_t c = mySerial.read();
		Serial.write(c);
	}
}

A sketch like such would pass data between your USB VCP and the actual uart port, in effect a bridge.
I occasionally used something like this, say on a 'blue pill' (stm32f103c8) board and use it as a usb-uart dongle.
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 »

Although it is not what I want, I thank you for this nice idea! Might really come in useful someday :)

What I wanted is activate the CDC USB Serial of Nucleo-L4R5, and simutaneously use some differente Usarts as well.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

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

Post by ag123 »

i did something like that with the 'old' 'rogers core'
https://github.com/rogerclarkmelbourne/ ... -w-signals

I made it listen for commands as well, using a "+++" (pause for 1/2 second) escape sequence.
if there is no pause before the next char, I assumed it is data.
works pretty well from a serial monitor

I actually once flashed updated a esp8266 AT rom doing just that with my 'blue pill uart dongle'

my guess is it is likely possible to port that to stm32 core. A catch is I hooked a usb callback to process usb requests.
e.g. things like setting baud rate etc. This is so that baud rates, line discipline (e.g. 8 N 1) can be passed from the serial monitor app directly to the uart.

one needs to figure out where to patch that same hook with the 'official' STM core.

--
as for USB CDC serial, you can try selecting that from the menu.
I think Serial would likely be replaced with USB CDC serial. Note that there are some complications as I think some stm32 soc has 2 usb interfaces, the default may go to the 'main' usb interface that is used for DFU updates.

An important concept is USB (CDC) serial is *not a uart*, baud rates etc don't apply, it is simply data at full speed 12 Mbps, but that it is multiplexed by the host, and pretty much every device gets a 1 ms time slice to 'talk' round-robin, again managed by the host via polling. The more devices on the bus (including the hubs (some are 'hidden')), the worse is the fragmented time slice, and hence lower multiplexed bandwidth available to your device. I tend to see about 1.x Mbps on my PC maximum.

then for the rest of uart ports they would likely need to be accessed from different names Serial1, Serial2, ... SerialLP1 etc
Post Reply

Return to “General discussion”