[SOLVED] UART.readline() not using timeout

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

[SOLVED] UART.readline() not using timeout

Post by liudr » Tue Aug 14, 2018 5:59 pm

[EDIT] timeout in MicroPython machine.UART is in milliseconds instead of seconds in PC Python pyserial module.
I did some tests and realized that UART.readline() doesn't timeout. Instead, it immediately returns an empty byte string. I am puzzled at why we can specify timeout if this call doesn't utilize it. It would mean a lot of unnecessary modifications for my script and break away from its consistency with the PC version script.

The PC version of serial module, pyserial, does honor the timeout and waits until timeout to return the empty byte string.

Is there anything I can do to make UART.readline() honor the timeout parameter? Thanks.

System: ESP32 Lobo firmware (I really don't think this is due to the unofficial firmware).
Last edited by liudr on Thu Aug 16, 2018 4:43 pm, edited 1 time in total.

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: UART.readline() not using timeout

Post by loboris » Tue Aug 14, 2018 7:02 pm

Please, read the UART Wiki.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: UART.readline() not using timeout

Post by liudr » Tue Aug 14, 2018 7:45 pm

Loboris,

Thanks for your quick response! The wiki indicates that the function will wait until timeout but my experience has been "it doesn't".

The real reason was that timeout was in milliseconds in MicroPython but in seconds in PC pyserial. Neither explicitly mentions on the constructor what time units timeout is in. Units are so important, as important as values! As a physicist, I protest! Use timeout_ms or timeout_s in the future! Billion dollar project ended up in a pile of junk when units were wrong:

https://www.wired.com/2010/11/1110mars- ... er-report/

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

Which docs are you reading?

Post by pythoncoder » Wed Aug 15, 2018 9:07 am

From the official docs: "timeout is the timeout in milliseconds to wait for writing/reading the first character."
Peter Hinch
Index to my micropython libraries.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: UART.readline() not using timeout

Post by kevinkk525 » Wed Aug 15, 2018 9:38 am

As he's using loboris port, I'm not sure the official docs apply to this.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: UART.readline() not using timeout

Post by loboris » Wed Aug 15, 2018 10:40 am

From Wiki.
If the line end character is not found in the input buffer and uart timeout is set to a value greater than 0, waits until line end character is received or timeout ms expires.
I have also updated the uart.init() timeout argument description.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: UART.readline() not using timeout

Post by liudr » Wed Aug 15, 2018 11:14 am

kevinkk525 wrote:
Wed Aug 15, 2018 9:38 am
As he's using loboris port, I'm not sure the official docs apply to this.
There is only esp8266 official doc:

http://docs.micropython.org/en/latest/e ... .UART.html

Units are not mentioned.

Here is wipy port doc. Same thing:

http://docs.micropython.org/en/latest/w ... .UART.html

Loboris firmware has a wiki with doc that he pointed out.

My point is to name a variable with units. In my 30+ years of experience in programming, including units has not been mentioned as important in any textbooks that I have read. Am I the only one who saw this as a grave issue? This simple fix could prevent many bad things from happening. It is as bad as using magic numbers to not include units when necessary.

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

Re: UART.readline() not using timeout

Post by pythoncoder » Thu Aug 16, 2018 8:26 am

If you spot an error in the documentation why not submit a GitHub pull request to fix it?
Peter Hinch
Index to my micropython libraries.

User avatar
liudr
Posts: 211
Joined: Tue Oct 17, 2017 5:18 am

Re: UART.readline() not using timeout

Post by liudr » Thu Aug 16, 2018 3:53 pm

pythoncoder wrote:
Thu Aug 16, 2018 8:26 am
If you spot an error in the documentation why not submit a GitHub pull request to fix it?
Peter,

Thanks for the suggestion. I opened an issue on github. Another potential place to improve is to define some alias for time.sleep() as time.sleep_s() since there are already _ms and _us counterparts.

Post Reply