UART - return of experience

Hope this helps to someone…
I had some trouble to read data from an electricity meter, which sends continuously serial data out, with the following format : 1200 bauds, 1 start bit, 7 data bits, 1 stop bit, parity:even, no handshake. I have first configured the uart like this :
It did not work, and I thought the problem came from my hardware…after a lot of hours fighting with my hardware, I came back to the program, and I tried to read the data in 8 bits :
this worked partially, sometimes the data was right, sometimes it was totally incoherent. I finally found the problem : the start bit was disturbing the data, so I cancelled it by making a AND operation between the data word and a mask in order to force the MSB to be equal to zero :;
if(data != -1) {
data = data & 127; // mask byte : 01111111 b (127 Dec)

(I tested the presence of data in the buffer before the test)

Is there any ‘hidden’ configuration possibility to avoid this start bit masking, or any other way to achieve this ?


The MSB (the bit you’re masking out) is actually sent last (UARTs are LSB first), so it’s not the start bit that’s disturbing your data - it’s the parity bit. You’ll almost certainly find that the bit is set for even numbers of 1’s in the data byte (or vice-versa, I can never remember which way round it’s calculated)

I suspect the meter is actually sending 7-even-1 data, and it’s possible there’s something wrong with the imp’s 7 bit UART config code. We’ll look into that (most people use 8 bit these days!).

I’d recommend you use (1200,8,PARITY_NONE,1,NO_CTSRTS) as right now you’re relying on the transmitter sending 2 stop bits (you’re receiving a 9 bit word with start & stop framing).

sounds familiar. my power meter sends 7 bits + parity too. i configured the imp for 8 bit and no parity, and mask the byte with data &= 0x7F

Yes, you’re absolutely right, I was in the wrong direction with LSB/MSB…It is the parity bit which is disturbing, this explains also why sometimes the decoded byte was OK, because the parity bit set to 0…
Thanks for the explanation, let me know if you find something in the UART code…

Ok, checked the code and there is a bug there because of the non-obvious way the STM32 is set up when you want 7 bit words with parity.

Currently, to get 7-even-1 you do need to use:


…and then mask out the top bit, just as you have done. This is because the STM32 considers the start bit part of the word length in configuration, which is absolutely the opposite of every other UART in the world :slight_smile:

We’ll get this fixed in the next release after release-23.

OK, thanks for the super-fast reply.
I will take care of that for the future