0

Using RS232 I am looking to type a string into a terminal window and for the string to be echoed back to the terminal using micropython. However with the code shown below I get an error with the bytes being unable to convert to a string. Any help would be appreciated. Thanks

from machine import UART, Pin, I2C
import utime

uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
uart.init(bits=8, parity=None, stop=2)
SDA = Pin(16)
SCL = Pin(17)


uart.write("Script Begin\n")


while True:
    data = uart.read()

    if data is not None:
        # display input
        print('\nInput:')
        print(data)
        print(type(data))
         
        # converting
        output = data.decode('UTF-8')
         
        # display output
        print('\nOutput:')
        print(output)
        print(type(output))

    utime.sleep(3)

I have tried using decode(), str() but continue to get error:

Traceback (most recent call last):
  File "<stdin>", line 42, in <module>
UnicodeError: 
  • 1
    Can you please elaborate on the error message? The provided code does not have 42 lines, so we don't know which statement triggers the error. And what comes behind "UnicodeError: " is the most interesting information. – Peter I. Aug 11 '23 at 15:22

0 Answers0