100 lines
3.9 KiB
C++
100 lines
3.9 KiB
C++
#include "calibrator.h"
|
||
#include <QCoreApplication>
|
||
#include <QTextStream>
|
||
#include <QCommandLineParser>
|
||
#include <QTimer>
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
QCoreApplication app(argc, argv);
|
||
QCoreApplication::setApplicationName("CO2Correct");
|
||
QCoreApplication::setApplicationVersion("1.0.0");
|
||
|
||
QTextStream cout(stdout);
|
||
QTextStream cin(stdin);
|
||
|
||
// 命令行参数解析
|
||
QCommandLineParser parser;
|
||
parser.setApplicationDescription("CO2传感器校准工具");
|
||
parser.addHelpOption();
|
||
parser.addVersionOption();
|
||
|
||
QCommandLineOption portOption(QStringList() << "p" << "port",
|
||
"串口设备路径", "port", "/dev/ttyWind");
|
||
parser.addOption(portOption);
|
||
|
||
QCommandLineOption baudOption(QStringList() << "b" << "baud",
|
||
"波特率", "baud", "9600");
|
||
parser.addOption(baudOption);
|
||
|
||
QCommandLineOption tempOption(QStringList() << "t" << "temperature",
|
||
"校准温度(℃)", "temperature", "24.0");
|
||
parser.addOption(tempOption);
|
||
|
||
QCommandLineOption autoOption(QStringList() << "a" << "auto",
|
||
"自动模式:连接后立即开始校准");
|
||
parser.addOption(autoOption);
|
||
|
||
parser.process(app);
|
||
|
||
QString portName = parser.value(portOption);
|
||
int baudRate = parser.value(baudOption).toInt();
|
||
double temperature = parser.value(tempOption).toDouble();
|
||
bool autoMode = parser.isSet(autoOption);
|
||
|
||
Calibrator calibrator;
|
||
|
||
// 连接信号
|
||
QObject::connect(&calibrator, &Calibrator::calibrationCompleted, [&]() {
|
||
cout << "\n Mission Complete!\n";
|
||
QTimer::singleShot(1000, &app, &QCoreApplication::quit);
|
||
});
|
||
|
||
QObject::connect(&calibrator, &Calibrator::calibrationFailed, [&](const QString &error) {
|
||
cout << "\n校准失败: " << error << "\n";
|
||
QTimer::singleShot(1000, &app, &QCoreApplication::quit);
|
||
});
|
||
|
||
// 连接串口
|
||
cout << QString("正在连接串口: %1, 波特率: %2...\n").arg(portName).arg(baudRate);
|
||
if (!calibrator.connectSerialPort(portName, baudRate)) {
|
||
cout << "串口连接失败,程序退出\n";
|
||
return 1;
|
||
}
|
||
|
||
if (autoMode) {
|
||
// 自动模式:立即开始校准
|
||
cout << QString("自动模式:开始校准,温度: %1℃\n").arg(temperature)<<flush;
|
||
calibrator.startCalibration(temperature);
|
||
} else {
|
||
// 交互模式
|
||
cout << "\n=== CO2传感器校准工具 ===\n";
|
||
cout << "串口: " << portName << ", 波特率: " << baudRate << "\n";
|
||
cout << "\n命令:\n";
|
||
cout << " start [温度] - 开始校准(默认温度24.0℃)\n";
|
||
cout << " stop - 停止校准\n";
|
||
cout << " send <命令> - 手动发送命令\n";
|
||
cout << " quit/exit - 退出程序\n";
|
||
cout << "\n提示: 使用 -a 参数可自动模式(连接后立即开始校准)\n";
|
||
cout << "示例: " << app.applicationName() << " -a -t 24.0\n\n";
|
||
|
||
// 在单独的线程中处理输入,或者使用QSocketNotifier
|
||
// 这里使用简单的提示,建议使用自动模式
|
||
cout << "注意: 交互模式需要手动输入命令。\n";
|
||
cout << "建议使用自动模式: " << app.applicationName() << " -a -t " << temperature << "\n";
|
||
cout << "或者直接开始校准,输入: start " << temperature << "\n\n";
|
||
|
||
// 使用QSocketNotifier来非阻塞读取stdin
|
||
// 简化处理:直接提示用户使用自动模式
|
||
cout << "等待5秒后自动开始校准(温度: " << temperature << "℃)...\n";
|
||
cout << "按Ctrl+C可取消\n";
|
||
|
||
QTimer::singleShot(5000, [&]() {
|
||
cout << "\n自动开始校准...\n";
|
||
calibrator.startCalibration(temperature);
|
||
});
|
||
}
|
||
|
||
return app.exec();
|
||
}
|