UART.readline() doesn't match the documentation

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
JumpZero
Posts: 27
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

UART.readline() doesn't match the documentation

Post by JumpZero » Wed Feb 20, 2019 1:04 pm

Hello,

I'm playing with the ESP8266 uart and doing some communication tests with an arduino.
The hardware is ok the communication works fine (of course I use a 2k2 + 1k voltage divider for Arduino 5V to ESP 3.3V)
Correct me if I'm wrong but I notice that in the documentation here:
https://docs.micropython.org/en/latest/ ... T.readline
The statement:
Return value: the line read or None on timeout.
should read
Return value: the line read or None on timeout if no character available, or the characters available (maximum 16).
It's not a big deal, but you better know it.

Here after I try to explain the tests which bring me to that conclusion.
Here is the Arduino software:

Code: Select all

int index;

void setup() {
    Serial.begin(115200) ;
    index = 0 ;
}

void loop() {
  Serial.println("Hello! This message from arduino, let's test that!");
  Serial.print("index = ");
  Serial.println(index);
  index++;
  delay(5000);

}
So this code doesn't do much, it prints 2 lines ending with \r\n every 5 seconds
fisrt line is 50 characters, second line is just an index value incrementing every turn.


And here is the Micropython webrepl console:

Code: Select all

>>> import uos, machine
>>> uos.dupterm(None, 1)
>>> uart = machine.UART(0, baudrate=115200, bits=8, parity=None, stop=1, rxbuf=100, timeout=0, timeout_char=1)
>>> while True:
...     if uart.any():
...         print(uart.readline())
...
...
...
b"Hello! This message from arduino, let's test that!\r\n"
b'index = 83\r\n'
b'Hello! This message from arduino, le'
b'Hello! This mess'
b'age from arduino'
b", let's test tha"
b't!\r\n'
b'index = 88\r\n'
b'Hello! This mess'
b'age from arduino'
b", let's test tha"
b't!\r\n'
b'index = 89\r\n'
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt:
>>> uart = machine.UART(0, baudrate=115200, bits=8, parity=None, stop=1, rxbuf=100, timeout=5, timeout_char=1)
>>> while True:
...     if uart.any():
...         print(uart.readline())
...
...
...
b"Hello! This message from arduino, let's test that!\r\n"
b'index = 93\r\n'
b'Hello! This message from arduino, le'
b"Hello! This message from arduino, let's test that!\r\n"
b'index = 95\r\n'
b"Hello! This message from arduino, let's test that!\r\n"
b'index = 96\r\n'
b"Hello! This message from arduino, let's test that!\r\n"
b'index = 97\r\n'
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt:
>>>
In the first part after disabling the repl on uart0 with uos.dupterm(None, 1), I set the uart with timeout=0.
Then read the incoming lines. The first line is complete because already in the buffer (see index is 83 next will be 88) but all following lines are truncated.
uart.readline() returns as soon as called (because timeout is 0 I guess) with a line of 16 characters not ending with \n.

Then in second part (after a KeyboardInterrupt by Ctrl-C) I set the uart with timeout=5 and restart the same incoming lines reading.
And this time uart.readline() behaves as expected: it returns a line ending by \n.

So my conclusion is that when uart.readline() returns on timeout if some characters are available in the buffer it will return them (but 16 maximum).

I also did this test @9600 bauds with same result.

Also as I found mentioned here by Roberthh
viewtopic.php?f=16&t=5359&hilit=uos.dup ... 5&start=10
that uart.readline() is non blocking and again it's ok, but you better know it, because when like me if you come from Python pyserial (or C under Linux) the same readline function is blocking.

So may I sugest a documentation update? Of course if I'm not wrong with my conclusion.

And thanks to all Micropython developers, Micropython is awesome :-)
--
Jmp0

Post Reply