cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect that console have just connected to HAL UART?

HTD
Senior III

I need my code called when something connects to the HAL UART.

I have a simple mini-debug that just prints something to the UART and I see it on a console connected to the STM debug COM port. I noticed that the HAL_UART_Transmit() always returns HAL_OK, no matter if there is anything listening on COM port or not.

Is there a way to detect connect / disconnect event?

Well, if there is no obvious way, maybe I could receive on that port? But then again - when there is a pending RX operation - can I transmit in that time?

My goal is that the Nucleo board should notify about events on the console, but when the console is just connected and nothing is being sent at the moment, I need to be able to query the board about its status by for example pressing a key on console, or just configure the session to send anything on starting the connection. So - the board should do its things, wait either for environment change (like temperature changed) and at the same time wait for user input, like key pressed. I'm just not sure what would happen if the board would have pending receive and I try to transmit. Would it wait? Would it fail? Would it succeed, while the receive operation would still be waiting for the input?

Do I need to transmit in interrupt mode? Or can I just receive in interrupt mode? When polling mode is used, are the interrupts still triggered and my registered callbacks would be called?

2 REPLIES 2
Andrew Neil
Evangelist III

"I noticed that the HAL_UART_Transmit() always returns HAL_OK, no matter if there is anything listening on COM port or not."

Indeed: it just transmits out of the Tx pin - there is no way to tell if anything is actually receiving what was sent.

Just like when you speak, there is no way to tell if anyone actually hears what you say!

"Is there a way to detect connect / disconnect event?"

You would have to provide some way for a terminal to indicate that it's listening; eg,

  • The terminal sends something to the UART receiver;
  • The terminal sets a pin to a certain state

But there is no harm in having a UART transmitting when nothing is connected.

"I'm just not sure what would happen if the board would have pending receive and I try to transmit."

The UART hardware is full-duplex; ie, it can send & receive at the same time

"Do I need to transmit in interrupt mode?"

You don't need to.

"can I just receive in interrupt mode?"

You can if you wish.

"When polling mode is used, are the interrupts still triggered"

Only if you enable them

It's just signalling a pin going up and down, the interface would need to get a lot more involved to detect other things happening.

For example, the thing you're connecting has a weak pull-down, on a signal that's normally high, I've used that on a product that has an I2C interface, but can be mapped to a UART for debug, and detect when my serial dongle is attached.

Some RS232 level converters have signalling pins (fault?) that can indicate if anything is attached to the externally facing connector. I've used those to selectively power the interface.

For NUCLEO's perhaps use the SWV Debug Communication Channel, for this type of output, and detect that debugger is connected, and related DWT/ITM registers are configured.

U(S)ART Receive and Transmit are independent functions, one can be used without the other, one can be polled, the other interrupt driven. You write the software, you get to configure the hardware.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..