我使用 pyserial 编写了一个 python 脚本,通过使用 Jetson nano J41 引脚和 Arduino Uno 上的软件串行与 Arduino Uno 进行串行通信来连接 NVIDIA Jetson Nano,但我在 arduino uno 上收到的消息有问题,有时我遇到了错误的字符消息。例如,我使用 pyserial “hello world”发送,当我检查 arduino 序列时,我得到“he⸮lo”worlf*”
此外,当 arduino 收到一条消息时,它会用 MESSAGE_OK 来回答,而 jetson nano 总是能正确处理,没有奇怪的字符。从 jetson 到 uno 收到坏字符,但从 nano 到 jetson 没关系。我正在使用逻辑电平转换器将 arduino 软件串行引脚连接到 jetson nano uart 引脚。
我一直试图弄清楚正在发生但没有成功,如果有人可以帮助我提出建议,或者答案会很好。
我正在尝试最简单的示例,这是我的 arduino 和 jetson nano 代码:
阿杜诺:
#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");
}
}
杰森纳米:
#!/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
此外,如果我尝试回显从 jetson 发送到 uno 然后从 uno 发送到 jetson 的内容,我会收到此消息,因为错误字符:错误:'utf-8' codec can't decode byte 0xec in position 0: unexpected end of data