|
- /***********************************************************************
- * Copyright (C) 2025-, XINJE Co., Ltd.
- *
- * File Name: serial_communication.h
- * Description: Modbus主站的串口通信头文件
- * Others:
- * Version: v1.0
- * Author: weikai XINJE
- * Date: 2025-7-30
- ***********************************************************************/
-
- #ifndef SERIALCOMMUNICATOR_H
- #define SERIALCOMMUNICATOR_H
-
- #include <QObject>
- #include <QSerialPort>
- #include <QSerialPortInfo>
- #include <QByteArray>
- #include <QString>
- #include"timeout_handler.h"
-
- /**********************************************************************
- * Iterates over the contents of a SerialCommunicator.
- *SerialCommunicator
- *提供串口通信功能,连接,断开连接,发送数据,接收数据处理等
- *调用TimeoutHandler实例进行超时处理
- ***********************************************************************/
- class SerialCommunicator : public QObject
- {
- Q_OBJECT
- public:
- // 构造函数
- explicit SerialCommunicator(QObject *parent = nullptr);
- ~SerialCommunicator();
-
- // 参数设置接口
- void setPortName(const QString &portName);
- void setBaudRate(int baudRate);
- void setDataBits(QSerialPort::DataBits dataBits);
- void setStopBits(QSerialPort::StopBits stopBits);
- void setParity(QSerialPort::Parity parity);
-
- //设置是否可以开始心跳
- void setIsCanHeart(bool flag) {isCanHeartbeat_ = flag;}
- void setHeartbeatDFrame(const QByteArray& frame){ heartbeatFrame_ = frame; }
-
- // 初始化函数(设置默认参数)
- void init();
-
- // 连接/断开接口
- bool connectDevice();
- void disconnectDevice();
-
- // 设置超时参数
- bool setTimeoutSettings(int timeoutMs, int maxRetries);
- //获取
- void getTimeoutSettings(int& timeoutMs,int& maxRetries);
- // 发送数据接口
- bool sendData(const QByteArray &data);
-
- // 状态查询接口
- bool isConnected() const;
- //获取可用端口列表
- QStringList getAvailablePorts();
-
- signals:
- // 数据接收信号(发送处理后的十六进制字符串)
- void dataReceived(const QString &hexData);
- // 状态通知信号
- void statusChanged(const QString &status);
- // 错误通知信号
- void errorOccurred(const QString &errorMsg);
- // 连接断开信号
- void connectionDisconnected();
-
- private:
- // 内部数据接收处理
- void onReadyRead();
- // 处理超时信号
- void onTimeoutOccurred(int currentRetry);
- // 处理最大重试次数达到
- void onMaxRetriesReached();
- //处理串口错误
- void onSerialError(QSerialPort::SerialPortError error);
-
- //发送心跳槽函数
- void onSendHeartbeat();
- //心跳超时槽函数
- void onHeartbeatTimeout();
- //启动心跳机制
- void startHeartbeat();
- //停止心跳机制
- void stopHeartbeat();
-
- QSerialPort *serialPort_;// 串口对象
- // 串口参数
- QString portName_;//串口名
- int baudRate_;//波特率
- QSerialPort::DataBits dataBits_;//数据位
- QSerialPort::StopBits stopBits_;//停止位
- QSerialPort::Parity parity_;//奇偶校验
-
- bool connected_;// 连接状态
-
- TimeoutHandler timeoutHandler_; // 超时处理器
- QByteArray pendingData_; // 等待响应的数据(用于重发)
-
- //断线重连
- QTimer* heartbeatTimer_;//定期发送心跳帧
- QTimer* heartbeatTimeoutTimer_;//心跳响应计时器
- int heartbeatInterval_;//心跳间隔
- int heartbeatTimeout_;//心跳响应时间
- QByteArray heartbeatFrame_;//心跳帧
- bool isCanHeartbeat_;//是否可以开始心跳
- };
-
- #endif // SERIALCOMMUNICATOR_H
|