ok,可以简单修改出厂配置
This commit is contained in:
parent
bbd47b3486
commit
98549e640c
1
main.cpp
1
main.cpp
|
@ -1,6 +1,7 @@
|
||||||
#include "widget.h"
|
#include "widget.h"
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QLineEdit>
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE QtCreatorProject>
|
<!DOCTYPE QtCreatorProject>
|
||||||
<!-- Written by QtCreator 4.12.2, 2024-11-29T17:17:45. -->
|
<!-- Written by QtCreator 4.12.2, 2024-12-18T09:05:17. -->
|
||||||
<qtcreator>
|
<qtcreator>
|
||||||
<data>
|
<data>
|
||||||
<variable>EnvironmentId</variable>
|
<variable>EnvironmentId</variable>
|
||||||
|
|
445
widget.cpp
445
widget.cpp
|
@ -1,11 +1,56 @@
|
||||||
#include "widget.h"
|
#include "widget.h"
|
||||||
#include "ui_widget.h"
|
#include "ui_widget.h"
|
||||||
|
|
||||||
|
#define UART_WAIT_MS 50
|
||||||
|
|
||||||
Widget::Widget(QWidget *parent)
|
Widget::Widget(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
, ui(new Ui::Widget)
|
, ui(new Ui::Widget)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
// 启动时关闭按钮
|
||||||
|
disable__();
|
||||||
|
// 限制输入范围
|
||||||
|
QLineEdit *edit_1R5 = ui -> write_1R5_lineEdit;
|
||||||
|
QLineEdit *edit_4R5 = ui -> write_4R5_lineEdit;
|
||||||
|
QLineEdit *edit_8R0 = ui -> write_8R0_lineEdit;
|
||||||
|
QLineEdit *rssi = ui -> write_lineEdit;
|
||||||
|
QLineEdit *coefficient = ui -> write_Coefficient_lineEdit;
|
||||||
|
QLineEdit *edit_ns = ui -> write_distance_NS_lineEdit;
|
||||||
|
QLineEdit *edit_we = ui -> write_distance_WE_lineEdit;
|
||||||
|
// 创建正则表达式对象
|
||||||
|
QRegExp rx_0_100("^([0-9]|[1-9][0-9]|100)$");
|
||||||
|
QRegExp rx_x_xxxx("^([0-9])(\\.\\d{4})$");
|
||||||
|
QRegExp rx__xx("^\\d+\\.\\d{2}$");
|
||||||
|
// 创建 QRegExpValidator 对象
|
||||||
|
QRegExpValidator *pReg_0_100 = new QRegExpValidator(rx_0_100, this);
|
||||||
|
QRegExpValidator *pReg_x_xxxx = new QRegExpValidator(rx_x_xxxx, this);
|
||||||
|
QRegExpValidator *pReg__xx = new QRegExpValidator(rx__xx, this);
|
||||||
|
edit_1R5->setValidator(pReg_0_100);
|
||||||
|
edit_4R5->setValidator(pReg_0_100);
|
||||||
|
edit_8R0->setValidator(pReg_0_100);
|
||||||
|
rssi->setValidator(pReg_0_100);
|
||||||
|
coefficient->setValidator(pReg_x_xxxx);
|
||||||
|
edit_ns->setValidator(pReg__xx);
|
||||||
|
edit_we->setValidator(pReg__xx);
|
||||||
|
|
||||||
|
error1_status_label[0] = ui -> error1_status_label;
|
||||||
|
error1_status_label[1] = ui -> error2_status_label;
|
||||||
|
error1_status_label[2] = ui -> error3_status_label;
|
||||||
|
error1_status_label[3] = ui -> error4_status_label;
|
||||||
|
error1_status_label[4] = ui -> error5_status_label;
|
||||||
|
error1_status_label[5] = ui -> error6_status_label;
|
||||||
|
error1_status_label[6] = ui -> error7_status_label;
|
||||||
|
error1_status_label[7] = ui -> error8_status_label;
|
||||||
|
error1_status_label[8] = ui -> error9_status_label;
|
||||||
|
error1_status_label[9] = ui -> error10_status_label;
|
||||||
|
error1_status_label[10] = ui -> error11_status_label;
|
||||||
|
error1_status_label[11] = ui -> error12_status_label;
|
||||||
|
error1_status_label[12] = ui -> error13_status_label;
|
||||||
|
error1_status_label[13] = ui -> error14_status_label;
|
||||||
|
error1_status_label[14] = ui -> error15_status_label;
|
||||||
|
error1_status_label[15] = ui -> error16_status_label;
|
||||||
|
|
||||||
// 启动时刷新串口
|
// 启动时刷新串口
|
||||||
QStringList serialNamePort;
|
QStringList serialNamePort;
|
||||||
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
|
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
|
||||||
|
@ -41,6 +86,35 @@ void Widget::closeEvent(QCloseEvent *event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Widget::disable__()
|
||||||
|
{
|
||||||
|
ui->groupBox->setEnabled(false);
|
||||||
|
ui->groupBox_2->setEnabled(false);
|
||||||
|
ui->groupBox_3->setEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Widget::able__()
|
||||||
|
{
|
||||||
|
ui->groupBox->setEnabled(true);
|
||||||
|
ui->groupBox_2->setEnabled(true);
|
||||||
|
ui->groupBox_3->setEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t Widget::modbusCrc16(const uint8_t *data, const int len)
|
||||||
|
{
|
||||||
|
uint16_t crc = 0xFFFF;
|
||||||
|
for (int pos = 0; pos < len; pos++) {
|
||||||
|
crc ^= *data++;
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
if ((crc & 0x0001) > 0)
|
||||||
|
crc = (crc >> 1) ^ 0xA001;
|
||||||
|
else
|
||||||
|
crc = crc >> 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (crc<<8&0xFF00)|(crc>>8&0x00FF);
|
||||||
|
}
|
||||||
|
|
||||||
//串口刷新
|
//串口刷新
|
||||||
void Widget::on_SerialPort_refresh_pushButton_clicked()
|
void Widget::on_SerialPort_refresh_pushButton_clicked()
|
||||||
{
|
{
|
||||||
|
@ -163,6 +237,10 @@ void Widget::on_Open_Serial_pushButton_clicked()
|
||||||
ui -> DataBits_label -> setEnabled(false);
|
ui -> DataBits_label -> setEnabled(false);
|
||||||
ui -> CheckBits_label -> setEnabled(false);
|
ui -> CheckBits_label -> setEnabled(false);
|
||||||
ui -> StopBits_label -> setEnabled(false);
|
ui -> StopBits_label -> setEnabled(false);
|
||||||
|
// 串口打开后使能功能
|
||||||
|
able__();
|
||||||
|
|
||||||
|
open_button_status = !open_button_status;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
QMessageBox::critical(this, "提示", "串口打开失败");
|
QMessageBox::critical(this, "提示", "串口打开失败");
|
||||||
|
@ -185,6 +263,371 @@ void Widget::on_Open_Serial_pushButton_clicked()
|
||||||
ui -> DataBits_label -> setEnabled(true);
|
ui -> DataBits_label -> setEnabled(true);
|
||||||
ui -> CheckBits_label -> setEnabled(true);
|
ui -> CheckBits_label -> setEnabled(true);
|
||||||
ui -> StopBits_label -> setEnabled(true);
|
ui -> StopBits_label -> setEnabled(true);
|
||||||
}
|
// 串口关闭后取消使能功能
|
||||||
|
disable__();
|
||||||
|
|
||||||
open_button_status = !open_button_status;
|
open_button_status = !open_button_status;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//读取按钮
|
||||||
|
void Widget::on_get_data_pushButton_clicked()
|
||||||
|
{
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x03));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x1E));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x0A));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
|
||||||
|
//连接信号与槽
|
||||||
|
//断开所有其他的信号与槽连接
|
||||||
|
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||||
|
connect(Serial_port, SIGNAL(readyRead()), this, SLOT(get_configdata()), Qt::UniqueConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::array<bool, 16> getBits(unsigned short num) {
|
||||||
|
std::array<bool, 16> bits;
|
||||||
|
for (int i = 15; i >= 0; --i) {
|
||||||
|
bits[15 - i] = ((num >> i) & 1);
|
||||||
|
}
|
||||||
|
return bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Widget::get_configdata()
|
||||||
|
{
|
||||||
|
// 接收数据,一会再接收,以便接收超长数据
|
||||||
|
QByteArray readed_data;
|
||||||
|
while(Serial_port->waitForReadyRead(UART_WAIT_MS)==true);
|
||||||
|
readed_data.append(Serial_port -> readAll());
|
||||||
|
|
||||||
|
//防止干扰
|
||||||
|
while(readed_data.length() > 1)
|
||||||
|
{
|
||||||
|
qDebug() << readed_data.length();
|
||||||
|
qDebug() << readed_data;
|
||||||
|
if(readed_data[0] != static_cast<char>(0x30) || readed_data[1] != static_cast<char>(0x03))
|
||||||
|
{
|
||||||
|
readed_data.remove(0, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t read_comand_u8[readed_data.length()];
|
||||||
|
for(int i = 0; i < readed_data.length(); i++)
|
||||||
|
{
|
||||||
|
read_comand_u8[i] = readed_data[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(readed_data.length() >= 25)
|
||||||
|
if(modbusCrc16(read_comand_u8, 23) >> 8 == read_comand_u8[23] && (modbusCrc16(read_comand_u8, 23) & 0x00FF) == read_comand_u8[24])
|
||||||
|
{
|
||||||
|
qDebug() << "CRC16校验成功";
|
||||||
|
uint16_t config_1R5 = read_comand_u8[3] << 8 | read_comand_u8[4];
|
||||||
|
uint16_t config_4R5 = read_comand_u8[5] << 8 | read_comand_u8[6];
|
||||||
|
uint16_t config_8R0 = read_comand_u8[7] << 8 | read_comand_u8[8];
|
||||||
|
uint16_t rssi = read_comand_u8[9] << 8 | read_comand_u8[10];
|
||||||
|
uint16_t coefficient = read_comand_u8[11] <<8 | read_comand_u8[12];
|
||||||
|
uint16_t ns_distance = read_comand_u8[13] << 8 | read_comand_u8[14];
|
||||||
|
uint16_t we_distance = read_comand_u8[15] << 8 | read_comand_u8[16];
|
||||||
|
uint16_t errorlog = read_comand_u8[17] << 8 | read_comand_u8[18];
|
||||||
|
uint16_t ns_c = read_comand_u8[19] << 8 | read_comand_u8[20];
|
||||||
|
uint16_t we_c = read_comand_u8[21] << 8 | read_comand_u8[22];
|
||||||
|
|
||||||
|
qDebug() << "config_1R5:" << config_1R5;
|
||||||
|
qDebug() << "config_4R5:" << config_4R5;
|
||||||
|
qDebug() << "config_8R0:" << config_8R0;
|
||||||
|
qDebug() << "rssi:" << rssi;
|
||||||
|
qDebug() << "coefficient:" << coefficient;
|
||||||
|
qDebug() << "ns_distance:" << ns_distance;
|
||||||
|
qDebug() << "we_distance:" << we_distance;
|
||||||
|
qDebug() << "errorlog:" << errorlog;
|
||||||
|
qDebug() << "ns_c:" << ns_c;
|
||||||
|
qDebug() << "we_c:" << we_c;
|
||||||
|
|
||||||
|
ui -> config_1R5_lineEdit -> setText(QString::number(config_1R5));
|
||||||
|
ui -> config_4R5_lineEdit -> setText(QString::number(config_4R5));
|
||||||
|
ui -> config_8R0_lineEdit -> setText(QString::number(config_8R0));
|
||||||
|
ui -> RSSI_lineEdit -> setText(QString::number((float)rssi/100.0));
|
||||||
|
ui -> Coefficient_lineEdit ->setText(QString::number((float)coefficient/10000.0));
|
||||||
|
ui -> distance_NS_lineEdit -> setText(QString::number((float)ns_distance/500.0) + "mm");
|
||||||
|
ui -> distance_WE_lineEdit -> setText(QString::number((float)we_distance/500.0) + "mm");
|
||||||
|
ui -> c_NS_lineEdit -> setText(QString::number((float)ns_c/10.0) + "m/s");
|
||||||
|
ui -> c_WE_lineEdit -> setText(QString::number((float)we_c/10.0) + "m/s");
|
||||||
|
|
||||||
|
std::array<bool, 16> bits = getBits(errorlog);
|
||||||
|
for (int i = 15; i >= 0; --i)
|
||||||
|
{
|
||||||
|
qDebug() << bits[i];
|
||||||
|
bits[i] ? error1_status_label[15 - i] -> setStyleSheet("QLabel { background-color :rgb(184, 0, 0); border-radius: 10px;}") \
|
||||||
|
: error1_status_label[15 - i] -> setStyleSheet("QLabel { background-color :rgb(0, 144, 0); border-radius: 10px;}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
disconnect(Serial_port, &QSerialPort::readyRead, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//30->1E
|
||||||
|
void Widget::on_pushButton_clicked()
|
||||||
|
{
|
||||||
|
QString text = ui -> write_1R5_lineEdit -> text();
|
||||||
|
bool ok;
|
||||||
|
double data = text.toDouble(&ok);
|
||||||
|
qDebug() << data;
|
||||||
|
uint16_t data_u16 = (uint16_t)data;
|
||||||
|
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x1E));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 >> 8));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 & 0x00FF));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
}
|
||||||
|
|
||||||
|
//31->1F
|
||||||
|
void Widget::on_pushButton_2_clicked()
|
||||||
|
{
|
||||||
|
QString text = ui -> write_4R5_lineEdit -> text();
|
||||||
|
bool ok;
|
||||||
|
double data = text.toDouble(&ok);
|
||||||
|
qDebug() << data;
|
||||||
|
uint16_t data_u16 = (uint16_t)data;
|
||||||
|
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x1F));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 >> 8));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 & 0x00FF));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
}
|
||||||
|
|
||||||
|
//32->20
|
||||||
|
void Widget::on_pushButton_3_clicked()
|
||||||
|
{
|
||||||
|
QString text = ui -> write_8R0_lineEdit -> text();
|
||||||
|
bool ok;
|
||||||
|
double data = text.toDouble(&ok);
|
||||||
|
qDebug() << data;
|
||||||
|
uint16_t data_u16 = (uint16_t)data;
|
||||||
|
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x20));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 >> 8));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 & 0x00FF));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
}
|
||||||
|
|
||||||
|
//33->21
|
||||||
|
void Widget::on_pushButton_4_clicked()
|
||||||
|
{
|
||||||
|
QString text = ui -> write_lineEdit -> text();
|
||||||
|
bool ok;
|
||||||
|
double data = text.toDouble(&ok);
|
||||||
|
qDebug() << data;
|
||||||
|
uint16_t data_u16 = (uint16_t)data;
|
||||||
|
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x21));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 >> 8));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 & 0x00FF));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
}
|
||||||
|
|
||||||
|
//34->22
|
||||||
|
void Widget::on_pushButton_5_clicked()
|
||||||
|
{
|
||||||
|
QString text = ui -> write_Coefficient_lineEdit -> text();
|
||||||
|
bool ok;
|
||||||
|
double data = text.toDouble(&ok);
|
||||||
|
data *= 10000;
|
||||||
|
qDebug() << data;
|
||||||
|
uint16_t data_u16 = (uint16_t)data;
|
||||||
|
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x22));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 >> 8));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 & 0x00FF));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
}
|
||||||
|
|
||||||
|
//35->23
|
||||||
|
void Widget::on_pushButton_6_clicked()
|
||||||
|
{
|
||||||
|
QString text = ui -> write_distance_NS_lineEdit -> text();
|
||||||
|
bool ok;
|
||||||
|
double data = text.toDouble(&ok);
|
||||||
|
data *= 500;
|
||||||
|
qDebug() << data;
|
||||||
|
uint16_t data_u16 = (uint16_t)data;
|
||||||
|
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x23));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 >> 8));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 & 0x00FF));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
}
|
||||||
|
|
||||||
|
//36->24
|
||||||
|
void Widget::on_pushButton_7_clicked()
|
||||||
|
{
|
||||||
|
QString text = ui -> write_distance_WE_lineEdit -> text();
|
||||||
|
bool ok;
|
||||||
|
double data = text.toDouble(&ok);
|
||||||
|
data *= 500;
|
||||||
|
qDebug() << data;
|
||||||
|
uint16_t data_u16 = (uint16_t)data;
|
||||||
|
|
||||||
|
QByteArray sent_comand;
|
||||||
|
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x30));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x10));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x24));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x00));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x01));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(0x02));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 >> 8));
|
||||||
|
sent_comand.append(static_cast<uint8_t>(data_u16 & 0x00FF));
|
||||||
|
|
||||||
|
uint8_t sent_comand_u8[sent_comand.length()];
|
||||||
|
for(int i = 0; i < sent_comand.length(); i++)
|
||||||
|
{
|
||||||
|
sent_comand_u8[i] = sent_comand[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t crc_16_val = modbusCrc16(sent_comand_u8, sent_comand.length());
|
||||||
|
|
||||||
|
sent_comand.append(crc_16_val >> 8);
|
||||||
|
sent_comand.append(crc_16_val);
|
||||||
|
|
||||||
|
Serial_port->write(sent_comand);
|
||||||
|
}
|
||||||
|
|
30
widget.h
30
widget.h
|
@ -7,6 +7,9 @@
|
||||||
#include <QSerialPort>
|
#include <QSerialPort>
|
||||||
#include <QSerialPortInfo>
|
#include <QSerialPortInfo>
|
||||||
#include <QCloseEvent>
|
#include <QCloseEvent>
|
||||||
|
#include <QRegExp>
|
||||||
|
#include <QRegExpValidator>
|
||||||
|
#include <QLabel>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui { class Widget; }
|
namespace Ui { class Widget; }
|
||||||
|
@ -25,12 +28,39 @@ private slots:
|
||||||
|
|
||||||
void on_Open_Serial_pushButton_clicked();
|
void on_Open_Serial_pushButton_clicked();
|
||||||
|
|
||||||
|
void on_get_data_pushButton_clicked();
|
||||||
|
|
||||||
|
//槽函数
|
||||||
|
void get_configdata(void);
|
||||||
|
|
||||||
|
void on_pushButton_clicked();
|
||||||
|
|
||||||
|
void on_pushButton_2_clicked();
|
||||||
|
|
||||||
|
void on_pushButton_3_clicked();
|
||||||
|
|
||||||
|
void on_pushButton_4_clicked();
|
||||||
|
|
||||||
|
void on_pushButton_5_clicked();
|
||||||
|
|
||||||
|
void on_pushButton_6_clicked();
|
||||||
|
|
||||||
|
void on_pushButton_7_clicked();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::Widget *ui;
|
Ui::Widget *ui;
|
||||||
//关闭窗口提示
|
//关闭窗口提示
|
||||||
void closeEvent(QCloseEvent *event) override;
|
void closeEvent(QCloseEvent *event) override;
|
||||||
|
//关闭控件
|
||||||
|
void disable__(void);
|
||||||
|
//打开控件
|
||||||
|
void able__(void);
|
||||||
|
//crc校验
|
||||||
|
uint16_t modbusCrc16(const uint8_t *data, const int len);
|
||||||
//串口
|
//串口
|
||||||
QSerialPort * Serial_port;
|
QSerialPort * Serial_port;
|
||||||
|
|
||||||
|
QLabel *error1_status_label[16];
|
||||||
|
|
||||||
};
|
};
|
||||||
#endif // WIDGET_H
|
#endif // WIDGET_H
|
||||||
|
|
Loading…
Reference in New Issue