Question about the UART buffer and sys.stdin

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.

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|_____1

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.

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