72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
#ifndef CALIBRATOR_H
|
||
#define CALIBRATOR_H
|
||
|
||
#include <QObject>
|
||
#include <QSerialPort>
|
||
#include <QTimer>
|
||
#include <QTextStream>
|
||
#include <QCoreApplication>
|
||
|
||
class Calibrator : public QObject
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit Calibrator(QObject *parent = nullptr);
|
||
~Calibrator();
|
||
|
||
bool connectSerialPort(const QString &portName, int baudRate = 9600);
|
||
void disconnectSerialPort();
|
||
void startCalibration(double temperature);
|
||
void stopCalibration();
|
||
void sendManualCommand(const QString &command);
|
||
bool isConnected() const { return serialPort && serialPort->isOpen(); }
|
||
|
||
signals:
|
||
void calibrationCompleted();
|
||
void calibrationFailed(const QString &error);
|
||
|
||
private slots:
|
||
void onSerialDataReceived();
|
||
void onCalibrationStepTimeout();
|
||
void onReceiveTimeout(); // 接收数据超时处理
|
||
|
||
private:
|
||
QSerialPort *serialPort;
|
||
QTimer *calibrationTimer;
|
||
QTimer *receiveTimeoutTimer; // 接收数据超时定时器
|
||
QTextStream *consoleOutput;
|
||
QByteArray receiveBuffer; // 接收数据缓冲区
|
||
|
||
// 校准状态
|
||
enum CalibrationState {
|
||
Idle,
|
||
Step1_TC0, // 发送TC0
|
||
Step2_KY18_First, // 第一次发送KY18
|
||
Step3_AC_First, // 第一次发送AC命令
|
||
Step4_KY18_Second, // 第二次发送KY18
|
||
Step5_AC_Second, // 第二次发送AC命令,检查校准结果
|
||
Step6_Restore_KY18, // 恢复模式:发送KY18
|
||
Step7_Restore_TC // 恢复模式:发送TC00001
|
||
};
|
||
|
||
CalibrationState currentState;
|
||
QString temperatureValue; // 校准温度值(如00240表示24.0℃)
|
||
int retryCount;
|
||
bool manualMode; // 手动模式标志
|
||
|
||
void sendCommand(const QString &command);
|
||
void processResponse(const QString &response);
|
||
void nextCalibrationStep();
|
||
bool checkCalibrationComplete(const QString &response);
|
||
QString extractACValues(const QString &response);
|
||
bool allValuesLessThan10(const QString &acValues);
|
||
void logMessage(const QString &message);
|
||
void logError(const QString &message);
|
||
void logSuccess(const QString &message);
|
||
void logInfo(const QString &message);
|
||
};
|
||
|
||
#endif // CALIBRATOR_H
|
||
|