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.
2 Answers 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.
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)
-
readline() returns numbers
... it's returning non-printable charactersjsotola– jsotola2023年03月05日 17:39:19 +00:00Commented Mar 5, 2023 at 17:39 -
... returns "Bytes" then?st2000– st20002023年03月05日 17:40:20 +00:00Commented 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– jsotola2023年03月05日 17:56:54 +00:00Commented Mar 5, 2023 at 17:56
77, 6F, 72, 6B, 20, 66, 69, 6E, 65, 0D, 0A
and the Jetson receives17, 0F, 09, 0B, 01, 06, 09, 0E, 15, 0D, 0A
... possibly a speed mismatch, or the data buffer is being swamped by incoming data