linux实现串口读取c程序,C++实现Linux下串口的读写

作为一个嵌入式程序员,串口的读写已经见怪不怪了。嵌入式Linux的串口也应用广泛。百度一搜也有许多讲解这方面的知识,今天我只展示我Linux下用C++实现的一个串口类可以实现类似于单片机中断的机制。使用起来很方便

我只展示接口:

/*

* File: BSerialPort.h

*/

#ifndef BSERIALPORT_H

#define BSERIALPORT_H

class BSerialPort {

public:

friend class BSerialPortManger;

enum SerialType{

Actual, Virtual,

};

BSerialPort();

BSerialPort(const BSerialPort& orig);

BSerialPort(char *filename, SerialType type = Actual);

BSerialPort(const char *filename, SerialType type = Actual);

virtual ~BSerialPort();

/*

* 参数: 串口设备文件路径

* 功能:打开串口,默认设置串口设备参数:9600,N,1

* 返回值:成功 true ; 失败 false

*/

bool open(char *filename);

/*

* 参数: 串口波特率

* 功能: 设置串口模特率,可以是下面的一个值

* 921600, 460800, 230400, 115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200, 300

* 返回值: 成功 true ; 失败 false

*/

bool set_Speed(int speed);

/*

* 参数: databits:数据位数

* stopbits:停止位

* parity: 校验位

* 功能: 设置串口的数据位、停止位、校验位。

* 返回值: 成功 true ; 失败 false

*/

bool set_Parity(int databits, int stopbits, char parity);

/*

* 参数:dest:数据读取缓存数组

* bytes:要读取数据的字节数

* 功能:从串口读取数据存储在dest中

* 返回值:实际读取数据字节数,出错返回-1。

*/

int read(char *dest, int bytes);

/*

* 参数:buff:要写入的数据

* bytes:要写入数据的字节数

* 功能:将buff中的数据写入串口

* 返回值:实际写入数据字节数,出错返回-1。

*/

int write(char *buff, int bytes);

/*关闭串口*/

void close(void);

bool operator==(BSerialPort &serial);

private:

/*串口文件描述符*/

int fd;

/*如果是虚拟串口,从设备的文件描述符*/

int sfd;

void (*function)(BSerialPort *serial);

};

#endif/* BSERIALPORT_H */

/*

* File: BSerialPortManger.h

*/

#ifndef BSERIALPORTMANGER_H

#define BSERIALPORTMANGER_H

#include "BSerialPort.h"

#include

#include

#include

#include

#include

using namespace std;

class BSerialPortManger {

public:

BSerialPortManger();

BSerialPortManger(const BSerialPortManger& orig);

virtual ~BSerialPortManger();

/*向串口管理类中添加一个待监控的串口*/

void add(BSerialPort *serial, void (*func)(BSerialPort *serial));

/*从管理类中移除一个串口*/

void remove(BSerialPort *serial);

/*每调用一次,检查一次是否有串口数据到达*/

void read_monitor();

private:

vector list;

int fd_max;

fd_set fd_sets;

fd_set handle_sets;

};

#endif/* BSERIALPORTMANGER_H */

上面我定义了两个类,一个是串口类一个是串口管理类。BSerialPortManger类是BSerialPort的友元类,这样利用BSerialPortManger就能管理所以注册到该类的串口对象,如果有可读数据就调用对应对象的回调函数。

下面展示BSerialPortManger::add的实现

void BSerialPortManger::add(BSerialPort* serial, void (*func)(BSerialPort *serial)){

serial->function = func;

if(serial->fd > 0){

FD_SET(serial->fd, &this->handle_sets);

this->list.push_back(serial);

this->fd_max = 0;

cout << "list_count:" << this->list.size()<

for(int i = 0; i < this->list.size(); i++){

if(this->list.at(i)->fd > this->fd_max)

this->fd_max = this->list.at(i)->fd;

}

}

}

监控函数的实现是利用select函数来监控所以注册的串口对象是否有数据要读取,如果有则调用相应的回调函数。

void BSerialPortManger::read_monitor(){

struct timeval timeout;

timeout.tv_sec = 0;

timeout.tv_usec = 100000;

this->fd_sets = this->handle_sets;

int rs = select(this->fd_max+1, &this->fd_sets, NULL, NULL, &timeout);

if(rs > 0){

for(int i = 0; i < this->list.size(); i++){

if(FD_ISSET(this->list.at(i)->fd, &this->fd_sets) && this->list.at(i)->function != NULL){

this->list.at(i)->function(this->list.at(i));

}

}

}

}

由于代码量较多,这里就不全部列出了。在类的实现中用到了回调函数和函数指针的知识,不懂的读者可以参考https://blog.csdn.net/BLUCEJIE/article/details/103538410。

串口类以及串口管理类的实现源码请在我博客的资源文件中下载http://download.csdn.net/download/BLUCEJIE/12063038

在上面的类实现中还实现了虚拟串口的功能,这样的虚拟串口是用Linux的为终端实现的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux下,可以使用C++中的文件操作和多线程技术实现高并发串口。具体实现步骤如下: 1. 打开串口设备文件 使用C++中的文件操作函数`open()`打开串口设备文件,获取文件描述符。例如: ```c++ int fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY); if (fd < 0) { perror("open serial port error!"); return -1; } ``` 其中,`/dev/ttyS0`是串口设备文件路径,`O_RDWR`表示打开文件可,`O_NOCTTY`表示不把串口设备作为控制终端,`O_NDELAY`表示非阻塞模式打开。如果打开失败,使用`perror()`函数输出错误信息并返回。 2. 配置串口属性 使用Linux系统调用函数`tty_ioctl()`配置串口属性,包括波特率、数据位、停止位和校验位等。例如: ```c++ struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cc[VTIME] = 0; options.c_cc[VMIN] = 1; tcsetattr(fd, TCSANOW, &options); ``` 其中,`cfsetispeed()`和`cfsetospeed()`函数设置输入输出波特率,`c_cflag`成员设置串口属性,`VTIME`和`VMIN`成员配置读取等待时间和最小字符数。如果配置失败,使用`perror()`函数输出错误信息并返回。 3. 创建线程 使用C++中的多线程技术创建线程,分别实现串口数据的读取入。例如: ```c++ void* read_thread(void* arg) { int fd = *(int*)arg; char buf[1024]; while (1) { int n = read(fd, buf, sizeof(buf)-1); if (n > 0) { buf[n] = '\0'; printf("read data: %s\n", buf); } usleep(10000); } return NULL; } void* write_thread(void* arg) { int fd = *(int*)arg; char buf[1024]; while (1) { fgets(buf, sizeof(buf), stdin); int n = write(fd, buf, strlen(buf)); if (n < 0) { perror("write serial port error!"); break; } } return NULL; } int main() { int fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY); if (fd < 0) { perror("open serial port error!"); return -1; } pthread_t tid1, tid2; pthread_create(&tid1, NULL, read_thread, &fd); pthread_create(&tid2, NULL, write_thread, &fd); pthread_join(tid1, NULL); pthread_join(tid2, NULL); close(fd); return 0; } ``` 其中,`read_thread()`函数实现串口数据的读取,`write_thread()`函数实现串口数据的入。使用`pthread_create()`函数创建两个线程,并传递串口文件描述符作为参数。主线程使用`pthread_join()`函数等待两个线程退出。如果失败,使用`perror()`函数输出错误信息并退出。 以上就是Linux C++高并发串口实现方法。需要注意的是,在多线程环境下,需要加锁保证数据的同步和互斥访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值