3

I want to communicate between Jetson nano and arduino nano through serial.

I wrote a simple script to test functionality but I receive strange data

here is the arduino code

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  Serial.println("work fine");
  delay(10000);
}

and this is the python code run on jetson nano

import serial
import time

arduino = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)

while True:
  try:
     data = arduino.readline()
     if data:
        print(data)
        print('Hi Arduino')
  except:
     arduino.close

the output is

b'\x17\x0f\t\x0b\x01\x06\t\x0e\x15\r\n'
Hi Arduino

if tried different baud rates but nothing change.

when is use serial monitor on windows it works fine.

5
  • start by sending only one character every second
    – jsotola
    Mar 5, 2023 at 16:54
  • Wondering if this might be a UTF conversion problem.
    – st2000
    Mar 5, 2023 at 17:22
  • refer to ASCII tables ... data bits are being dropped ... working in hex makes it clear ... the arduino sends 77, 6F, 72, 6B, 20, 66, 69, 6E, 65, 0D, 0A and the Jetson receives 17, 0F, 09, 0B, 01, 06, 09, 0E, 15, 0D, 0A ... possibly a speed mismatch, or the data buffer is being swamped by incoming data
    – jsotola
    Mar 5, 2023 at 17:44
  • regarding the speed, is there parameter other than baud rate that can cause mismatch? I sent character "I" each 10 sec, but received b'\t\r\n' Mar 6, 2023 at 12:46
  • Asynchronous serial transmission has 3 additional parameters: number of data bits, usage of parity, number of stop bits. A common set is 8-N-1, meaning 8 data bits, no parity, 1 stop bit. You need to check these. A mismatch on the number of stop bits is harmless, as long as the sender uses more than the receiver. Jul 4, 2023 at 5:48

2 Answers 2

2

I also belive its to do with the following line,

 data = arduino.readline()

Try the following,

while True:


try:
     arduino.flushInput() // Clear buffer

     data = arduino.readline()

     // decode the bytes into str format, 'utf-8' arg should work
     data.decode('utf-8')

     // strip the \r\t in the str output
     data.strip()
     if data:
        print(data)
        print('Hi Arduino')
  except:
     arduino.close

See if this resolves your problem.

Note: I used an ESP32 and python script with the above-mentioned changes and it worked for me.

Also, as mentioned in the comment section some of the information might be missing in the output 'b'\x17\x0f\t\x0b\x01\x06\t\x0e\x15\r\n' this is not something reproducible for me. Just for reference my byte string output without the modifications was b'work fine\r\n' not 'b'\x17\x0f\t\x0b\x01\x06\t\x0e\x15\r\n'

  • double-check Arduino write speed,
  • double-check python serial connection baud rate,
  • include the py script correction

see if this helps resolve your problem.

0

This stackoverflow question/answer implies that .readline() needs to be converted to a string.

import serial
ser = serial.Serial("COM11", 9600)
while True:
     cc=str(ser.readline())
     print(cc[2:][:-5])

I agree w/one of the comments to that answer that it might be better written as:

import serial
ser = serial.Serial("COM11", 9600)
while True:
     cc=str(ser.readline())
     cc=cc.decode("utf-8")
     print(cc)
3
  • readline() returns numbers ... it's returning non-printable characters
    – jsotola
    Mar 5, 2023 at 17:39
  • ... returns "Bytes" then?
    – st2000
    Mar 5, 2023 at 17:40
  • there is no difference between text and bytes ... it's all bytes ... the difference is how you present those bytes to the user ... the python code is printing text because it escapes non-printable characters ... the escape character is the \
    – jsotola
    Mar 5, 2023 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.