com_client/sipm_deubg_qt.cpp

212 lines
6.9 KiB
C++
Raw Normal View History

2024-11-20 02:16:11 +00:00
#include "sipm_deubg_qt.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include "./ui_sipm_deubg_qt.h"
void sipm_deubg_qt::load_default_settings() {
int index = 0;
// 波特率配置表
ui->cb_bandrate->addItem("1200", QSerialPort::Baud1200);
ui->cb_bandrate->addItem("2400", QSerialPort::Baud2400);
ui->cb_bandrate->addItem("4800", QSerialPort::Baud4800);
ui->cb_bandrate->addItem("9600", QSerialPort::Baud9600);
ui->cb_bandrate->addItem("19200", QSerialPort::Baud19200);
ui->cb_bandrate->addItem("38400", QSerialPort::Baud38400);
ui->cb_bandrate->addItem("38400", QSerialPort::Baud57600);
ui->cb_bandrate->addItem("57600", QSerialPort::Baud57600);
ui->cb_bandrate->addItem("115200", QSerialPort::Baud115200);
ui->cb_bandrate->addItem("3000000", 3000000);
// 设置默认波特率
index = ui->cb_bandrate->findText("9600");
ui->cb_bandrate->setCurrentIndex(index);
// 数据位配置表
ui->cb_databit->addItem("8", QSerialPort::Data8);
ui->cb_databit->addItem("7", QSerialPort::Data7);
ui->cb_databit->addItem("6", QSerialPort::Data6);
ui->cb_databit->addItem("5", QSerialPort::Data5);
// 设置默认数据位
index = ui->cb_databit->findText("8");
ui->cb_databit->setCurrentIndex(index);
// 校验位配置表
ui->cb_parity->addItem("奇校验", QSerialPort::OddParity);
ui->cb_parity->addItem("偶校验", QSerialPort::EvenParity);
ui->cb_parity->addItem("无校验", QSerialPort::NoParity);
// 设置默认校验位
index = ui->cb_parity->findText("无校验");
ui->cb_parity->setCurrentIndex(index);
// 停止位配置表
ui->cb_stop_bit->addItem("1", QSerialPort::OneStop);
ui->cb_stop_bit->addItem("1.5", QSerialPort::OneAndHalfStop);
ui->cb_stop_bit->addItem("2", QSerialPort::TwoStop);
// 设置默认停止位
index = ui->cb_stop_bit->findText("1");
ui->lb_con_sta->setText("未打开");
}
void sipm_deubg_qt::lock_config_ui() {
ui->cb_com_name->setEnabled(false);
ui->cb_bandrate->setEnabled(false);
ui->cb_databit->setEnabled(false);
ui->cb_parity->setEnabled(false);
ui->cb_stop_bit->setEnabled(false);
}
void sipm_deubg_qt::unlock_config_ui() {
ui->cb_com_name->setEnabled(true);
ui->cb_bandrate->setEnabled(true);
ui->cb_databit->setEnabled(true);
ui->cb_stop_bit->setEnabled(true);
ui->cb_parity->setEnabled(true);
}
void sipm_deubg_qt::open_com_port() {
if (serial != nullptr) {
return;
}
serial = new QSerialPort(ui->cb_com_name->currentText());
serial->setBaudRate(
ui->cb_bandrate->itemData(ui->cb_bandrate->currentIndex()).toInt());
QSerialPort::DataBits data_bits = static_cast<QSerialPort::DataBits>(
ui->cb_databit->itemData(ui->cb_databit->currentIndex()).toInt());
serial->setDataBits(data_bits);
QSerialPort::Parity parity = static_cast<QSerialPort::Parity>(
ui->cb_parity->itemData(ui->cb_parity->currentIndex()).toInt());
serial->setParity(parity);
QSerialPort::StopBits stop_bits = static_cast<QSerialPort::StopBits>(
ui->cb_stop_bit->itemData(ui->cb_stop_bit->currentIndex()).toInt());
serial->setStopBits(stop_bits);
if (serial->open(QIODevice::ReadWrite)) {
ui->lb_con_sta->setText("已打开");
connect(serial, &QSerialPort::readyRead, this, &sipm_deubg_qt::data_append);
lock_config_ui();
} else {
ui->lb_con_sta->setText("打开失败");
serial->close();
delete serial;
serial = nullptr;
unlock_config_ui();
}
}
void sipm_deubg_qt::close_com_port() {
if (serial != nullptr) {
serial->close();
delete serial;
serial = nullptr;
ui->lb_con_sta->setText("未打开");
unlock_config_ui();
}
}
// 写个扫描串口设备的函数
void sipm_deubg_qt::scan_com_port() {
// printf("scan_com_port\n");
if (serial != nullptr) {
return;
}
ui->cb_com_name->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
ui->cb_com_name->addItem(info.portName());
}
}
void sipm_deubg_qt::start_scan_com_port() {
// 一直扫描
if (scan_com == nullptr) {
scan_com = new QTimer(this);
connect(scan_com, &QTimer::timeout, this, &sipm_deubg_qt::scan_com_port);
scan_com->start(100);
}
}
void sipm_deubg_qt::data_append() {
QByteArray data = serial->readAll();
// 数据转换成 u8数组
std::vector<uint8_t> rawData(data.size());
memcpy(rawData.data(), data.data(), data.size());
ui->tb_data_read->append(data.toHex().data());
if (rawData[0] != 0xaa || rawData[1] != 0x55) {
return;
}
switch (data[4]) {
case 0x09: {
unsigned int gain = (rawData[5] << 8) | (rawData[6]);
ui->le_gain->setText(QString::number(gain));
} break;
case 0x18: {
} break;
case 0x06: {
unsigned int temp = (rawData[5] << 8) | (rawData[6]);
float f_temp = (float)temp * 0.01;
ui->le_temp->setText(QString::number(f_temp, 'f', 2));
} break;
default:
break;
}
}
void sipm_deubg_qt::sipm_cmd_send(int cmd, char *data, int len) {
CHECK_SERRIAL_IS_OPEN();
printf("send cmd : %x\n", cmd);
char send_data[100] = {0};
char prity = 0;
send_data[0] = 0xaa;
send_data[1] = 0x55;
int temp_len = len + 4;
send_data[2] = (temp_len >> 8) & 0xff;
send_data[3] = temp_len & 0xff;
send_data[4] = cmd;
for (int i = 2; i < 5; i++) {
prity += send_data[i];
}
for (int i = 0; i < len; i++) {
send_data[5 + i] = data[i];
prity += data[i];
}
send_data[5 + len] = prity;
serial->write(send_data, 5 + len + 1);
serial->flush();
}
void sipm_deubg_qt::sipm_read_gain() {
CHECK_SERRIAL_IS_OPEN();
sipm_cmd_send(0x09, nullptr, 0);
}
void sipm_deubg_qt::sipm_read_SN() {
CHECK_SERRIAL_IS_OPEN();
sipm_cmd_send(0x02, nullptr, 0);
}
void sipm_deubg_qt::sipm_read_temp() {
CHECK_SERRIAL_IS_OPEN();
sipm_cmd_send(0x06, nullptr, 0);
}
void sipm_deubg_qt::sipm_set_gain() {
CHECK_SERRIAL_IS_OPEN();
int gain = ui->le_gain->text().toInt();
char data[2] = {0};
data[0] = (gain >> 8) & 0xff;
data[1] = gain & 0xff;
sipm_cmd_send(0x18, data, 2);
}
sipm_deubg_qt::sipm_deubg_qt(QWidget *parent)
: QMainWindow(parent), ui(new Ui::sipm_deubg_qt) {
ui->setupUi(this);
serial = nullptr;
load_default_settings();
scan_com = nullptr;
start_scan_com_port();
connect(ui->bt_open_com, &QPushButton::clicked, this,
&sipm_deubg_qt::open_com_port);
connect(ui->bt_close_com, &QPushButton::clicked, this,
&sipm_deubg_qt::close_com_port);
connect(ui->bt_read_gain, &QPushButton::clicked, this,
&sipm_deubg_qt::sipm_read_gain);
connect(ui->bt_read_SN, &QPushButton::clicked, this,
&sipm_deubg_qt::sipm_read_SN);
connect(ui->bt_write_gain, &QPushButton::clicked, this,
&sipm_deubg_qt::sipm_set_gain);
connect(ui->bt_read_temp, &QPushButton::clicked, this,
&sipm_deubg_qt::sipm_read_temp);
}
sipm_deubg_qt::~sipm_deubg_qt() { delete ui; }