2025-03-11T17:31:15

This commit is contained in:
yuntang 2025-03-11 17:31:15 +08:00
parent b6c5cc3d33
commit 7b5bc35e55
16 changed files with 475 additions and 121 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -22,8 +22,8 @@ QT_BEGIN_MOC_NAMESPACE
QT_WARNING_PUSH QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_SerialPort_t { struct qt_meta_stringdata_SerialPort_t {
QByteArrayData data[5]; QByteArrayData data[6];
char stringdata0[58]; char stringdata0[71];
}; };
#define QT_MOC_LITERAL(idx, ofs, len) \ #define QT_MOC_LITERAL(idx, ofs, len) \
Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \ Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \
@ -36,11 +36,12 @@ QT_MOC_LITERAL(0, 0, 10), // "SerialPort"
QT_MOC_LITERAL(1, 11, 21), // "sigSendSerialPortData" QT_MOC_LITERAL(1, 11, 21), // "sigSendSerialPortData"
QT_MOC_LITERAL(2, 33, 0), // "" QT_MOC_LITERAL(2, 33, 0), // ""
QT_MOC_LITERAL(3, 34, 10), // "CommonData" QT_MOC_LITERAL(3, 34, 10), // "CommonData"
QT_MOC_LITERAL(4, 45, 12) // "slotReadData" QT_MOC_LITERAL(4, 45, 12), // "slotSendData"
QT_MOC_LITERAL(5, 58, 12) // "slotReadData"
}, },
"SerialPort\0sigSendSerialPortData\0\0" "SerialPort\0sigSendSerialPortData\0\0"
"CommonData\0slotReadData" "CommonData\0slotSendData\0slotReadData"
}; };
#undef QT_MOC_LITERAL #undef QT_MOC_LITERAL
@ -50,7 +51,7 @@ static const uint qt_meta_data_SerialPort[] = {
8, // revision 8, // revision
0, // classname 0, // classname
0, 0, // classinfo 0, 0, // classinfo
2, 14, // methods 3, 14, // methods
0, 0, // properties 0, 0, // properties
0, 0, // enums/sets 0, 0, // enums/sets
0, 0, // constructors 0, 0, // constructors
@ -58,15 +59,17 @@ static const uint qt_meta_data_SerialPort[] = {
1, // signalCount 1, // signalCount
// signals: name, argc, parameters, tag, flags // signals: name, argc, parameters, tag, flags
1, 1, 24, 2, 0x06 /* Public */, 1, 1, 29, 2, 0x06 /* Public */,
// slots: name, argc, parameters, tag, flags // slots: name, argc, parameters, tag, flags
4, 0, 27, 2, 0x08 /* Private */, 4, 1, 32, 2, 0x0a /* Public */,
5, 0, 35, 2, 0x08 /* Private */,
// signals: parameters // signals: parameters
QMetaType::Void, 0x80000000 | 3, 2, QMetaType::Void, 0x80000000 | 3, 2,
// slots: parameters // slots: parameters
QMetaType::Void, QMetaType::QByteArray, 2,
QMetaType::Void, QMetaType::Void,
0 // eod 0 // eod
@ -79,7 +82,8 @@ void SerialPort::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id,
Q_UNUSED(_t) Q_UNUSED(_t)
switch (_id) { switch (_id) {
case 0: _t->sigSendSerialPortData((*reinterpret_cast< const CommonData(*)>(_a[1]))); break; case 0: _t->sigSendSerialPortData((*reinterpret_cast< const CommonData(*)>(_a[1]))); break;
case 1: _t->slotReadData(); break; case 1: _t->slotSendData((*reinterpret_cast< const QByteArray(*)>(_a[1]))); break;
case 2: _t->slotReadData(); break;
default: ; default: ;
} }
} else if (_c == QMetaObject::IndexOfMethod) { } else if (_c == QMetaObject::IndexOfMethod) {
@ -123,13 +127,13 @@ int SerialPort::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_id < 0) if (_id < 0)
return _id; return _id;
if (_c == QMetaObject::InvokeMetaMethod) { if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 2) if (_id < 3)
qt_static_metacall(this, _c, _id, _a); qt_static_metacall(this, _c, _id, _a);
_id -= 2; _id -= 3;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) { } else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 2) if (_id < 3)
*reinterpret_cast<int*>(_a[0]) = -1; *reinterpret_cast<int*>(_a[0]) = -1;
_id -= 2; _id -= 3;
} }
return _id; return _id;
} }

