init
This commit is contained in:
42
include/MySerial.h
Normal file
42
include/MySerial.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef SERIALPORT_H
|
||||
#define SERIALPORT_H
|
||||
|
||||
#include <string>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
class SerialPort
|
||||
{
|
||||
private:
|
||||
std::string m_portName; // 串口名称(如/dev/ttyUSB0)
|
||||
speed_t m_baudRate; // 串口波特率
|
||||
int m_fd; // 文件描述符,用于表示串口文件
|
||||
struct termios m_tty; // 用于保存串口配置的结构体
|
||||
public:
|
||||
// 构造函数,初始化串口路径和波特率
|
||||
SerialPort(const std::string &portName, speed_t baudRate);
|
||||
|
||||
// 析构函数,确保在对象销毁时关闭串口
|
||||
~SerialPort();
|
||||
|
||||
// 打开串口
|
||||
int openPort();
|
||||
|
||||
// 关闭串口
|
||||
void closePort();
|
||||
|
||||
// 写数据到串口
|
||||
int writeData(const char *data, size_t size);
|
||||
|
||||
// 从串口读取数据
|
||||
int readData(char *buffer, size_t bufferSize);
|
||||
|
||||
// 判断串口是否已打开
|
||||
bool isOpen() const;
|
||||
};
|
||||
|
||||
#endif // SERIALPORT_H
|
Reference in New Issue
Block a user