Page 1 of 1

UART write int array

Posted: Thu Jan 27, 2022 10:21 am
by abreyu
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?

Re: UART write int array

Posted: Thu Jan 27, 2022 1:13 pm
by Roberthh
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.