View File

@ -28,6 +28,7 @@ SOURCES += \
ui/realtimedataform.cpp ui/realtimedataform.cpp
HEADERS += \ HEADERS += \
defines.h \
ui/centralwidget.h \ ui/centralwidget.h \
mainwidget.h \ mainwidget.h \
mainwindow.h \ mainwindow.h \

View File

@ -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.11.1, 2025-03-10T17:17:05. --> <!-- Written by QtCreator 4.11.1, 2025-03-11T08:50:22. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

12
src/defines.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef DEFINES_H
#define DEFINES_H
#include <cstdint>
typedef uint8_t uchar;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
#endif // DEFINES_H

View File

@ -1,3 +1,6 @@
#include <QDebug>
#include <QDataStream>
#include "dataparser.h" #include "dataparser.h"
DataParser::DataParser() DataParser::DataParser()
@ -5,9 +8,69 @@ DataParser::DataParser()
} }
void DataParser::connectToDevice(const QVector<Phase> &vtr) QByteArray DataParser::connectToDevice(const QVector<Phase> &vtr)
{ {
for(const Phase& phase:vtr){ const int phaseLength = sizeof(Phase);
const int phaseArrayLength = vtr.size() * phaseLength;
char phaseArray[300] = {};
int offset = 0;
for(const Phase& phase:vtr){
memcpy(phaseArray + offset,&phase, sizeof(Phase));
offset += phaseLength;
} }
//
// int totalDataLength = 0;
char totalData[300] = {};
u16 header = 0x5aa5;
memcpy(totalData,(char*)&header,sizeof(u16));
// totalDataLength +=2;
totalData[2] = 1 + 2 + phaseArrayLength + 2;
// totalDataLength++;
totalData[3] = 0x82;
// totalDataLength++;
u16 address = 0x0050;
memcpy(totalData + 4,(char*)&address,sizeof(u16));
memcpy(totalData + 6,phaseArray,phaseArrayLength);
//crc
char data[300] = {};
data[0] = 0x82;
// u16 address = 0x0050;
memcpy(data + 1,(char*)&address,sizeof(u16));
memcpy(data + 3,phaseArray,phaseArrayLength);
u16 crc = modbusCRC16((u8*)data, 3 + phaseArrayLength);
//
memcpy(totalData + 6 + phaseArrayLength,(char*)&crc,sizeof(u16));
//
return QByteArray(totalData,6 + phaseArrayLength + 2);
}
unsigned short DataParser::modbusCRC16(unsigned char *data, unsigned short length) {
unsigned short crc = 0xFFFF;
unsigned char i;
for (unsigned short j = 0; j < length; j++) {
crc ^= (unsigned short)data[j];
for (i = 0; i < 8; i++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0xA001;
} else {
crc = crc >> 1;
}
}
}
// CRC结果低字节在前高字节在后
return crc;
} }

View File

@ -2,14 +2,19 @@
#define DATAPARSER_H #define DATAPARSER_H
#include <QVector> #include <QVector>
#include <qbytearray.h>
#include "protocol.h" #include "protocol.h"
#include "defines.h"
class DataParser class DataParser
{ {
public: public:
DataParser(); DataParser();
static void connectToDevice(const QVector<Phase>&);
static QByteArray connectToDevice(const QVector<Phase>&);
static unsigned short modbusCRC16(unsigned char *data,
unsigned short length);
}; };
#endif // DATAPARSER_H #endif // DATAPARSER_H

View File

