UART write int array

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
abreyu
Posts: 1
Joined: Thu Jan 27, 2022 10:15 am

UART write int array

Post by abreyu » Thu Jan 27, 2022 10:21 am

hi

what is the best way to write an int array to uart? I've never used python, maybe my conversion from int[] to string is wrong:

Code: Select all

array = [42, 86, 127]
buf = ''

for i in array:
    buf += chr(i)

uart.write(buf)
[edit]

ok, i solved it like this:

Code: Select all

array = [42, 86, 127]
byte_array = bytearray(array)
uart.write(byte_array) 
now I'm working on read data:

Code: Select all

sleep_ms(100)

rxData = bytes()
while uart.any() > 0:
    rxData += uart.read(1)

print(rxData)
whithout sleep_ms(n) I cant read data, no better way to achive the result?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART write int array

Post by Roberthh » Thu Jan 27, 2022 1:13 pm

Obviously your code has to wait for response. Instead of waiting for a fixed time, you can poll UART using uart.any(), like:

Code: Select all

while uart.any() == 0:
    time.sleep_ms(1)
If you count the loop iterations, you can implement a timeout.

Post Reply