Help uart.read() with \r\n

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
Bionaught
Posts: 5
Joined: Sun Apr 08, 2018 5:06 pm

Help uart.read() with \r\n

Post by Bionaught » Sun Apr 29, 2018 8:16 pm

I'm getting the serial data I want from a medical device, but uart.readline is returning on \newline & \carriage return, as shown here.
I'd like it to just do this on new lines and not for \n & \r. I've tried every combination of parameters in uart.readline('\n\r'), uart.readline('\n) uart.readline('\r')... passing parameters seems to have no effect what so ever. I can't find any documentation on this?
I'm new to programming in Python & programming full stop :lol: Any pointers will be appreciated.
b'$$810818076011147\n'
b'\r'
b'$$8203178256\n'
b'\r'
b'$$83042150000\n'
b'\r'
b'$$84042160000\n'
b'\r'
b'$$8506059000000\n'
b'\r'
b'$$86042312560\n'
b'\r'
b'$$87042280531\n'
b'\r$$8803016RUN\n'
b'\r'

Code: Select all

from machine import UART           # load resources
uart = UART(2, 19200, bits=8, parity=0, stop=1, rx=16, tx=17, timeout=0)       # create UART object
while True:                         # loop continuously
    if uart.any():               # has there gone a minute/is there new data?
        incoming=uart.readline('\r\n') # read from the UART
        print(incoming)             # write status

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Help uart.read() with \r\n

Post by dhylands » Sun Apr 29, 2018 11:14 pm

readline breaks on '\n'

If you want a different behaviour then you should just code up a routine that reads the UART yourself and produces the behaviour you want.

User avatar
Bionaught
Posts: 5
Joined: Sun Apr 08, 2018 5:06 pm

Re: Help uart.read() with \r\n

Post by Bionaught » Mon Apr 30, 2018 2:32 am

I thought as much. Thanks for the quick response, confirming this. I guess the code I end up with is gonna be really long and messy before I'm any where even close to done reaching my goal. I'm glad this is just my hobby and not my job, with deadlines.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Help uart.read() with \r\n

Post by Roberthh » Mon Apr 30, 2018 6:48 am

If you get single lines with just '\r' in them, you might simply skip these. If the '\r' is appended to a line, you can strip that from the incoming line.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Help uart.read() with \r\n

Post by dhylands » Mon Apr 30, 2018 4:19 pm

Bionaught wrote:
Mon Apr 30, 2018 2:32 am
I thought as much. Thanks for the quick response, confirming this. I guess the code I end up with is gonna be really long and messy before I'm any where even close to done reaching my goal. I'm glad this is just my hobby and not my job, with deadlines.
As a professional software developer, and having worked with serial comms for several decades, I almost always design my handling of input to be one character at a time. This is really the only way to guarantee the exact behaviour you want for weird error cases (timeouts in the middle, really long lines, etc). In almost every piece of code I've seen that tries to deal with more than one character at a time there are cases where it will fail to deal with something when the input isn't perfect. And trust me, you will encounter noise in your input somewhere down the line.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Robust serial comms

Post by pythoncoder » Thu May 03, 2018 4:51 am

dhylands wrote:
Mon Apr 30, 2018 4:19 pm
...I almost always design my handling of input to be one character at a time. This is really the only way to guarantee the exact behaviour you want for weird error cases (timeouts in the middle, really long lines, etc)...
Hi Dave. This strikes me as a comment on firmware quality rather than something fundamental about serial comms. I'm well aware that there are numerous failure modes, especially if you have 100m of RS232 cable trailing across a factory floor to a cabinet which might be switched on or off at any time. However.

Say you have an application which needs to acquire and parse lines of data. You write code which reads the data from the UART character by character, checking for timeouts, counting for lines that are too long, handling framing errors and suchlike. When it gets to a newline it returns the data for your application to parse. On error it does something sensible like throw an exception.

So you've just written UART.readline(). It does rather strike me that if UART.readline() had been written properly in the first case this wouldn't be necessary.

Or am I missing something?
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Help uart.read() with \r\n

Post by dhylands » Thu May 03, 2018 7:05 pm

Using uart.readline() is fine if all of its behaviors are exactly what you want.

The problem is, that everybody seems to have a different idea of exactly what the "correct" behavior should be. If you code it yourself, then you always get exactly the behavior you coded.

Post Reply