@ -4,21 +4,23 @@
#include <qdatetime.h> #include <qdatetime.h>
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include <QSerialPortInfo>
#include "protocol.h" #include "protocol.h"
#include "defines.h"
using namespace std; using namespace std;
const QString conSPName = "COM3"; const u16 conVid = 1155; // 0x0483
const QString conPid = "7523"; const u16 conPid = 22336; //0x5740
const QString conVid = "1a86";
SerialPort::SerialPort(QObject*parent) SerialPort::SerialPort(QObject*parent)
:QObject(parent) :QObject(parent)
,_sp(nullptr) ,_sp(nullptr)
{ {
// init(); // displayPortInfo();
#if 1 openSp();
#if 0
// 1.初始化 // 1.初始化
_sp = new QSerialPort(); _sp = new QSerialPort();
_sp->setPortName(conSPName); _sp->setPortName(conSPName);
@ -92,8 +94,9 @@ void SerialPort::slotReadData()
QByteArray data = _sp->readAll(); QByteArray data = _sp->readAll();
// 将 QByteArray 转换为十六进制字符串 // 将 QByteArray 转换为十六进制字符串
// QString hexData = info.toHex(' '); // ' ' 作为分隔符,可选参数 QString hexData = data.toHex(' '); // ' ' 作为分隔符,可选参数
// qDebug() << "receive info (hex):" << hexData; qDebug() << "receive info (hex):" << hexData;
#if 0
CommonData *serialPortData = CommonData *serialPortData =
reinterpret_cast<CommonData*>(const_cast<char*>(&data.data()[6])); reinterpret_cast<CommonData*>(const_cast<char*>(&data.data()[6]));
@ -105,38 +108,51 @@ void SerialPort::slotReadData()
qDebug() << "cold temp:" << formattedColdTemp; qDebug() << "cold temp:" << formattedColdTemp;
emit sigSendSerialPortData(*serialPortData); emit sigSendSerialPortData(*serialPortData);
#endif
} }
void SerialPort::init() void SerialPort::openSp()
{ {
foreach (const QSerialPortInfo &info, foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
QSerialPortInfo::availablePorts())
{ {
#if 0 u16 pid = info.productIdentifier();
qDebug() << "Name : " << info.portName(); u16 vid = info.vendorIdentifier();
qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer();
qDebug() << "Serial Number: " << info.serialNumber();
qDebug() << "System Location: " << info.systemLocation();
qDebug()<<"portName"<< info.portName();
qDebug() <<QString("pid:0x%1").arg(info.productIdentifier(),4,16,QLatin1Char('0'));
qDebug() <<QString("vid:0x%1").arg(info.vendorIdentifier(),4,16,QLatin1Char('0'));
qDebug()<<"";
#endif
QString pid = QString("%1") if((pid == conPid) && (vid == conVid)){
.arg(info.productIdentifier()
,4,16,QLatin1Char('0'));
QString vid = QString("%1")
.arg(info.vendorIdentifier()
,4,16,QLatin1Char('0'));
#if 0
if((pid == conPid)&& (vid == conVid)){
_sp = new QSerialPort(info); _sp = new QSerialPort(info);
break; break;
} }
}
#if 0
if(!_sp->isOpen()){
qDebug()<<"Com open failed.";
return;
}else{
qDebug()<<"Com open succ.";
}
#endif #endif
_sp = new QSerialPort(info);
//设置波特率和读写方向
_sp->setBaudRate(QSerialPort::Baud115200,
QSerialPort::AllDirections);
_sp->setDataBits(QSerialPort::Data8); //数据位为8位
_sp->setFlowControl(QSerialPort::NoFlowControl);//无流控制
_sp->setParity(QSerialPort::NoParity); //无校验位
_sp->setStopBits(QSerialPort::OneStop); //一位停止位
//4.连接信号槽
connect(_sp,&QSerialPort::readyRead,
this,&SerialPort::slotReadData);
// 2.打开串口
if(!_sp->open(QIODevice::ReadWrite)){
qDebug()<<"open failed."<<_sp->error();
exit(0);
}
else{
qDebug()<<"open succ.";
//设置 DTR 信号为就绪状态true 表示低电平)
_sp->setDataTerminalReady(true);
} }
} }
@ -191,6 +207,44 @@ void SerialPort::to_hex(char* in_char, int char_length, char* out_char)
} }
} }
void SerialPort::displayPortInfo()
{
// 获取系统中所有可用的串口信息
QList<QSerialPortInfo> serialPorts = QSerialPortInfo::availablePorts();
// 遍历每个串口信息
for (const QSerialPortInfo &portInfo : serialPorts) {
qDebug()<<"================================================";
// 打印串口的名称
qDebug() << "串口名称: " << portInfo.portName();
// 打印串口的描述信息
qDebug() << "描述信息: " << portInfo.description();
// 打印串口的制造商信息
qDebug() << "制造商: " << portInfo.manufacturer();
// 打印串口的序列号
qDebug() << "序列号: " << portInfo.serialNumber();
// 打印串口的系统位置
qDebug() << "系统位置: " << portInfo.systemLocation();
// 打印是否有虚拟调制解调器
qDebug() << "是否有虚拟调制解调器: " << (portInfo.hasVendorIdentifier() ? "" : "");
// 打印是否有产品标识符
qDebug() << "是否有产品标识符: " << (portInfo.hasProductIdentifier() ? "" : "");
// 如果有虚拟调制解调器,打印其标识符
if (portInfo.hasVendorIdentifier()) {
qint16 vid = portInfo.vendorIdentifier();
qDebug() << "虚拟调制解调器标识符: " << vid;
QString hexStr = QString("0x%1").arg(vid, 4, 16, QChar('0')).toUpper();
qDebug()<<"vid 0x"<< hexStr;
}
// 如果有产品标识符,打印其标识符
if (portInfo.hasProductIdentifier()) {
qint16 pid = portInfo.productIdentifier();
qDebug() << "产品标识符: " << pid;
QString hexStr = QString("0x%1").arg(pid, 4, 16, QChar('0')).toUpper();
qDebug()<<"pid 0x"<< hexStr;
}
}
}
void SerialPort::parserTest() void SerialPort::parserTest()
{ {
const uchar data[] = {0xa5,0x5a,0x2d,0x83, const uchar data[] = {0xa5,0x5a,0x2d,0x83,
@ -233,6 +287,17 @@ void SerialPort::parserTest()
#endif #endif
} }
void SerialPort::slotSendData(const QByteArray &ba)
{
qDebug()<<"slotSendData:"<<ba.size();
_sp->write(ba);
// 将 QByteArray 转换为十六进制字符串
QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数
qDebug() << "send info (hex):" << hexData;
}

View File

@ -23,6 +23,8 @@ public:
void sendCmd(const E_CMD_TYPE e); void sendCmd(const E_CMD_TYPE e);
static void parserTest(); static void parserTest();
public slots:
void slotSendData(const QByteArray&);
signals: signals:
void sigSendSerialPortData(const CommonData&); void sigSendSerialPortData(const CommonData&);
protected: protected:
@ -30,8 +32,9 @@ protected:
private slots: private slots:
void slotReadData(); void slotReadData();
private: private:
void init(); void openSp();
void to_hex(char* in_char, int char_length, char* out_char); void to_hex(char* in_char, int char_length, char* out_char);
void displayPortInfo();
private: private:
QSerialPort *_sp; QSerialPort *_sp;
}; };

