|
- /***********************************************************************
- * Copyright (C) 2025-, XINJE Co., Ltd.
- *
- * File Name: timeout_handler.h
- * Description: Modbus主站的超时处理头文件
- * Others:
- * Version: v1.0
- * Author: weikai XINJE
- * Date: 2025-7-30
- ***********************************************************************/
-
- #ifndef TIMEOUT_HANDLER_H
- #define TIMEOUT_HANDLER_H
- #include <QObject>
- #include <QTimer>
-
- /**********************************************************************
- * Iterates over the contents of a TimeoutHandler.
- *TimeoutHandler
- *提供超时处理功能,若超过时间未收到响应,则触发超时重发机制
- *被SerialCommunicator调用使用
- ***********************************************************************/
- class TimeoutHandler : public QObject
- {
- Q_OBJECT
- public:
- explicit TimeoutHandler(QObject *parent = nullptr);
-
- // 获取当前重试次数
- int getCurrentRetryCount() const
- {
- return currentRetryCount_;
- }
-
- // 设置超时时间(毫秒)
- void setTimeoutInterval(int msec);
-
- // 获取当前超时时间(毫秒)
- int getTimeoutInterval() const;
-
- // 设置最大重试次数
- void setRetryCount(int count);
-
- // 获取当前设置的重试次数
- int getRetryCount() const;
-
- // 启动超时计时(若已在运行则重启)
- void start();
-
- // 停止超时计时并重置重试计数
- void stop();
-
- // 判断是否正在计时中
- bool isRunning() const;
-
- signals:
- // 超时信号(参数为当前重试次数)
- void timeoutOccurred(int retryCount);
-
- // 达到最大重试次数信号
- void maxRetriesReached();
-
- private:
- // 处理定时器超时
- void onTimerTimeout();
-
- QTimer *timer_; // 定时器
- int timeoutInterval_; // 超时时间(毫秒)
- int maxRetryCount_; // 最大重试次数
- int currentRetryCount_; // 当前重试次数
- };
-
- #endif // TIMEOUT_HANDLER_H
|