I use UART 1(GPS Uart) to communicate with RS485 Modbus. It works fine on REPL(Run the code line by line)
But in main.py, It cannot read data properly(some bytes are missing and some are corrupt except the first 2 bytes) even though I put sleep before reading or use while-loop. uart.any() returns 1. Buffer should not be an issue because the data to read is actually only 9 bytes. Try uasyncio also not help.
In my testing, if I use time.sleep or while loop after writing, the received data will have problem, that's why when I run write and then read in REPL, the received data is fine
I change the baudrate to 2400, the result is correct when the sensor value is less than 80%(humidity) but after that the data is corrupt again. I guess the transmission interval of the sensor change when the sensor value is high that's why it's corrupt again.
here's my init code:
Code: Select all
uart=UART(1,4800) uart.init(4800, bits=8, parity=None, stop=1)
uart.write(b'\x01\x03\x02')
time.sleep_ms(sleep)
while uart.any()==0:
time.sleep_ms(10)
res=uart.read()