View File

@ -3,6 +3,8 @@
#include "experimentsettingform.h" #include "experimentsettingform.h"
#include "ui_experimentsettingform.h" #include "ui_experimentsettingform.h"
#include "protocol.h" #include "protocol.h"
#include "dataparser.h"
#include "serialport.h"
ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) : ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) :
QWidget(parent), QWidget(parent),
@ -18,10 +20,13 @@ ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) :
connect(ui->checkBox_phase_4,&QCheckBox::clicked, connect(ui->checkBox_phase_4,&QCheckBox::clicked,
this,&ExperimentSettingForm::slotPhase4StateChanged); this,&ExperimentSettingForm::slotPhase4StateChanged);
connect(ui->pushButton_connect_to_device,&QPushButton::click, connect(ui->pushButton_connect_to_device,&QPushButton::clicked,
this,&ExperimentSettingForm::slotConnectToDevice); this,&ExperimentSettingForm::slotConnectToDevice);
connect(ui->pushButton_cancel,&QPushButton::click, connect(ui->pushButton_cancel,&QPushButton::clicked,
this,&ExperimentSettingForm::slotCancel); this,&ExperimentSettingForm::slotCancel);
connect(this,&ExperimentSettingForm::sigConnectToDevice,
SerialPort::instance(),&SerialPort::slotSendData);
} }
ExperimentSettingForm::~ExperimentSettingForm() ExperimentSettingForm::~ExperimentSettingForm()
@ -161,7 +166,10 @@ void ExperimentSettingForm::slotConnectToDevice()
phaseVtr.push_back(phase); phaseVtr.push_back(phase);
} }
QByteArray ba = DataParser::connectToDevice(phaseVtr);
qDebug()<<"ba size:"<<ba.size();
emit sigConnectToDevice(ba);
} }
void ExperimentSettingForm::slotCancel() void ExperimentSettingForm::slotCancel()

View File

@ -14,6 +14,9 @@ class ExperimentSettingForm : public QWidget
public: public:
explicit ExperimentSettingForm(QWidget *parent = nullptr); explicit ExperimentSettingForm(QWidget *parent = nullptr);
~ExperimentSettingForm(); ~ExperimentSettingForm();
signals:
void sigConnectToDevice(const QByteArray&);
private: private:
void uiReset(); void uiReset();
//slot //slot