【linux】一种基于虚拟串口的方式使两个应用通讯

在Linux系统中,两个应用之间通过串口(Serial Port)进行通信是一种常见的通信方式,特别是在嵌入式系统、工业自动化等领域。串口通信通常涉及到对串口设备的配置和读写操作。以下是一个基本的步骤指南,说明如何在Linux中设置两个应用以通过串口进行通信:

1. 确认串口设备

首先,你需要确认你的Linux系统上有哪些串口设备。通常,串口设备在/dev目录下,如/dev/ttyS0/dev/ttyUSB0等。你可以使用dmesg命令查看系统启动时串口设备的识别信息,或者使用ls /dev/tty*来列出所有tty设备。

2. 配置串口参数

串口通信需要配置一些参数,如波特率(Baud Rate)、数据位(Data Bits)、停止位(Stop Bits)、校验位(Parity)等。在Linux中,你可以使用stty命令来配置这些参数。例如,要将/dev/ttyS0配置为9600波特率,8数据位,1停止位,无奇偶校验,可以使用以下命令:

3. 编写通信程序

接下来,你需要编写两个程序,一个作为发送方,另一个作为接收方。这些程序可以使用Linux系统调用(如open(), read(), write(), close())来操作串口设备。

示例代码(C语言)

发送方(sender.c)

cpp 复制代码
#include <stdio.h> 
#include <stdlib.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <string.h> 


int main() { 
int fd = open("/dev/ttyS0", O_WRONLY); 
if (fd < 0) { 
perror("Error opening serial port"); 
return -1; 
} 


const char *msg = "Hello, Serial Port!"; 
write(fd, msg, strlen(msg)); 


close(fd); 
return 0; 
}

接收方(receiver.c)

cpp 复制代码
#include <stdio.h> 
#include <stdlib.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <termios.h> 


#define BUFFER_SIZE 1024 


int main() { 
int fd = open("/dev/ttyS0", O_RDONLY | O_NOCTTY | O_NDELAY); 
if (fd < 0) { 
perror("Error opening serial port"); 
return -1; 
} 


// 清除非阻塞标志 
fcntl(fd, F_SETFL, 0); 


char buffer[BUFFER_SIZE]; 
int num_bytes = read(fd, buffer, BUFFER_SIZE); 
if (num_bytes < 0) { 
perror("Error reading from serial port"); 
return -1; 
} 


buffer[num_bytes] = '\0'; 
printf("Received: %s\n", buffer); 


close(fd); 
return 0; 
}

4. 编译和运行程序

使用gcc编译你的C程序,并运行它们。确保发送方在接收方之前运行,或者确保接收方能够处理串口数据的到达。

5. 调试

如果通信没有按预期工作,检查以下几点:

  • 串口设备是否正确配置。
  • 串口参数(如波特率)在两个程序中是否一致。
  • 是否有其他程序正在使用同一个串口。
  • 使用dmesgtail -f /var/log/syslog(取决于你的系统)来查看系统日志,了解是否有错误信息。

通过以上步骤,你应该能够在Linux系统中设置两个应用通过串口进行通信。

另附一段串口测试源码:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int main() {
    int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        perror("open_port: Unable to open /dev/ttyS0 - ");
        return(-1);
    }

    struct termios options;
    tcgetattr(fd, &options);

    // 设置波特率
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);

    // 设置数据位数
    options.c_cflag &= ~CSIZE; // Mask the character size bits
    options.c_cflag |= CS8;

    // 设置为无奇偶校验位
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;

    // 设置为一个字符一个停止位
    options.c_cflag &= ~CRTSCTS;

    // 应用设置
    tcsetattr(fd, TCSANOW, &options);

    // 写数据
    char *write_buffer = "Hello World";
    write(fd, write_buffer, sizeof(write_buffer));

    // 读数据
    char read_buffer[100];
    read(fd, read_buffer, sizeof(read_buffer));
    printf("Received: %s\n", read_buffer);

    close(fd);
    return 0;
}
相关推荐
kblj555519 分钟前
学习Linux——网络基础管理
linux·网络·学习
前端世界20 分钟前
用Python打造智能成绩分析系统:从异常处理到断言验证的全流程实战
服务器·数据库·python
小王C语言42 分钟前
Linux基础开发工具----yum、vim和gcc/g++
linux·运维·服务器
newxtc1 小时前
【辽宁政务服务网-注册_登录安全分析报告】
运维·selenium·安全·政务·安全爆破
_w_z_j_1 小时前
Linux----文件系统
linux·运维·服务器
测试-鹏哥1 小时前
要将ITP集成到Jenkins Pipeline中,实现开发发版时自动触发自动化测试
运维·python·测试工具·ci/cd·jenkins
SKYDROID云卓小助手2 小时前
无人设备遥控器之数字图传技术
运维·服务器·单片机·嵌入式硬件·fpga开发
努力努力再努力wz2 小时前
【Linux进阶系列】:线程(上)
java·linux·运维·服务器·数据结构·c++·redis
java 乐山2 小时前
蓝牙网关(备份)
linux·网络·算法
2301_803554522 小时前
面试后查缺补漏--cmake,makefiles,g++,gcc(自写精华版)
linux·运维·服务器