The upper computer and Raspberry Pi are connected by USB to TTL module, and serial communication is adopted

Using the USB to TTL module, the industrial computer of the Linux system is connected to the USB socket, and the GPIO port of the Raspberry Pi is connected to the TTL serial port. How to write a two-way communication program?

USB to TTL-CH340 module
insert image description here

ChatGPT

The following is an example showing the basic framework of how to use the USB to TTL module to perform two-way serial communication between the industrial computer of the Linux system and the Raspberry Pi.

Python code on the industrial computer (sending program):

import serial

# 配置串口参数
port = '/dev/ttyUSB0'  # USB转TTL模块所对应的串口设备路径
baudrate = 9600  # 波特率

# 打开串口
ser = serial.Serial(port, baudrate)

# 发送数据
data = b'Hello, World!'  # 待发送的数据
ser.write(data)

# 接收数据
response = ser.readline()
print('Received:', response)

# 关闭串口
ser.close()

Python code on Raspberry Pi (receiving program):

import serial
import RPi.GPIO as GPIO

# 配置串口参数
port = '/dev/serial0'  # 树莓派上UART接口所对应的串口设备路径
baudrate = 9600  # 波特率

# 打开串口
ser = serial.Serial(port, baudrate)

# 配置GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.OUT)  # 示例中使用GPIO 14作为LED指示灯

# 接收数据
while True:
    if ser.in_waiting > 0:  # 检查串口缓冲区是否有数据
        data = ser.read(ser.in_waiting)  # 读取数据
        print('Received:', data)

        # 发送响应
        response = b'Received: ' + data
        ser.write(response)

        # 控制LED指示灯
        GPIO.output(14, GPIO.HIGH)  # 点亮LED

# 关闭串口
ser.close()

portMake sure to modify and according to the actual situation before running the above code baudrate, which respectively represent the serial device path and baud rate of the USB to TTL module and the UART interface on the Raspberry Pi.

In the sending program on the industrial computer, we use to ser.write(data)send data to the Raspberry Pi, and use to ser.readline()read the response data from the Raspberry Pi.

In the receiving program on the Raspberry Pi, we use to ser.read()read the data in the serial port receiving buffer, and use to ser.write(response)send response data to the industrial computer.

Ensure that the serial port parameters and communication protocol settings are consistent when performing serial port communication between the industrial computer and the Raspberry Pi, and make appropriate adjustments according to specific hardware and communication requirements.

Please note that the above code is just an example, and the specific implementation may need to be adjusted and extended according to your specific needs.

Does the Raspberry Pi have a serial port?

ChatGPT

Yes, the Raspberry Pi has a serial interface. The GPIO pins of the Raspberry Pi include multiple pins that can be configured as serial port functions, such as UART (Universal Asynchronous Receiver Transmission) interface. These pins can be used for serial communication with external devices, such as RS-232, RS-485, etc.

Different models of Raspberry Pi have different pin layouts and capabilities, but generally all provide at least one usable UART serial port. In the GPIO pin diagram of the Raspberry Pi, the UART interface is usually marked with TX (transmit) and RX (receive) pins, as well as other pins related to serial communication (such as CTS, RTS, etc.).

How to wire the Raspberry Pi 4B GPIO port as a TTL serial port

ChatGPT

To use the GPIO port of the Raspberry Pi as a TTL serial port, you can wire it as follows:

  1. Determine which GPIO pins to use: The GPIO interface of the Raspberry Pi has multiple pins available, and you need to choose a pair of GPIO pins as the transmit (TX) and receive (RX) pins of the TTL serial port. Commonly used pins include GPIO14 (TXD0) and GPIO15 (RXD0), which correspond to the GPIO physical numbers of Raspberry Pi as 8 and 10.
  2. Connect the transmit (TX) pin: connect the transmit pin (TX) of the TTL serial device to the GPIO pin of the Raspberry Pi, for example, connect the TX pin of the TTL serial port to the GPIO14 pin of the Raspberry Pi.
  3. Connect the receiving (RX) pin: connect the receiving pin (RX) of the TTL serial device to the GPIO pin of the Raspberry Pi, for example, connect the RX pin of the TTL serial port to the GPIO15 pin of the Raspberry Pi.
  4. Connect the ground (Ground) pin: In order to ensure the same level, connect the ground (Ground) pin of the TTL serial device to the ground (Ground) pin of the Raspberry Pi. Typically, the ground pin is the black wire or the pin labeled "GND".

Please note that the connection method mentioned here is suitable for using the GPIO port of the Raspberry Pi as a simple TTL serial port.
Please add a picture description

Guess you like

Origin blog.csdn.net/NICAI001/article/details/131615087