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:
Code:
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 in MicroPython by Johnny_Wu New to the Community (0 points)
edited Mar 9 by TomCollins

Please log in or register to answer this question.

1 Answer

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().

Reference:
https://www.digi.com/resources/documentation/digidocs/90002219/default.htm#reference/r_uart_constants.htm%3FTocPath%3DAccess%2520the%2520primary%2520UART%7C_____1
answered Mar 9 by DigiFan23 Seasoned Professional (245 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.
...