|
- /*******************************
- * Copyright (C) 2025-.
- *
- * File Name: serialcommunicator.h
- * Description: 串口通信管理类,封装串口操作、数据收发和超时处理
- * Others:
- * Version: 1.0
- * Author: lipengpeng
- * Date: 2025-7-23
- *******************************/
- #ifndef SERIALCOMMUNICATOR_H
- #define SERIALCOMMUNICATOR_H
-
- #include <QObject>
- #include <QSerialPort>
- #include <QTimer>
- #include <QByteArray>
-
- /**
- * @brief 串口通信管理类
- * @class SerialCommunicator
- *
- * @details 此类封装了串口通信的核心功能,包括:
- * 1. 串口参数配置与开关控制
- * 2. 数据发送与自动重发机制
- * 3. 数据接收与完整性判断
- * 4. 超时处理与状态通知
- *
- * @note 使用流程:
- * 1. 设置串口参数(setPortParams)
- * 2. 打开串口(open)
- * 3. 发送数据(sendData)
- * 4. 接收数据(通过dataReceived信号)
- * 5. 关闭串口(close)
- */
- class SerialCommunicator : public QObject
- {
- Q_OBJECT
- public:
- /**
- * @brief 构造函数
- * @param parent 父对象指针
- */
- SerialCommunicator(QObject *parent = nullptr);
-
- /**
- * @brief 析构函数
- * @note 自动关闭串口并释放资源
- */
- ~SerialCommunicator();
-
- // ====================== 串口配置 ====================== //
-
- /**
- * @brief 设置串口参数
- * @param portName 串口号(如"COM1"或"/dev/ttyS0")
- * @param baudRate 波特率(如9600, 115200)
- * @param dataBits 数据位(通常为QSerialPort::Data8)
- * @param parity 校验位(无校验/奇校验/偶校验)
- * @param stopBits 停止位(通常为QSerialPort::OneStop)
- * @note 此方法应在打开串口前调用
- */
- void setPortParams(QString portName_, qint32 baudRate_,
- QSerialPort::DataBits dataBits_,
- QSerialPort::Parity parity_,
- QSerialPort::StopBits stopBits_);
-
- /**
- * @brief 打开串口
- * @return true-打开成功, false-打开失败
- * @note 成功打开后会发出statusChanged信号
- */
- bool open();
-
- /**
- * @brief 关闭串口
- * @note 关闭后会发出statusChanged信号
- */
- void close();
-
- // ====================== 数据操作 ====================== //
-
- /**
- * @brief 发送数据并启动超时重发机制
- * @param data 待发送的数据
- * @note 数据发送后启动重发定时器,如达到最大重发次数仍未收到响应,
- * 将发出timeoutOccurred信号
- */
- void sendData(const QByteArray &data);
-
- // ====================== 超时配置 ====================== //
-
- /**
- * @brief 设置最大重发次数
- * @param count 重发次数(默认3次)
- */
- void setMaxRetry(int count);
-
- /**
- * @brief 设置接收完成超时时间
- * @param ms 超时时间(毫秒,默认50ms)
- * @note 当串口持续ms毫秒无新数据时,认为接收完成
- */
- void setRecvTimeout(int ms);
-
- /**
- * @brief 设置重发间隔时间
- * @param ms 重发间隔(毫秒,默认1000ms)
- */
- void setResendTimeout(int ms);
-
- // ====================== 状态查询 ====================== //
-
- /**
- * @brief 检查串口是否打开
- * @return true-已打开, false-已关闭
- */
- bool isOpen() const;
-
- void setCheckMsg(QByteArray msg);
-
- signals:
- /**
- * @brief 接收到完整数据信号
- * @param data 接收到的完整数据包
- */
- void dataReceived(const QByteArray &data);
-
- /**
- * @brief 状态变化信号
- * @param status 状态描述文本
- * @note 可能的状态: "已连接", "已断开", "接收超时"等
- */
- void statusChanged(const QString &status);
-
- /**
- * @brief 通信超时信号
- * @note 当达到最大重发次数仍未收到响应时触发
- */
- void timeoutOccurred();
-
- /**
- * @brief 串口错误信号
- * @note 当串口发生异常错误时触发
- */
- void physicalDisconnected();
-
- void stationConnect(bool online);
-
- private slots:
- /**
- * @brief 串口数据到达处理
- * @note 当串口有数据可读时触发,数据存入缓冲区并启动接收超时定时器
- */
- void onReadyRead();
-
- /**
- * @brief 接收完成超时处理
- * @note 当recvTimer超时,认为数据接收完成,发出dataReceived信号
- */
- void onRecvTimeout();
-
- /**
- * @brief 重发超时处理
- * @note 当resendTimer超时,检查重发次数并决定是否重发数据
- */
- void onResendTimeout();
-
- /**
- * @brief 处理串口错误
- * @param error 串口错误代码
- */
- void handleSerialError(QSerialPort::SerialPortError error);
-
- void stationCheck();
- void checkTimeOut();
-
- private:
- QSerialPort *serialPort_; // 串口对象
- QTimer *recvTimer_; // 接收完成判断定时器
- QTimer *resendTimer_; // 超时重发定时器
- QTimer *stationCheck_; // 检测是否连接到从站
- QTimer *checkTimeOut_;
- QByteArray recvBuffer_; // 接收缓冲区
- QByteArray currentData_; // 当前待重发数据
- int comCount_; // 重发计数器
- int maxRetry_; // 最大重发次数
- int recvTimeout_; // 接收超时时间(ms)
- int resendTimeout_; // 重发间隔(ms)
- QByteArray test_;
-
- // 串口参数
- QString portName_;
- qint32 baudRate_;
- QSerialPort::DataBits dataBits_;
- QSerialPort::Parity parity_;
- QSerialPort::StopBits stopBits_;
- };
-
- #endif // SERIALCOMMUNICATOR_H
|