raspberry pi和teensy之间的串行通信(使用UART GPIO引脚)

问题描述 投票:2回答:1

我正试图从我的树莓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

}

在raspberry pi上,我正在运行这个测试脚本。

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'

当我运行这个脚本的时候,我在串行控制台中没有看到任何输出 以及一个空的信息(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

作为一个额外的测试,我跑了 minicom -b 9600 -o -D /dev/ttyS0 用1根线连接pi上的RX和TX,成功回传。

我是代码问题,还是可能是硬件问题?也许因为是teensy,所以需要一些不同的协议?请看 此处

我已经不知道为什么它不能正常通讯了。这是我的接线。

python arduino raspberry-pi serial-port teensy
1个回答
2
投票

你把Rx线连在一起,Tx线连在一起。 一个人传输什么,另一个人就需要接收什么。 你需要走Tx-Rx和Rx-Tx。

© www.soinside.com 2019 - 2024. All rights reserved.