2025-03-11T17:31:15
This commit is contained in:
parent
b6c5cc3d33
commit
7b5bc35e55
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
@ -22,8 +22,8 @@ QT_BEGIN_MOC_NAMESPACE
|
||||
QT_WARNING_PUSH
|
||||
QT_WARNING_DISABLE_DEPRECATED
|
||||
struct qt_meta_stringdata_SerialPort_t {
|
||||
QByteArrayData data[5];
|
||||
char stringdata0[58];
|
||||
QByteArrayData data[6];
|
||||
char stringdata0[71];
|
||||
};
|
||||
#define QT_MOC_LITERAL(idx, ofs, 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(2, 33, 0), // ""
|
||||
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"
|
||||
"CommonData\0slotReadData"
|
||||
"CommonData\0slotSendData\0slotReadData"
|
||||
};
|
||||
#undef QT_MOC_LITERAL
|
||||
|
||||
@ -50,7 +51,7 @@ static const uint qt_meta_data_SerialPort[] = {
|
||||
8, // revision
|
||||
0, // classname
|
||||
0, 0, // classinfo
|
||||
2, 14, // methods
|
||||
3, 14, // methods
|
||||
0, 0, // properties
|
||||
0, 0, // enums/sets
|
||||
0, 0, // constructors
|
||||
@ -58,15 +59,17 @@ static const uint qt_meta_data_SerialPort[] = {
|
||||
1, // signalCount
|
||||
|
||||
// signals: name, argc, parameters, tag, flags
|
||||
1, 1, 24, 2, 0x06 /* Public */,
|
||||
1, 1, 29, 2, 0x06 /* Public */,
|
||||
|
||||
// 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
|
||||
QMetaType::Void, 0x80000000 | 3, 2,
|
||||
|
||||
// slots: parameters
|
||||
QMetaType::Void, QMetaType::QByteArray, 2,
|
||||
QMetaType::Void,
|
||||
|
||||
0 // eod
|
||||
@ -79,7 +82,8 @@ void SerialPort::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id,
|
||||
Q_UNUSED(_t)
|
||||
switch (_id) {
|
||||
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: ;
|
||||
}
|
||||
} else if (_c == QMetaObject::IndexOfMethod) {
|
||||
@ -123,13 +127,13 @@ int SerialPort::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
|
||||
if (_id < 0)
|
||||
return _id;
|
||||
if (_c == QMetaObject::InvokeMetaMethod) {
|
||||
if (_id < 2)
|
||||
if (_id < 3)
|
||||
qt_static_metacall(this, _c, _id, _a);
|
||||
_id -= 2;
|
||||
_id -= 3;
|
||||
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
|
||||
if (_id < 2)
|
||||
if (_id < 3)
|
||||
*reinterpret_cast<int*>(_a[0]) = -1;
|
||||
_id -= 2;
|
||||
_id -= 3;
|
||||
}
|
||||
return _id;
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
@ -28,6 +28,7 @@ SOURCES += \
|
||||
ui/realtimedataform.cpp
|
||||
|
||||
HEADERS += \
|
||||
defines.h \
|
||||
ui/centralwidget.h \
|
||||
mainwidget.h \
|
||||
mainwindow.h \
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!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>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
12
src/defines.h
Normal file
12
src/defines.h
Normal 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
|
@ -1,3 +1,6 @@
|
||||
#include <QDebug>
|
||||
#include <QDataStream>
|
||||
|
||||
#include "dataparser.h"
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -2,14 +2,19 @@
|
||||
#define DATAPARSER_H
|
||||
|
||||
#include <QVector>
|
||||
#include <qbytearray.h>
|
||||
|
||||
#include "protocol.h"
|
||||
#include "defines.h"
|
||||
|
||||
class DataParser
|
||||
{
|
||||
public:
|
||||
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
|
||||
|
@ -4,21 +4,23 @@
|
||||
#include <qdatetime.h>
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <QSerialPortInfo>
|
||||
|
||||
#include "protocol.h"
|
||||
#include "defines.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const QString conSPName = "COM3";
|
||||
const QString conPid = "7523";
|
||||
const QString conVid = "1a86";
|
||||
const u16 conVid = 1155; // 0x0483
|
||||
const u16 conPid = 22336; //0x5740
|
||||
|
||||
SerialPort::SerialPort(QObject*parent)
|
||||
:QObject(parent)
|
||||
,_sp(nullptr)
|
||||
{
|
||||
// init();
|
||||
#if 1
|
||||
// displayPortInfo();
|
||||
openSp();
|
||||
#if 0
|
||||
// 1.初始化
|
||||
_sp = new QSerialPort();
|
||||
_sp->setPortName(conSPName);
|
||||
@ -92,8 +94,9 @@ void SerialPort::slotReadData()
|
||||
QByteArray data = _sp->readAll();
|
||||
|
||||
// 将 QByteArray 转换为十六进制字符串
|
||||
// QString hexData = info.toHex(' '); // ' ' 作为分隔符,可选参数
|
||||
// qDebug() << "receive info (hex):" << hexData;
|
||||
QString hexData = data.toHex(' '); // ' ' 作为分隔符,可选参数
|
||||
qDebug() << "receive info (hex):" << hexData;
|
||||
#if 0
|
||||
CommonData *serialPortData =
|
||||
reinterpret_cast<CommonData*>(const_cast<char*>(&data.data()[6]));
|
||||
|
||||
@ -105,38 +108,51 @@ void SerialPort::slotReadData()
|
||||
qDebug() << "cold temp:" << formattedColdTemp;
|
||||
|
||||
emit sigSendSerialPortData(*serialPortData);
|
||||
#endif
|
||||
}
|
||||
|
||||
void SerialPort::init()
|
||||
void SerialPort::openSp()
|
||||
{
|
||||
foreach (const QSerialPortInfo &info,
|
||||
QSerialPortInfo::availablePorts())
|
||||
foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
|
||||
{
|
||||
#if 0
|
||||
qDebug() << "Name : " << info.portName();
|
||||
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
|
||||
u16 pid = info.productIdentifier();
|
||||
u16 vid = info.vendorIdentifier();
|
||||
|
||||
QString pid = QString("%1")
|
||||
.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)){
|
||||
if((pid == conPid) && (vid == conVid)){
|
||||
_sp = new QSerialPort(info);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#if 0
|
||||
if(!_sp->isOpen()){
|
||||
qDebug()<<"Com open failed.";
|
||||
return;
|
||||
}else{
|
||||
qDebug()<<"Com open succ.";
|
||||
}
|
||||
#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()
|
||||
{
|
||||
const uchar data[] = {0xa5,0x5a,0x2d,0x83,
|
||||
@ -233,6 +287,17 @@ void SerialPort::parserTest()
|
||||
#endif
|
||||
}
|
||||
|
||||
void SerialPort::slotSendData(const QByteArray &ba)
|
||||
{
|
||||
qDebug()<<"slotSendData:"<<ba.size();
|
||||
|
||||
_sp->write(ba);
|
||||
|
||||
// 将 QByteArray 转换为十六进制字符串
|
||||
QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数
|
||||
qDebug() << "send info (hex):" << hexData;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -23,6 +23,8 @@ public:
|
||||
|
||||
void sendCmd(const E_CMD_TYPE e);
|
||||
static void parserTest();
|
||||
public slots:
|
||||
void slotSendData(const QByteArray&);
|
||||
signals:
|
||||
void sigSendSerialPortData(const CommonData&);
|
||||
protected:
|
||||
@ -30,8 +32,9 @@ protected:
|
||||
private slots:
|
||||
void slotReadData();
|
||||
private:
|
||||
void init();
|
||||
void openSp();
|
||||
void to_hex(char* in_char, int char_length, char* out_char);
|
||||
void displayPortInfo();
|
||||
private:
|
||||
QSerialPort *_sp;
|
||||
};
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include "experimentsettingform.h"
|
||||
#include "ui_experimentsettingform.h"
|
||||
#include "protocol.h"
|
||||
#include "dataparser.h"
|
||||
#include "serialport.h"
|
||||
|
||||
ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
@ -18,10 +20,13 @@ ExperimentSettingForm::ExperimentSettingForm(QWidget *parent) :
|
||||
connect(ui->checkBox_phase_4,&QCheckBox::clicked,
|
||||
this,&ExperimentSettingForm::slotPhase4StateChanged);
|
||||
|
||||
connect(ui->pushButton_connect_to_device,&QPushButton::click,
|
||||
connect(ui->pushButton_connect_to_device,&QPushButton::clicked,
|
||||
this,&ExperimentSettingForm::slotConnectToDevice);
|
||||
connect(ui->pushButton_cancel,&QPushButton::click,
|
||||
connect(ui->pushButton_cancel,&QPushButton::clicked,
|
||||
this,&ExperimentSettingForm::slotCancel);
|
||||
|
||||
connect(this,&ExperimentSettingForm::sigConnectToDevice,
|
||||
SerialPort::instance(),&SerialPort::slotSendData);
|
||||
}
|
||||
|
||||
ExperimentSettingForm::~ExperimentSettingForm()
|
||||
@ -161,7 +166,10 @@ void ExperimentSettingForm::slotConnectToDevice()
|
||||
phaseVtr.push_back(phase);
|
||||
}
|
||||
|
||||
QByteArray ba = DataParser::connectToDevice(phaseVtr);
|
||||
qDebug()<<"ba size:"<<ba.size();
|
||||
|
||||
emit sigConnectToDevice(ba);
|
||||
}
|
||||
|
||||
void ExperimentSettingForm::slotCancel()
|
||||
|
@ -14,6 +14,9 @@ class ExperimentSettingForm : public QWidget
|
||||
public:
|
||||
explicit ExperimentSettingForm(QWidget *parent = nullptr);
|
||||
~ExperimentSettingForm();
|
||||
|
||||
signals:
|
||||
void sigConnectToDevice(const QByteArray&);
|
||||
private:
|
||||
void uiReset();
|
||||
//slot
|
||||
|
Loading…
Reference in New Issue
Block a user