插件窝 干货文章 配置Linux系统以支持串口通信编程

配置Linux系统以支持串口通信编程

串口 tty serial 设备 1000    来源:    2025-04-17

Linux系统串口通信编程配置指南

1. 检查串口设备

首先确认系统识别到的串口设备:

ls /dev/ttyS*    # 查看传统串口(COM)
ls /dev/ttyUSB*  # 查看USB转串口设备
ls /dev/ttyACM*  # 查看某些USB设备(如Arduino)
dmesg | grep tty # 查看内核识别的串口设备信息

2. 设置串口权限

默认情况下普通用户可能没有串口访问权限:

# 临时解决方案 - 更改设备权限
sudo chmod 666 /dev/ttyS0

# 永久解决方案 - 将用户加入dialout组
sudo usermod -a -G dialout $USER
# 然后重新登录使更改生效

3. 安装必要工具

# 安装串口通信工具
sudo apt-get install minicom screen picocom putty

# 安装开发库(根据编程语言选择)
sudo apt-get install libserial-dev  # C++
sudo apt-get install python-serial  # Python

4. 配置串口参数

使用stty命令配置串口参数:

stty -F /dev/ttyS0 115200 cs8 -parenb -cstopb

常用参数说明: - 115200: 波特率 - cs8: 8位数据位 - -parenb: 无奇偶校验 - -cstopb: 1位停止位(使用cstopb表示2位)

5. 测试串口通信

使用minicom测试:

minicom -D /dev/ttyS0 -b 115200

使用screen测试:

screen /dev/ttyS0 115200

(退出screen: Ctrl+A, 然后按k, 再按y确认)

6. 编程接口

C语言示例:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main() {
    int serial_port = open("/dev/ttyS0", O_RDWR);

    struct termios tty;
    memset(&tty, 0, sizeof tty);

    if(tcgetattr(serial_port, &tty) != 0) {
        printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
        return 1;
    }

    cfsetospeed(&tty, B115200);
    cfsetispeed(&tty, B115200);

    tty.c_cflag &= ~PARENB; // 无奇偶校验
    tty.c_cflag &= ~CSTOPB; // 1位停止位
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;     // 8位数据位

    tty.c_cflag &= ~CRTSCTS; // 禁用硬件流控
    tty.c_cflag |= CREAD | CLOCAL; // 启用接收,忽略控制线

    tty.c_lflag &= ~ICANON; // 非规范模式
    tty.c_lflag &= ~ECHO;   // 禁用回显
    tty.c_lflag &= ~ECHOE;
    tty.c_lflag &= ~ECHONL;
    tty.c_lflag &= ~ISIG;   // 禁用信号字符

    tty.c_iflag &= ~(IXON | IXOFF | IXANY); // 禁用软件流控
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);

    tty.c_oflag &= ~OPOST; // 原始输出
    tty.c_oflag &= ~ONLCR;

    tty.c_cc[VMIN] = 1;   // 读取至少1个字符
    tty.c_cc[VTIME] = 10; // 等待时间(十分之一秒)

    if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
        printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
        return 1;
    }

    // 写入数据
    unsigned char msg[] = "Hello Serial!\n";
    write(serial_port, msg, sizeof(msg));

    // 读取数据
    char buf[256];
    int n = read(serial_port, &buf, sizeof(buf));

    close(serial_port);
    return 0;
}

Python示例:

import serial

ser = serial.Serial(
    port='/dev/ttyS0',
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)

ser.write(b'Hello Serial!\n')
response = ser.readline()
print(response.decode('utf-8'))

ser.close()

7. 常见问题解决

问题1: 无法打开串口设备

  • 检查设备是否存在 ls /dev/tty*
  • 检查权限 ls -l /dev/ttyS0
  • 确保没有其他程序占用该串口

问题2: 数据乱码

  • 确认两端波特率、数据位、停止位、校验位设置一致
  • 检查线缆连接是否正常
  • 尝试降低波特率测试

问题3: 无法接收数据

  • 检查连接线是否正确(TX-RX交叉)
  • 使用stty -F /dev/ttyS0查看当前设置
  • 确保对方设备确实在发送数据

8. 高级配置

永久设置串口参数:

创建/etc/rc.local文件添加:

stty -F /dev/ttyS0 115200 cs8 -parenb -cstopb

禁用串口控制台:

编辑/boot/cmdline.txt(树莓派)或/etc/default/grub(其他Linux):

console=tty1

然后运行sudo update-grub并重启。

通过以上配置,您的Linux系统应该能够支持串口通信编程了。根据您的具体应用需求,可以选择合适的编程语言和库进行开发。