【问题标题】:Serial communication between raspberry pi and teensy (using UART / GPIO pins)raspberry pi 和 teensy 之间的串行通信(使用 UART / GPIO 引脚)
【发布时间】:2020-04-29 02:07:12
【问题描述】:

我正在尝试从我的树莓派 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

}

在树莓派上,我正在运行这个测试脚本:

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

作为附加测试,我使用 1 根线将 RX 连接到 pi 上的 TX 运行 minicom -b 9600 -o -D /dev/ttyS0,并成功回显。

我是否有代码问题或可能的硬件问题?也许因为它很小,所以需要一些不同的协议?见here

我不知道为什么它不能正确通信。这是我的接线:

【问题讨论】:

  • 你能用 arduino IDE 串行终端和小孩子说话吗?或minicom ...大多数时候我看到这个问题是arduino在打开端口后有一些小的串行启动时间......在python打开arduino的串行端口后尝试休眠几秒钟,然后再尝试发送任何数据
  • 是我看图不对还是你有Rx-Rx和Tx-Tx?
  • @Delta_G 不是应该排队吗?
  • 当你和某人说话时,你会在他们的嘴里说话并听他们的耳朵吗?一方传输另一方需要接收的内容。

标签: python arduino raspberry-pi serial-port teensy


【解决方案1】:

Rx 线连接在一起,Tx 线连接在一起。一个发送另一个需要接收的内容。你需要去 Tx-Rx 和 Rx-Tx。

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 2018-12-18
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多