CP控制V1.0
This commit is contained in:
63
Serial/SerialPort.h
Normal file
63
Serial/SerialPort.h
Normal file
@ -0,0 +1,63 @@
|
||||
#ifndef ATCMD_SERIALPORT_H
|
||||
#define ATCMD_SERIALPORT_H
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
using speed_t = unsigned int;
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
class SerialPort {
|
||||
public:
|
||||
// 处理接收数据函数模板
|
||||
using ReceiveCallback = std::function<void(const std::vector<uint8_t> &)>;
|
||||
|
||||
// 打开串口设备
|
||||
SerialPort(const std::string &device, int baudrate);
|
||||
|
||||
~SerialPort();
|
||||
|
||||
// 配置串口参数
|
||||
void configure(int baudrate, int fd);
|
||||
|
||||
// 发送数据
|
||||
ssize_t write(const std::vector<uint8_t>& data);
|
||||
ssize_t write(const std::string & data);
|
||||
ssize_t write(unsigned char *str,int size);
|
||||
|
||||
// 开始接收线程
|
||||
void start_receiving(const ReceiveCallback &callback);
|
||||
|
||||
// 停止接收线程
|
||||
void stop_receiving();
|
||||
|
||||
void close();
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
HANDLE fd_ = INVALID_HANDLE_VALUE;
|
||||
#else
|
||||
int fd_ = -1;
|
||||
#endif
|
||||
std::atomic<bool> running_;
|
||||
std::thread receiver_thread_;
|
||||
|
||||
// 接收数据
|
||||
std::vector<uint8_t> read();
|
||||
|
||||
//获取波特率
|
||||
speed_t getBaudRate(int baudrate);
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //ATCMD_SERIALPORT_H
|
Reference in New Issue
Block a user