简体   繁体   English

使用 UART 在 2 个 Raspberry Pi 之间进行通信

[英]Communicate between 2 Raspberry Pi with UART

I want to send data from one pi to another with UART communication.我想通过 UART 通信将数据从一个 pi 发送到另一个。 The first Raspberry model is Raspberry Pi 4, and the second one Raspberry Pi 3. To do this communication Im connecting both Raspberry pins in this way:第一个 Raspberry model 是 Raspberry Pi 4,第二个是 Raspberry Pi 3。要进行此通信,我以这种方式连接两个 Raspberry 引脚:

Pi 4 -> Pi 3

Tx -> Rx

Rx -> Tx

Ground -> Ground

I have already activated both Pis serial connection on the raspberry configuration following the steps of this link: https://iot4beginners.com/raspberry-pi-configuration-settings/ .我已经按照此链接的步骤激活了树莓配置上的两个 Pis 串行连接: https://iot4beginners.com/raspberry-pi-configuration-settings/ In order to write and send the data I have created the next python program:为了写入和发送数据,我创建了下一个 python 程序:

import time
import serial

ser = serial.Serial(
        port='/dev/ttyS0', #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.SEVENBITS)
counter=0
while True: 
    ser.write(str.encode(str(counter))) 
    time.sleep(1) 
    counter += 1

To read and print the data received I have created the next program:为了读取和打印收到的数据,我创建了下一个程序:

import time
import serial

ser = serial.Serial(
        port='/dev/ttySO', #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0
        baudrate = 9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.SEVENBITS
)
counter=0

while 1:
    x = ser.readline()
    print(x)

Finally when I run the reading program I get the next error:最后,当我运行阅读程序时,我得到下一个错误:

Traceback (most recent call last):
  File "serial_read.py", line 14, in <module>
    x = ser.readline()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 501, in read
    'device reports readiness to read but returned no data '
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

I'm new to Raspberry communication and I will be thankful for any suggestion.我是 Raspberry 通信的新手,我将不胜感激任何建议。

Python 2.7 is officially dead, so I would recommend using Python3 - especially for new projects such as yours. Python 2.7 正式死亡,所以我建议使用 Python3 - 特别是对于像你这样的新项目。

So, rather than run your script with:因此,不要使用以下命令运行您的脚本:

python3 YourScript.py

you could put a proper shebang at the start so it automatically uses the correct Python version you intended:您可以在开始时放置一个适当的 shebang,以便它自动使用您想要的正确 Python 版本:

#!/usr/bin/env python3

import ...

Then make the script executable with:然后使脚本可执行:

chmod +x YourScript.py

Now you can run it with:现在您可以使用以下命令运行它:

./YourScript.py

and be happy it will use Python3, and also even if someone else uses it.并且很高兴它将使用 Python3,即使其他人使用它也是如此。


The actual issue, I think, is that the readline() on the receiving end is looking for a newline that you didn't send.我认为,实际问题是接收端的readline()正在寻找您未发送的换行符。 So I would try adding one:所以我会尝试添加一个:

ser.write(str.encode(str(counter) + '\n')) 

Personally, I find f-strings to be much easier than the older % and .format() stuff, so maybe try:就个人而言,我发现f-strings比旧的%.format()东西容易得多,所以也许试试:

ser.write(str.encode(f'{counter}\n'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM