com_client/com_erpc_core.cpp

222 lines
6.9 KiB
C++

#include "com_erpc_core.hpp"
#include <QSerialPort>
#include <QSerialPortInfo>
#include "./ui_com_erpc_core.h"
void com_erpc_core::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 com_erpc_core::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 com_erpc_core::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);
}
/**
* com port 断开连接回调
*/
void com_erpc_core::com_port_disconnect(QSerialPort::SerialPortError error) {
if (serial != nullptr) {
if(error == QSerialPort::ResourceError)
{
if(stop_com == nullptr){
stop_com = new QTimer(this);
connect(stop_com, &QTimer::timeout, this, &com_erpc_core::close_com_port);
stop_com->start(10);
}
}
}
}
/**
* 打开串口
*/
void com_erpc_core::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);
//注册断线回调
connect(serial, &QSerialPort::errorOccurred, this, &com_erpc_core::com_port_disconnect);
//尝试打开com口
if (serial->open(QIODevice::ReadWrite)) {
ui->lb_con_sta->setText("已打开");
connect(serial, &QSerialPort::readyRead, this, &com_erpc_core::data_append);
lock_config_ui();
} else {
ui->lb_con_sta->setText("打开失败");
serial->close();
delete serial;
serial = nullptr;
unlock_config_ui();
}
}
void com_erpc_core::close_com_port() {
if (serial != nullptr) {
serial->close();
delete serial;
serial = nullptr;
ui->lb_con_sta->setText("未打开");
unlock_config_ui();
}
if(stop_com!= nullptr){
stop_com->stop();
delete stop_com;
stop_com = nullptr;
}
}
// 写个扫描串口设备的函数
void com_erpc_core::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 com_erpc_core::start_scan_com_port() {
// 一直扫描
if (scan_com == nullptr) {
scan_com = new QTimer(this);
connect(scan_com, &QTimer::timeout, this, &com_erpc_core::scan_com_port);
scan_com->start(100);
}
}
void com_erpc_core::data_append() {
printf("data_append\n");
QByteArray data = serial->readAll();
ui->tb_data_read->append(QString::fromUtf8(data));
}
void com_erpc_core::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 com_erpc_core::sipm_read_gain() {
CHECK_SERRIAL_IS_OPEN();
sipm_cmd_send(0x09, nullptr, 0);
}
void com_erpc_core::sipm_read_SN() {
CHECK_SERRIAL_IS_OPEN();
sipm_cmd_send(0x02, nullptr, 0);
}
void com_erpc_core::sipm_read_temp() {
CHECK_SERRIAL_IS_OPEN();
sipm_cmd_send(0x06, nullptr, 0);
}
void com_erpc_core::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);
}
com_erpc_core::com_erpc_core(QWidget *parent)
: QMainWindow(parent), ui(new Ui::com_erpc_core) {
ui->setupUi(this);
serial = nullptr;
load_default_settings();
scan_com = nullptr;
stop_com = nullptr;
start_scan_com_port();
connect(ui->bt_open_com, &QPushButton::clicked, this,
&com_erpc_core::open_com_port);
connect(ui->bt_close_com, &QPushButton::clicked, this,
&com_erpc_core::close_com_port);
connect(ui->bt_read_gain, &QPushButton::clicked, this,
&com_erpc_core::sipm_read_gain);
connect(ui->bt_read_SN, &QPushButton::clicked, this,
&com_erpc_core::sipm_read_SN);
connect(ui->bt_write_gain, &QPushButton::clicked, this,
&com_erpc_core::sipm_set_gain);
connect(ui->bt_read_temp, &QPushButton::clicked, this,
&com_erpc_core::sipm_read_temp);
extern void com_excute_init();
com_excute_init();
}
com_erpc_core::~com_erpc_core() { delete ui; }