cancel
Showing results for 
Search instead for 
Did you mean: 

After UART send the bytes from the buffer, \0 is also received.

RPG
Senior

Why I get \0 at the end of the transmission?

I imaging the \0 is a NUL termination character but I have seen from the terminal that sometimes is not received or arrives with a big delay.

Could somebody tell me anything about it?

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Piranha
Chief II

Probably the receiver has no pull-up on it's Rx line and, when the MCU releases the Tx pin, the whole line is floating. If the line goes low, such a situation could be interpreted as a start bit + 0x00 data byte. You can test/solve it by enabling a pull-up on MCU's Tx pin.

And yes - you need to understand why the original code disabled the transmitter at all.

View solution in original post

25 REPLIES 25
gbm
Lead III

The UART sends exactly what you order it to send. Check your code.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Hard to say, use strlen() not sizeof() for strings passed to the output routines, the latter with describe/include the trailing NUL too.

The TX pin should normally be HIGH between transmissions.

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

The code I saw from the previous developer is using the registers to send the content of the buffer. In the buffer there is no \0.

Don’t remember the use of sizeof or strlen. But will check out as soon I arrive to my office.

>>> The TX pin should normally be HIGH between transmissions.

I have removed this line of code and now I don't see the \0 at the end:

USART1->CR1 &= ~USART_CR1_TE; // Disable transmitter

I find out that line is disabling the transmitter by putting low bit on the third bit of the CR1 register of the UART. And now is not sending the \0. Is it ok that the transmitter is always enabled? 

@Raúl Pérez Gómez​ "I find out that line is disabling the transmitter"

Well, that is what the comment says! ��

"Is it ok that the transmitter is always enabled?"

It's fine as far as the STM32 is concerned.

Whether it's a Good Thing for your particular application is an entirely different question - you're going to have to research why your predecessor(s) did that; eg,

  • Is the link shared with other things?
  • Is it for power saving?

After I removed the line USART1->CR1 &= ~USART_CR1_TE; // Disable transmitter it seems that the TX buffer is not getting flushed. Is there a way to flush it which is not disabling the TX on the CR1 register?

@Raúl Pérez Gómez​ "it seems that the TX buffer is not getting flushed"

How do you determine that?

Before I removed the disable TX I was receiving \0 at the end of the transmissions from my device to may python script. I wanted to avoid that.

After I removed that line I get data from the prior command when transmitting from the device to my script.

Sorry I am quite new and I am quite lost.