/*
* 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 <vector>
#include <string>
#include <unistd.h>
#include <sys/time.h>
#include <iostream>
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<BSerialPort *> 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()<<endl;
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));
}
}
}
}
本站文章版權歸原作者及原出處所有 。內容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構成任何投資及應用建議。本站是一個個人學習交流的平臺,網站上部分文章為轉載,并不用于任何商業目的,我們已經盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯系我們,我們將根據著作權人的要求,立即更正或者刪除有關內容。本站擁有對此聲明的最終解釋權。