Search code examples
arduinopyserialnvidia-jetson-nano

Jetson nano pyserial write message to arduino get wrong chars


I wrote a python script using pyserial to connect NVIDIA Jetson Nano through serial communication with Arduino Uno using Jetson nano J41 pins and Software Serial on Arduino Uno but I'm having an issue with the messages received on arduino uno, sometimes I got bad characters on the message. e.g. I send with pyserial "hello world" and when I check arduino serial I got "he⸮lo"worlf*"

Also when the arduino receive a message it answers with a MESSAGE_OK and the jetson nano always get it right without weird characters. From jetson to uno bad characters are received but from from nano to jetson it's ok. I'm using a logic level converter to connect arduino software serial pins to jetson nano uart pins.

I've been trying to figure out was going on but without success, if someone can help me with suggestions, or the answer that would be great.

I'm trying with the easiest example, here is my code for arduino and for jetson nano:

Arduino:

#include <SoftwareSerial.h>

String a;
// Arduino uno Ext Serial pins
int ext_rx_pin = 9;
int ext_tx_pin = 8;
SoftwareSerial ext(ext_rx_pin, ext_tx_pin); //RX, TX 

void setup() {
  // opens serial port
  Serial.begin(38400); 
  // Setup external serial connection to jetson
  ext.begin(38400);
}

void loop() {
  while (ext.available() > 0) {
    a = ext.readStringUntil('\n'); // read the incoming data as string
    // Print message on ide console
    Serial.println(a);
    // Answer to jetson
    ext.print("MESSAGE_OK");
    ext.print("\n");
  }
}

Jetson nano:

#!/usr/bin/python3
import time
import serial

serial_port = serial.Serial(
    port="/dev/ttyTHS1",
    baudrate=38400,
    timeout=0.5
)

# Wait a second to let the port initialize
time.sleep(1)

arduino_message = ""
wait = True

try:
    while True:
        text = input("Input message: ")
        print("Sending:", text)
        text = text + "\n"
        print(text.encode())
        for i in text:
            serial_port.write(i.encode('utf-8'))
            time.sleep(0.1)
        wait = True
        while wait:
            if serial_port.inWaiting() > 0:
                data = serial_port.read()
                arduino_message = arduino_message + data.decode('utf-8')
                if data == "\n".encode():
                    wait = False
                    print(arduino_message)
                    arduino_message = ""

except KeyboardInterrupt:
    print("Exiting Program")

except Exception as exception_error:
    print("Error occurred. Exiting Program")
    print("Error: " + str(exception_error))

finally:
    serial_port.close()
    pass

Also if I try to echo what is send from jetson to uno and then from uno to jetson I got this message because the bad characters: Error: 'utf-8' codec can't decode byte 0xec in position 0: unexpected end of data


Solution

  • There are a number of single and double bit errors in the expected and received characters. This could be an electrical and/or timing issue.

    e.g.

    space (0010 0000) => " (0010 0010)

    d (0010 0010) => f (0110 0110)

    \n (0000 1011 and 0000 1010) => * (one of which ends up being 0010 1010)

    Try using a slower baud rate between the boards as timing is especially an issue with software serial.

    Alternatively, use a hardware serial port on the Arduino Uno to communicate with the other board.