ret*_*tep 5 python serial-port arduino raspberry-pi teensy
我正在尝试从我的 raspberry PI 通信到一个 teensy(一个 arduino,可以假装为那些没有经验的人的鼠标和键盘)。
我想接收有关 arduino 的信息,并根据该信息移动鼠标。
在arduino方面,我制作了这个测试脚本:
void setup() {
Serial1.begin(9600); // According to the Teensy Docs, this is the RX1 & TX1 on my board.
// Serial itself corrosponds to the micro-usb port
}
String msg = "";
void loop() {
if(Serial1.available() > 0) {
msg = "";
while(Serial1.available() > 0) {
char read = Serial1.read();
msg += read;
}
Serial1.write('X'); // Acknowledge with reply
}
Serial1.println(msg); // Output to console for debugging
// Should be a number 1-9
// TODO: further processing
}
Run Code Online (Sandbox Code Playgroud)
在树莓派上,我正在运行这个测试脚本:
import time
import serial
import random
ser = serial.Serial(
port='/dev/ttyS0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
while True:
n = random.randint(1,9)
print("Writing", n)
ser.write(n)
time.sleep(1)
feedback = ser.read()
print(feedback) // Expecting 'X'
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,我在串行控制台中看不到任何输出以及一条空消息 ( b'') (注意超时参数)
我已经启用了串行通信raspi-config并重新启动。当我列出设备 ( ls -l /dev/) 时,我可以看到:
lrwxrwxrwx 1 root root 5 Apr 28 20:21 serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 Apr 28 20:21 serial1 -> ttyAMA0
Run Code Online (Sandbox Code Playgroud)
作为额外的测试,我minicom -b 9600 -o -D /dev/ttyS0在 pi 上用 1 根线将 RX 连接到 TX,它成功地回显了。
我有代码问题,还是可能的硬件问题?也许因为它很小,所以需要一些不同的协议?看这里
我不知道为什么它不能正确通信。这是我的接线:

| 归档时间: |
|
| 查看次数: |
1456 次 |
| 最近记录: |