[英]Serial communication between raspberry pi and teensy (using UART / GPIO pins)
I am trying to communicate from my raspberry PI to a teensy (a arduino that can pretend to be a mouse and keyboard for those uninitiated).我正在尝试从我的覆盆子 PI 与一个青少年(一个 arduino 可以假装为外行的鼠标和键盘)进行交流。
I want to receive information on the arduino, and based on that information move the mouse.我想收到有关 arduino 的信息,并根据该信息移动鼠标。
On the arduino side, I have made this test script:在 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
}
On the raspberry pi, I am running this test script:在树莓派上,我正在运行这个测试脚本:
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'
When i run the script, I see no output in the serial console as well as an empty message ( b''
) (Note the timeout parameter)当我运行脚本时,我在串行控制台中看不到 output 以及一条空消息(
b''
)(注意超时参数)
I have already enabled serial communication with raspi-config
and restarted.我已经启用了与
raspi-config
串行通信并重新启动。 When I list devices ( ls -l /dev/
), I can see:当我列出设备(
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
As an additional test, I ran minicom -b 9600 -o -D /dev/ttyS0
with 1 wire connecting RX to TX on the pi, and it successfully echoed back.作为附加测试,我运行
minicom -b 9600 -o -D /dev/ttyS0
,用 1 根线将 RX 连接到 pi 上的 TX,并成功回显。
Do I have a code issue, or possible hardware issue?我是否有代码问题或可能的硬件问题? Maybe since it is a teensy some different protocol is required?
也许因为它很小,所以需要一些不同的协议? See here
看这里
I'm out of ideas as to why it isn't communicating correctly.我不知道为什么它不能正确通信。 Here is my wiring:
这是我的接线:
You have the Rx line connected together and the Tx lines connected together.您将 Rx 线连接在一起,并将 Tx 线连接在一起。 What one transmits the other needs to receive.
一个发送另一个需要接收的内容。 You need to go Tx-Rx and Rx-Tx.
您需要 go Tx-Rx 和 Rx-Tx。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.