问题描述
我们如何使用带有 python 脚本的树莓派进行串行通信以向我的笔记本电脑发送/接收数据?如果是,我们可以使用 RS232 电缆直接连接到树莓派上的 TX/RX 端口吗?如果是,RS232 电缆必须使用哪些引脚?如果有人可以发布示例 python 脚本,那会很有帮助吗?
How can we do serial communication using the raspberry pi with a python script to send/receive data to my laptop? If yes can we use the RS232 cable to connect to the TX/RX ports on the raspberry pi directly? If yes, what pins must be used from the RS232 cable? It would be help full if anyone can post a example python script?
由于我的第一个目标是向笔记本电脑发送/从树莓派接收数据,因此我在两端使用 RS232 转 USB 电缆连接到笔记本电脑和树莓派.
Since my first aim is to send/receive data to the laptop to/from the raspberry pi, i'm using a RS232 to USB cable at both end to connect to the laptop as well as the raspberry pi.
推荐答案
RS-232/RS-485转PC和USB转PC
对于 RS-232
1) 下载腻子.
2) 购买 串口 RS232 转 TTL 转换器模块和 PC 用 RS-232 串口线.
2) Buy a Serial Port RS232 to TTL Converter Module and a RS-232 serial cable for PC.
3) 按照连接到微控制器或其他外围设备中的步骤操作链接.实际阅读全文以更好地理解.
3) Follow the steps in Connection to a microcontroller or other peripheral on thislink. Actually read the whole thing for better understanding.
4) 使用 3.3V(引脚 1)或 5.0V(引脚 2)为转换器模块供电,将模块的 Rxd 引脚连接到 Rpi 上的 Rxd(引脚 8),将 Txd 引脚连接到 Rpi 上的 Txd(引脚 10).
4) Power your converter module with either 3.3V (pin 1) or 5.0V (pin 2), connect Rxd pin of the module to Rxd (pin 8) on Rpi and Txd pin to Txd (pin 10) on Rpi.
5) 将您的 RS-232(来自 PC)电缆连接到转换器模块
5) Connect your RS-232 (from PC) cable to the converter module
现在您已准备好使用 Python 进行一些编码.但在此之前,请确保您有一个名为 serial 的库,用于 Python 来创建通信.您可以通过终端输入 sudo apt-get install python-serial 轻松获取它.您还将看到代码中的波特率为 7200.它可能会更少或更多,具体取决于同步.还要确保 putty 和 COM1 中的波特率应该相同,COM1 是 RS-232 连接到 PC 的端口.您可以在 Windows 的设备管理器中检查和设置它.顺便说一下,timeout 是您收到的每条消息之间的时间间隔.
Now you are ready to do some coding in Python. But before that make sure that you have the library called serial for python to create the communication. You can easliy get it via terminal by typing sudo apt-get install python-serial. Also you will see the baudrate in the code is 7200. It could be less or more depending on the synchronization. Also make sure that baudrate should be same in putty and COM1, which is the port that RS-232 is connected to your PC. You can check and set it from device manager in Windows. By the way, timeout is the time gap between each message you are receiving.
如果您无法从空闲状态运行代码(某些库会发生这种情况),请在终端中运行.为此,请转到保存 Python 代码的文件夹并输入 python name.py.
In case you can't run the code from idle (that happens for some libraries), do it in terminal. For that go to the folder where you keep your python code and type python name.py.
import serial
import time
def readlineCR(port):
rv = ""
while True:
ch = port.read()
rv += ch
if ch == '\r' or ch == '':
return rv
port = serial.Serial("/dev/ttyAMA0", baudrate = 7200, timeout = 2)
while True:
rcv = readlıneCR(port)
port.write("I typed: " + repr(rcv))
print(rcv)
USB串口转PC
你有两个选择
首先,您可以购买用于 RS-232/RS-485 的 USB 加密狗这样您就不会使用 GPIO 引脚.但最好为所有鼠标、键盘和加密狗配备一个 USB 集线器.
First, you can buy a USB dongle for RS-232/RS-485 so that you would not use GPIO pins. But it is better to get a USB hub for all mouse, keyboard and dongle.
第二个更简单的是,您可以购买 FTDI USB 到 TTL 转换器 并使用 GPIO 与 Rpi 进行串行通信.此代码与上面的代码完全相同.连接很容易.
Second and easier, you can buy a FTDI USB to TTL converter and use GPIOs to have a serial communication with Rpi. The code for this is exactly the same the one with above. Connection for this is easy.
模块 -- Rpi
发送 --> 发送
接收 --> 接收
地线--->地线
这篇关于与树莓派的串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!