Home/Support/Support Forum/Question about the UART buffer and sys.stdin

Question about the UART buffer and sys.stdin

0 votes
Hi there,
I'm currently doing a project and trying to read some data from a MCU. The XBee I'm using is XBee3 Cellular LTE-M/NB-IoT.
The general process should be as follow:
1. xbee send 0x05 to MCU to wake up the MCU, and MCU will send 0x06 back.
2. xbee send a command to request some data from MCU and MCU will send a 9-byte data back.

this is the code I'm testing:
send_com(enq) time.sleep_ms(200) ack =sys.stdin.read() if ack!= None: sys.stdin.close() send_com(L_E) time.sleep_ms(90) link = sys.stdin.read() if link!= None: sys.stdin.close()

send_com() is used to send a bytearray to MCU.
The first read is fine and I got ack = '\x06' as it supposed to be.
The second read should give me '\x01 0 0 \x03 F 0 5 3 \x04' but I got link = 'F 0 5 3 \x04' instead.
When I reduce the sleep time after 'send_com(L_E)', I got link = '\x01 0 0'.
I think I probably have some misunderstanding about the buffer. Can someone tell me where I did wrong? Or any helpful documentation recommend?

I appreciate for any answer.
asked Mar 6, 2020 in MicroPython by Johnny_Wu New to the Community (0 points)
edited Mar 9, 2020 by TomCollins

Please log in or register to answer this question.

2 Answers

0 votes
I would recommend using sys.stdin.buffer.read() and sys.stdout.buffer.write(), otherwise there are line ending conversions.
Also, I am note sure it is necessary to call sys.stdin.close().

answered Mar 9, 2020 by DigiFan23 Veteran of the Digi Community (278 points)
Thanks for answer!
Actually I have tried sys.stdin.buffer.read() and sys.stdout.buffer.write(),but it seems it's not working. There is a message says "unresolved attribute reference 'buffer' for class 'FileIO' " and I still trying to figure out why this message shows up.
And you are right, sys.stdin.close() doesn't do anything in my case.
0 votes
When i use .read() it returns right away (with None if there is no byte waiting), when I use .read(1) it blocks until it reads a single byte.
I think it might block forever though, which might not work for you
answered Jul 16, 2021 by plater Community Contributor (64 points)