You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
387 lines
12 KiB
387 lines
12 KiB
#include "mainwindow.h"
|
|
|
|
#include <QDateTime>
|
|
#include <QMessageBox>
|
|
#include <QtConcurrent>
|
|
#include <QtSerialPort/QSerialPort>
|
|
#include <QtSerialPort/QSerialPortInfo>
|
|
|
|
#include "./ui_mainwindow.h"
|
|
#include "logger.hpp"
|
|
#include "qt_serial_datachannel.hpp"
|
|
#include "version.h"
|
|
|
|
using namespace iflytop;
|
|
using namespace std;
|
|
|
|
static MainWindow *m_mainWindow;
|
|
QTimer *timer0;
|
|
QTimer *timer1;
|
|
QTimer *timer2;
|
|
QTimer *timer3;
|
|
QTimer *checkConnectTimer0;
|
|
static bool m_connected = false;
|
|
|
|
ZQThread *m_zqthread;
|
|
#define TAG "MainWindow"
|
|
|
|
static const char *fmt(const char *fmt, ...) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
static char buf[1024] = {0};
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
va_end(args);
|
|
return buf;
|
|
}
|
|
|
|
static const uint32_t str2int(QString str) {
|
|
// 如果0x开头,??16进制转换
|
|
// 如果0b开头,??2进制转换
|
|
// 否则??10进制转换
|
|
// 去除掉str中_
|
|
str.remove("_");
|
|
|
|
if (str.startsWith("0x")) {
|
|
return str.toUInt(nullptr, 16);
|
|
} else if (str.startsWith("0b")) {
|
|
// remove 0b
|
|
str.remove(0, 2);
|
|
return str.toUInt(nullptr, 2);
|
|
} else {
|
|
return str.toUInt(nullptr, 10);
|
|
}
|
|
}
|
|
// static QSerialPort G_SerialPort;
|
|
// static QThread G_SerialPortThread;
|
|
static QTDataChannel G_QTDataChannel;
|
|
|
|
// static const QString zaferror_to_str(zaf_error_code_t value) {
|
|
// if (value == kaf_ec_overtime) {
|
|
// return "通信超时";
|
|
// } else if (value == kaf_ec_device_notopen) {
|
|
// return "设备未打开";
|
|
// } else {
|
|
// return "未知错误";
|
|
// }
|
|
// }
|
|
|
|
void MainWindow::log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
|
|
// QString text;
|
|
// text.append(msg);
|
|
}
|
|
// void MainWindow::append_log_slot(QString text) { ui->logbrowser->append(text); }
|
|
void MainWindow::doinui_slot(QFunction func) {
|
|
if (func.get()) func.get()();
|
|
}
|
|
|
|
void MainWindow::refreshReadonlyPage0() {}
|
|
void MainWindow::refreshReadonlyPage1() {}
|
|
void MainWindow::refreshReadonlyPage2() {}
|
|
void MainWindow::refreshReadonlyPage3() {}
|
|
bool MainWindow::checkConnected() { return true; }
|
|
|
|
void MainWindow::constructUI() {
|
|
/*******************************************************************************
|
|
* serialPortCB *
|
|
*******************************************************************************/
|
|
const auto infos = QSerialPortInfo::availablePorts();
|
|
for (const QSerialPortInfo &info : infos) {
|
|
ui->serialPortCB->addItem(info.portName());
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* 波特率填?? *
|
|
*******************************************************************************/
|
|
ui->serialBaudrateCB->addItem("9600");
|
|
ui->serialBaudrateCB->addItem("14400");
|
|
ui->serialBaudrateCB->addItem("19200");
|
|
ui->serialBaudrateCB->addItem("38400");
|
|
ui->serialBaudrateCB->addItem("57600");
|
|
ui->serialBaudrateCB->addItem("115200");
|
|
ui->serialBaudrateCB->addItem("500000");
|
|
ui->serialBaudrateCB->setCurrentIndex(0);
|
|
|
|
/*******************************************************************************
|
|
* 刷新串口 *
|
|
*******************************************************************************/
|
|
connect(ui->serialPortRefreshKey, &QPushButton::clicked, this, [this](bool check) {
|
|
ui->serialPortCB->clear();
|
|
const auto infos = QSerialPortInfo::availablePorts();
|
|
for (const QSerialPortInfo &info : infos) {
|
|
ui->serialPortCB->addItem(info.portName());
|
|
}
|
|
});
|
|
|
|
/*******************************************************************************
|
|
* 打开串口 *
|
|
*******************************************************************************/
|
|
connect(ui->serialOpenKey, &QPushButton::clicked, this, [=](bool check) {
|
|
// 打开串口
|
|
if (ui->serialOpenKey->text() == "打开") {
|
|
G_QTDataChannel.setPortName(ui->serialPortCB->currentText().toStdString());
|
|
G_QTDataChannel.setBaudRate(ui->serialBaudrateCB->currentText().toInt());
|
|
G_QTDataChannel.setDataBits(QSerialPort::Data8);
|
|
G_QTDataChannel.setParity(QSerialPort::NoParity);
|
|
G_QTDataChannel.setFlowControl(QSerialPort::NoFlowControl);
|
|
G_QTDataChannel.setStopBits(QSerialPort::OneStop);
|
|
|
|
if (!G_QTDataChannel.open()) {
|
|
QMessageBox::about(NULL, "提示", "串口无法打开,串口不存在或已被占??");
|
|
return;
|
|
}
|
|
ui->serialOpenKey->setText("关闭");
|
|
// 下拉菜单控件使能
|
|
ui->serialBaudrateCB->setEnabled(false);
|
|
ui->serialPortCB->setEnabled(false);
|
|
ui->serialPortRefreshKey->setEnabled(false);
|
|
} else {
|
|
G_QTDataChannel.close();
|
|
ui->serialOpenKey->setText("打开");
|
|
ui->serialBaudrateCB->setEnabled(true);
|
|
ui->serialPortCB->setEnabled(true);
|
|
ui->serialPortRefreshKey->setEnabled(true);
|
|
}
|
|
});
|
|
}
|
|
|
|
typedef enum {
|
|
koutput_mode_continue_mode,
|
|
koutput_mode_pluse_mode,
|
|
} OutputMode_t;
|
|
|
|
typedef enum {
|
|
// 自定义
|
|
koutput_pluse_type_custom,
|
|
// 方波
|
|
koutput_pluse_type_square_wave,
|
|
} OutputPluseType_t;
|
|
|
|
static QString outputMode2Str(OutputMode_t mode) {
|
|
if (mode == koutput_mode_continue_mode) {
|
|
return "连续模式";
|
|
} else if (mode == koutput_mode_pluse_mode) {
|
|
return "脉冲模式";
|
|
} else {
|
|
return "未知模式";
|
|
}
|
|
}
|
|
|
|
static OutputMode_t outputModeStr2Enum(QString str) {
|
|
if (str == "连续模式") {
|
|
return koutput_mode_continue_mode;
|
|
} else if (str == "脉冲模式") {
|
|
return koutput_mode_pluse_mode;
|
|
} else {
|
|
return koutput_mode_continue_mode;
|
|
}
|
|
}
|
|
|
|
static QString outputPluseType2Str(OutputPluseType_t type) {
|
|
if (type == koutput_pluse_type_custom) {
|
|
return "自定义";
|
|
} else if (type == koutput_pluse_type_square_wave) {
|
|
return "方波";
|
|
} else {
|
|
return "未知";
|
|
}
|
|
}
|
|
|
|
static OutputPluseType_t outputPluseTypeStr2Enum(QString str) {
|
|
if (str == "自定义") {
|
|
return koutput_pluse_type_custom;
|
|
} else if (str == "方波") {
|
|
return koutput_pluse_type_square_wave;
|
|
} else {
|
|
return koutput_pluse_type_custom;
|
|
}
|
|
}
|
|
|
|
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
|
// G_SerialPort.moveToThread();
|
|
// QObject::connect(&G_SerialPortThread, &QThread::started, &G_SerialPort, &QSerialPort::open);
|
|
|
|
G_QTDataChannel.init();
|
|
|
|
ui->setupUi(this);
|
|
m_mainWindow = this;
|
|
|
|
qRegisterMetaType<int32_t>("int32_t");
|
|
qRegisterMetaType<uint32_t>("uint32_t");
|
|
qRegisterMetaType<float>("float");
|
|
qRegisterMetaType<function<void()>>("function<void()>");
|
|
qRegisterMetaType<QFunction>("QFunction");
|
|
// qInstallMessageHandler(log_output);
|
|
connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
|
|
constructUI();
|
|
|
|
this->setMinimumSize(QSize(width(), height()));
|
|
this->setMaximumSize(QSize(width(), height()));
|
|
ui->UpperVersion->setText(fmt("V%d", VERSION));
|
|
|
|
{
|
|
//
|
|
ui->StateGB->hide();
|
|
ui->OutputModeVal->addItem(outputMode2Str(koutput_mode_continue_mode));
|
|
ui->OutputModeVal->addItem(outputMode2Str(koutput_mode_pluse_mode));
|
|
|
|
ui->OutputPluseTypeVal->addItem(outputPluseType2Str(koutput_pluse_type_custom));
|
|
ui->OutputPluseTypeVal->addItem(outputPluseType2Str(koutput_pluse_type_square_wave));
|
|
|
|
ui->OutputPluseWidthVal->setText("1");
|
|
ui->OutputPluseNumVal->setText("1");
|
|
ui->OutputPluseFreqVal->setText("1000");
|
|
}
|
|
|
|
G_QTDataChannel.regRxListener([this](uint8_t *data, size_t len) {
|
|
char m_rxbuf[1024] = {0};
|
|
int m_rxlen = 0;
|
|
|
|
memcpy(m_rxbuf + m_rxlen, data, len);
|
|
m_rxlen += len;
|
|
|
|
iPreviewAppend(string((char *)data, len));
|
|
|
|
// dumpLog("RX:%s", rxbuf, len);
|
|
});
|
|
|
|
// 创建定时器
|
|
timer0 = new QTimer(this);
|
|
timer1 = new QTimer(this);
|
|
timer2 = new QTimer(this);
|
|
timer3 = new QTimer(this);
|
|
checkConnectTimer0 = new QTimer(this);
|
|
|
|
m_zqthread = new ZQThread("", [this]() {
|
|
while (true) {
|
|
static bool first = true;
|
|
bool connect = checkConnected();
|
|
if (m_connected != connect || first) {
|
|
first = false;
|
|
m_connected = connect;
|
|
emit doinui_signal(QFunction([this, connect]() {
|
|
if (connect) {
|
|
ui->DeviceConnectStateTB->setText("已连接");
|
|
ui->DeviceConnectStateTB->setStyleSheet("color: green");
|
|
} else {
|
|
ui->DeviceConnectStateTB->setText("未连接");
|
|
ui->DeviceConnectStateTB->setStyleSheet("color: red");
|
|
}
|
|
if (m_connected) {
|
|
// m_clstc->initDevice();
|
|
// m_clstc->setAllShawdowRegDirty();
|
|
// refreshPage();
|
|
}
|
|
}));
|
|
}
|
|
|
|
if (connect) {
|
|
refreshReadonlyPage0();
|
|
refreshReadonlyPage1();
|
|
refreshReadonlyPage2();
|
|
refreshReadonlyPage3();
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
}
|
|
});
|
|
m_zqthread->start();
|
|
}
|
|
|
|
QString iPreviewContent;
|
|
|
|
void MainWindow::iPreviewClean() {
|
|
emit doinui_signal(QFunction([this]() {
|
|
ui->instructionPreview->clear();
|
|
iPreviewContent = "";
|
|
}));
|
|
}
|
|
void MainWindow::iPreviewAppend(std::string info) {
|
|
emit doinui_signal(QFunction([this, info]() { //
|
|
iPreviewContent += QString(info.c_str());
|
|
ui->instructionPreview->setText(iPreviewContent);
|
|
}));
|
|
}
|
|
void MainWindow::iPreviewShow(const char *fmt, ...) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
static char buf[1024] = {0};
|
|
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
va_end(args);
|
|
iPreviewAppend(buf);
|
|
}
|
|
|
|
MainWindow::~MainWindow() { delete ui; }
|
|
|
|
void MainWindow::on_OutputModeVal_currentIndexChanged(const QString &arg1) { onModeChange(); }
|
|
void MainWindow::on_OutputPluseTypeVal_currentIndexChanged(const QString &arg1) { onModeChange(); }
|
|
void MainWindow::onModeChange() {
|
|
OutputMode_t outputMode = outputModeStr2Enum(ui->OutputModeVal->currentText());
|
|
OutputPluseType_t outputPluseType = outputPluseTypeStr2Enum(ui->OutputPluseTypeVal->currentText());
|
|
|
|
if (outputMode == koutput_mode_continue_mode) {
|
|
ui->OutputPluseNumVal->hide();
|
|
ui->OutputPluseNumTag->hide();
|
|
} else if (outputMode == koutput_mode_pluse_mode) {
|
|
ui->OutputPluseNumVal->show();
|
|
ui->OutputPluseNumTag->show();
|
|
}
|
|
|
|
if (outputPluseType == koutput_pluse_type_custom) {
|
|
ui->OutputPluseWidthVal->show();
|
|
ui->OutputPluseWidthTag->show();
|
|
} else if (outputPluseType == koutput_pluse_type_square_wave) {
|
|
ui->OutputPluseWidthVal->hide();
|
|
ui->OutputPluseWidthTag->hide();
|
|
}
|
|
}
|
|
|
|
void MainWindow::on_SendButton_clicked() {
|
|
OutputMode_t outputMode = outputModeStr2Enum(ui->OutputModeVal->currentText());
|
|
OutputPluseType_t outputPluseType = outputPluseTypeStr2Enum(ui->OutputPluseTypeVal->currentText());
|
|
|
|
uint32_t outputPluseWidthUs_01 = str2int(ui->OutputPluseWidthVal->text()) * 10;
|
|
uint32_t outputPluseNum = str2int(ui->OutputPluseNumVal->text());
|
|
|
|
int freq = str2int(ui->OutputPluseFreqVal->text());
|
|
|
|
/**
|
|
*
|
|
* @brief 协议
|
|
*
|
|
* S0000112345601234567
|
|
* S 0 0 0 0 1 1 2 3 4 5 6 0 1 2 3 4 5 6 7
|
|
* [S] [2][3][4][5][6] [7][8][9][10][11][12][13] [14][15][16][17][18][19][20]
|
|
* 开始标志 频率值HZ 脉宽0.1us 脉冲数
|
|
*
|
|
*
|
|
*/
|
|
|
|
// S9900000000500000010 99khz,50%,10个脉冲
|
|
// S9900000000500000011 99khz,50%,11个脉冲
|
|
// S9900000000500000000 99khz,50%,连续脉冲
|
|
// S0100000050000000010 1khz,%50 10个脉冲
|
|
// S0100000050000000020 1khz,%50 20个脉冲
|
|
// S 01000 0005000 0000000 1khz,%50连续脉冲
|
|
|
|
if (outputPluseType == koutput_pluse_type_square_wave) {
|
|
double periodus = 1.0 * 1000 * 1000 / freq;
|
|
outputPluseWidthUs_01 = periodus * 10 / 2;
|
|
}
|
|
|
|
QString cmd = fmt("S%05d%07d%07d", freq, outputPluseWidthUs_01, outputPluseNum);
|
|
QString txcmd = fmt("%s\r\n", cmd.toStdString().c_str());
|
|
|
|
iPreviewClean();
|
|
iPreviewShow(" %s\n", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str());
|
|
if (G_QTDataChannel.isOpen()) {
|
|
G_QTDataChannel.send((const uint8_t *)txcmd.toStdString().c_str(), txcmd.length());
|
|
iPreviewShow("-----------------TX----------------\n");
|
|
iPreviewShow("%s\n", cmd.toStdString().c_str());
|
|
iPreviewShow("\n");
|
|
iPreviewShow("-----------------RX----------------\n");
|
|
} else {
|
|
iPreviewShow("串口未打开\n");
|
|
}
|
|
|
|
// dumpLog
|
|
}
|