2025-03-05 09:33:46 +00:00
|
|
|
|
#include "serialport.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <qdebug.h>
|
|
|
|
|
|
#include <qdatetime.h>
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
#include <cstring>
|
2025-03-11 09:31:15 +00:00
|
|
|
|
#include <QSerialPortInfo>
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
|
|
|
|
|
#include "protocol.h"
|
2025-03-11 09:31:15 +00:00
|
|
|
|
#include "defines.h"
|
2025-03-12 09:29:52 +00:00
|
|
|
|
#include "dataparser.h"
|
2025-03-13 09:27:31 +00:00
|
|
|
|
#include "global.h"
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
const u16 conVid = 1155; // 0x0483
|
|
|
|
|
|
const u16 conPid = 22336; // 0x5740
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
SerialPort::SerialPort(QObject *parent)
|
|
|
|
|
|
: QObject(parent), _sp(nullptr)
|
2025-03-05 09:33:46 +00:00
|
|
|
|
{
|
2025-03-11 09:31:15 +00:00
|
|
|
|
// displayPortInfo();
|
2025-03-12 09:29:52 +00:00
|
|
|
|
|
2025-03-11 09:31:15 +00:00
|
|
|
|
#if 0
|
2025-03-05 09:33:46 +00:00
|
|
|
|
// 1.初始化
|
|
|
|
|
|
_sp = new QSerialPort();
|
|
|
|
|
|
_sp->setPortName(conSPName);
|
|
|
|
|
|
|
|
|
|
|
|
if(_sp == nullptr){
|
|
|
|
|
|
qDebug()<<"sp is null.";
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 3.设置
|
|
|
|
|
|
//设置波特率和读写方向
|
|
|
|
|
|
_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(false == _sp->open(QIODevice::ReadWrite)){
|
|
|
|
|
|
qDebug()<<"open failed."
|
|
|
|
|
|
<<_sp->error();
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else{
|
|
|
|
|
|
qDebug()<<"open succ.";
|
|
|
|
|
|
|
|
|
|
|
|
// 设置 DTR 信号为就绪状态(true 表示低电平)
|
|
|
|
|
|
_sp->setDataTerminalReady(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//写数据定时器
|
|
|
|
|
|
// startTimer(500);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
sendCmd(e_zero);
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < 9; i++){
|
|
|
|
|
|
sendCmd(e_back);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SerialPort *SerialPort::instance()
|
|
|
|
|
|
{
|
|
|
|
|
|
static SerialPort ins;
|
|
|
|
|
|
return &ins;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SerialPort::~SerialPort()
|
|
|
|
|
|
{
|
2025-03-12 09:29:52 +00:00
|
|
|
|
if (_sp->isOpen())
|
|
|
|
|
|
{
|
2025-03-05 09:33:46 +00:00
|
|
|
|
_sp->clear();
|
|
|
|
|
|
_sp->close();
|
|
|
|
|
|
}
|
|
|
|
|
|
delete _sp;
|
2025-03-12 09:29:52 +00:00
|
|
|
|
_sp = nullptr;
|
2025-03-05 09:33:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerialPort::timerEvent(QTimerEvent *event)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerialPort::slotReadData()
|
|
|
|
|
|
{
|
2025-03-14 08:58:08 +00:00
|
|
|
|
#if 1
|
2025-03-12 09:29:52 +00:00
|
|
|
|
QByteArray ba = _sp->readAll();
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
2025-03-13 09:27:31 +00:00
|
|
|
|
#if 0
|
2025-03-12 09:29:52 +00:00
|
|
|
|
QString hexData = ba.toHex(' '); // ' ' 作为分隔符,可选参数
|
2025-03-11 09:31:15 +00:00
|
|
|
|
qDebug() << "receive info (hex):" << hexData;
|
2025-03-12 09:29:52 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-03-17 09:30:54 +00:00
|
|
|
|
SerialPortProtocol *spp = (SerialPortProtocol *)ba.data();
|
|
|
|
|
|
// qDebug()<<"header:"<<QString::number(spp->head,16).toUpper();
|
|
|
|
|
|
if (FRANE_HEAD != spp->head)
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "Data header error.";
|
2025-03-14 08:58:08 +00:00
|
|
|
|
return;
|
2025-03-12 09:29:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-17 09:30:54 +00:00
|
|
|
|
int dataLength = spp->len - 5;
|
2025-03-12 09:29:52 +00:00
|
|
|
|
|
2025-03-14 08:58:08 +00:00
|
|
|
|
CommonData cd;
|
2025-03-17 09:30:54 +00:00
|
|
|
|
u8 *cdPtr = (u8 *)&cd;
|
|
|
|
|
|
memcpy(cdPtr + spp->addr, spp->data_buf, dataLength);
|
2025-03-12 09:29:52 +00:00
|
|
|
|
|
2025-03-17 09:30:54 +00:00
|
|
|
|
if (WRITE_CMD == spp->cmd)
|
|
|
|
|
|
{
|
|
|
|
|
|
writeCmdHandle(dataLength, spp->addr, cd);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Global::Mode::ExperimentStart == Global::instance()->getMode())
|
|
|
|
|
|
{
|
2025-03-14 08:58:08 +00:00
|
|
|
|
emit sigSendCommonData(cd);
|
|
|
|
|
|
}
|
|
|
|
|
|
emit sigSendCommonDataToRealDataForm(cd);
|
2025-03-13 09:27:31 +00:00
|
|
|
|
}
|
2025-03-14 08:58:08 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
}
|
2025-03-12 09:29:52 +00:00
|
|
|
|
|
2025-03-17 09:30:54 +00:00
|
|
|
|
void SerialPort::writeCmdHandle(const int dataLength, const u16 addr,
|
|
|
|
|
|
const CommonData &cd)
|
2025-03-14 08:58:08 +00:00
|
|
|
|
{
|
2025-03-17 09:30:54 +00:00
|
|
|
|
int localLength = dataLength;
|
|
|
|
|
|
int localAddr = addr;
|
|
|
|
|
|
|
|
|
|
|
|
int phaseByteSize = sizeof(Phase);
|
|
|
|
|
|
auto phaseParserFunc = [&](const int index){
|
|
|
|
|
|
Phase phase;
|
|
|
|
|
|
phase.onoff = 1;
|
|
|
|
|
|
phase.gas = cd.phase_data[index].gas;
|
|
|
|
|
|
phase.temp_flow = cd.phase_data[index].temp_flow;
|
|
|
|
|
|
phase.cutoff_temp = cd.phase_data[index].cutoff_temp;
|
|
|
|
|
|
phase.constant_temp_time_min =
|
|
|
|
|
|
cd.phase_data[index].constant_temp_time_min;
|
|
|
|
|
|
|
|
|
|
|
|
localLength -= phaseByteSize;
|
|
|
|
|
|
localAddr += phaseByteSize;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
while (localLength)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (localAddr)
|
|
|
|
|
|
{
|
|
|
|
|
|
case offsetof(CommonData, current_gas): //当前气氛
|
|
|
|
|
|
// gas_type_set(dev->temp, msg_data.current_gas);
|
|
|
|
|
|
localAddr += 1;
|
|
|
|
|
|
localLength -= 1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case offsetof(CommonData, auto_pid_temp_flow): //自整定升温速率
|
|
|
|
|
|
localAddr += 4;
|
|
|
|
|
|
localLength -= 4;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case offsetof(CommonData, run_mode):
|
|
|
|
|
|
{
|
|
|
|
|
|
DeviceStartMode mode = (DeviceStartMode)cd.run_mode;
|
|
|
|
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DeviceStartMode::Stop:
|
|
|
|
|
|
Global::instance()->setMode(Global::Mode::Analysis);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DeviceStartMode::Start:
|
|
|
|
|
|
Global::instance()->setMode(Global::Mode::ExperimentStart);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
//
|
|
|
|
|
|
localLength--;
|
|
|
|
|
|
localAddr++;
|
2025-03-14 08:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
2025-03-17 09:30:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
case offsetof(CommonData, phase_data[0].onoff):
|
|
|
|
|
|
phaseParserFunc(0); break;
|
|
|
|
|
|
case offsetof(CommonData, phase_data[1].onoff):
|
|
|
|
|
|
phaseParserFunc(1); break;
|
|
|
|
|
|
case offsetof(CommonData, phase_data[2].onoff):
|
|
|
|
|
|
phaseParserFunc(2); break;
|
|
|
|
|
|
case offsetof(CommonData, phase_data[3].onoff):
|
|
|
|
|
|
phaseParserFunc(3); break;
|
|
|
|
|
|
case offsetof(CommonData, phase_data[4].onoff):
|
|
|
|
|
|
phaseParserFunc(4); break;
|
|
|
|
|
|
case offsetof(CommonData, phase_data[5].onoff):
|
|
|
|
|
|
phaseParserFunc(5); break;
|
|
|
|
|
|
default:break;
|
2025-03-14 08:58:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
}
|
2025-03-05 09:33:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-13 09:27:31 +00:00
|
|
|
|
bool SerialPort::openSp()
|
2025-03-05 09:33:46 +00:00
|
|
|
|
{
|
2025-03-17 09:30:54 +00:00
|
|
|
|
if (_sp != nullptr && _sp->isOpen())
|
|
|
|
|
|
{
|
2025-03-13 09:27:31 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
|
2025-03-05 09:33:46 +00:00
|
|
|
|
{
|
2025-03-11 09:31:15 +00:00
|
|
|
|
u16 pid = info.productIdentifier();
|
|
|
|
|
|
u16 vid = info.vendorIdentifier();
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
if ((pid == conPid) && (vid == conVid))
|
|
|
|
|
|
{
|
2025-03-05 09:33:46 +00:00
|
|
|
|
_sp = new QSerialPort(info);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-03-11 09:31:15 +00:00
|
|
|
|
}
|
2025-03-12 09:29:52 +00:00
|
|
|
|
|
|
|
|
|
|
if (_sp == nullptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "Device not found.";
|
2025-03-13 09:27:31 +00:00
|
|
|
|
return false;
|
2025-03-11 09:31:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
// 设置波特率和读写方向
|
2025-03-11 09:31:15 +00:00
|
|
|
|
_sp->setBaudRate(QSerialPort::Baud115200,
|
|
|
|
|
|
QSerialPort::AllDirections);
|
2025-03-12 09:29:52 +00:00
|
|
|
|
_sp->setDataBits(QSerialPort::Data8); // 数据位为8位
|
|
|
|
|
|
_sp->setFlowControl(QSerialPort::NoFlowControl); // 无流控制
|
|
|
|
|
|
_sp->setParity(QSerialPort::NoParity); // 无校验位
|
|
|
|
|
|
_sp->setStopBits(QSerialPort::OneStop); // 一位停止位
|
2025-03-11 09:31:15 +00:00
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
// 4.连接信号槽
|
|
|
|
|
|
connect(_sp, &QSerialPort::readyRead,
|
|
|
|
|
|
this, &SerialPort::slotReadData);
|
2025-03-11 09:31:15 +00:00
|
|
|
|
|
|
|
|
|
|
// 2.打开串口
|
2025-03-12 09:29:52 +00:00
|
|
|
|
if (!_sp->open(QIODevice::ReadWrite))
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "open failed." << _sp->error();
|
2025-03-13 09:27:31 +00:00
|
|
|
|
return false;
|
2025-03-11 09:31:15 +00:00
|
|
|
|
}
|
2025-03-12 09:29:52 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "open succ.";
|
|
|
|
|
|
// 设置 DTR 信号为就绪状态(true 表示低电平)
|
2025-03-11 09:31:15 +00:00
|
|
|
|
_sp->setDataTerminalReady(true);
|
2025-03-05 09:33:46 +00:00
|
|
|
|
}
|
2025-03-13 09:27:31 +00:00
|
|
|
|
return true;
|
2025-03-05 09:33:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SerialPort::sendCmd(const SerialPort::E_CMD_TYPE e)
|
|
|
|
|
|
{
|
|
|
|
|
|
#if 1
|
|
|
|
|
|
int length = 20;
|
|
|
|
|
|
char in_char[21] = {'\0'};
|
|
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
if (e == e_zero)
|
|
|
|
|
|
{
|
2025-03-05 09:33:46 +00:00
|
|
|
|
// char data[10] = {'55','aa','0b','0a','20',
|
|
|
|
|
|
// '4e','00','00','00','c3'};
|
2025-03-06 07:25:37 +00:00
|
|
|
|
const char *data = "55aa0b0a204e000000c3";
|
2025-03-12 09:29:52 +00:00
|
|
|
|
memcpy(in_char, data, length);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (e == e_back)
|
|
|
|
|
|
{
|
2025-03-05 09:33:46 +00:00
|
|
|
|
// char data[10] = {'55','aa','08','10','27',
|
|
|
|
|
|
// 'f0','d8','ff','ff','c3'};
|
2025-03-06 07:25:37 +00:00
|
|
|
|
const char *data = "55aa081027f0d8ffffc3";
|
2025-03-12 09:29:52 +00:00
|
|
|
|
memcpy(in_char, data, length);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (e == e_forward)
|
|
|
|
|
|
{
|
2025-03-05 09:33:46 +00:00
|
|
|
|
// char data[10] = {'55','aa','08','10','27',
|
|
|
|
|
|
// '10','27','00','00','c3'};
|
2025-03-06 07:25:37 +00:00
|
|
|
|
const char *data = "55aa08102710270000c3";
|
2025-03-12 09:29:52 +00:00
|
|
|
|
memcpy(in_char, data, length);
|
2025-03-05 09:33:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
// char in_char[] = "55aa0b0a204e000000c3";
|
|
|
|
|
|
|
|
|
|
|
|
char out_char[21] = {'\0'};
|
|
|
|
|
|
int hex_length = 10;
|
|
|
|
|
|
to_hex(in_char, hex_length, out_char);
|
|
|
|
|
|
|
|
|
|
|
|
int num = _sp->write(out_char,
|
|
|
|
|
|
hex_length);
|
2025-03-12 09:29:52 +00:00
|
|
|
|
if (num == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "write failed.";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "write ok|num:" << num;
|
2025-03-05 09:33:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
void SerialPort::to_hex(char *in_char, int char_length, char *out_char)
|
2025-03-05 09:33:46 +00:00
|
|
|
|
{
|
|
|
|
|
|
while (char_length--)
|
|
|
|
|
|
{
|
|
|
|
|
|
*out_char = (*in_char & 0x40 ? *in_char + 9 : *in_char) << 4;
|
|
|
|
|
|
++in_char;
|
|
|
|
|
|
*out_char |= (*in_char & 0x40 ? *in_char + 9 : *in_char) & 0xF;
|
|
|
|
|
|
++in_char;
|
|
|
|
|
|
++out_char;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-11 09:31:15 +00:00
|
|
|
|
void SerialPort::displayPortInfo()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 获取系统中所有可用的串口信息
|
|
|
|
|
|
QList<QSerialPortInfo> serialPorts = QSerialPortInfo::availablePorts();
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历每个串口信息
|
2025-03-12 09:29:52 +00:00
|
|
|
|
for (const QSerialPortInfo &portInfo : serialPorts)
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "================================================";
|
2025-03-11 09:31:15 +00:00
|
|
|
|
// 打印串口的名称
|
|
|
|
|
|
qDebug() << "串口名称: " << portInfo.portName();
|
|
|
|
|
|
// 打印串口的描述信息
|
|
|
|
|
|
qDebug() << "描述信息: " << portInfo.description();
|
|
|
|
|
|
// 打印串口的制造商信息
|
|
|
|
|
|
qDebug() << "制造商: " << portInfo.manufacturer();
|
|
|
|
|
|
// 打印串口的序列号
|
|
|
|
|
|
qDebug() << "序列号: " << portInfo.serialNumber();
|
|
|
|
|
|
// 打印串口的系统位置
|
|
|
|
|
|
qDebug() << "系统位置: " << portInfo.systemLocation();
|
|
|
|
|
|
// 打印是否有虚拟调制解调器
|
|
|
|
|
|
qDebug() << "是否有虚拟调制解调器: " << (portInfo.hasVendorIdentifier() ? "是" : "否");
|
|
|
|
|
|
// 打印是否有产品标识符
|
|
|
|
|
|
qDebug() << "是否有产品标识符: " << (portInfo.hasProductIdentifier() ? "是" : "否");
|
|
|
|
|
|
// 如果有虚拟调制解调器,打印其标识符
|
2025-03-12 09:29:52 +00:00
|
|
|
|
if (portInfo.hasVendorIdentifier())
|
|
|
|
|
|
{
|
|
|
|
|
|
qint16 vid = portInfo.vendorIdentifier();
|
2025-03-11 09:31:15 +00:00
|
|
|
|
qDebug() << "虚拟调制解调器标识符: " << vid;
|
|
|
|
|
|
QString hexStr = QString("0x%1").arg(vid, 4, 16, QChar('0')).toUpper();
|
2025-03-12 09:29:52 +00:00
|
|
|
|
qDebug() << "vid 0x" << hexStr;
|
2025-03-11 09:31:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
// 如果有产品标识符,打印其标识符
|
2025-03-12 09:29:52 +00:00
|
|
|
|
if (portInfo.hasProductIdentifier())
|
|
|
|
|
|
{
|
|
|
|
|
|
qint16 pid = portInfo.productIdentifier();
|
2025-03-11 09:31:15 +00:00
|
|
|
|
qDebug() << "产品标识符: " << pid;
|
|
|
|
|
|
QString hexStr = QString("0x%1").arg(pid, 4, 16, QChar('0')).toUpper();
|
2025-03-12 09:29:52 +00:00
|
|
|
|
qDebug() << "pid 0x" << hexStr;
|
2025-03-11 09:31:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-05 09:33:46 +00:00
|
|
|
|
void SerialPort::parserTest()
|
|
|
|
|
|
{
|
2025-03-12 09:29:52 +00:00
|
|
|
|
const uchar data[] = {0xa5, 0x5a, 0x2d, 0x83,
|
|
|
|
|
|
0x00, 0x00,
|
|
|
|
|
|
0x00, 0x01, 0x23, 0x23,
|
|
|
|
|
|
0x23, 0x23, 0x23, 0x23, 0x23,
|
|
|
|
|
|
0x23, 0x07, 0xbc, 0xb5, 0xf2, 0xc8, 0x57, 0x38, 0x40, 0x1b, 0x63, 0x27, 0xbc, 0x04, 0xa7, 0xf2, 0x3f, 0x55, 0x55, 0x55, 0xd5, 0x04, 0xf3, 0xab, 0xbf, 0xfa, 0x2b, 0xcd, 0x41, 0x00, 0x00, 0x93, 0xba};
|
2025-03-05 09:33:46 +00:00
|
|
|
|
CommonData *serialPortData = nullptr;
|
|
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
qDebug() << "data length:" << sizeof(data) / sizeof(uchar);
|
2025-03-05 09:33:46 +00:00
|
|
|
|
// serialPortData = (CommonData)&data[4];
|
2025-03-06 07:25:37 +00:00
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
serialPortData = reinterpret_cast<CommonData *>(const_cast<uchar *>(&data[6]));
|
|
|
|
|
|
qDebug() << "run type:" << serialPortData->run_type;
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
|
|
|
|
|
// qDebug()<<"run time:"<<serialPortData->add_run_time;
|
|
|
|
|
|
// qDebug()<<"sample temp:"<<serialPortData->sample_temp;
|
|
|
|
|
|
|
|
|
|
|
|
// 格式化输出 float 类型,保留两位小数
|
|
|
|
|
|
QString formattedRunTime = QString::number(serialPortData->add_run_time, 'f', 2);
|
|
|
|
|
|
qDebug() << "run time:" << formattedRunTime;
|
|
|
|
|
|
|
|
|
|
|
|
// 格式化输出 double 类型,保留三位小数
|
|
|
|
|
|
QString formattedSampleTemp = QString::number(serialPortData->sample_temp, 'f', 3);
|
|
|
|
|
|
qDebug() << "sample temp:" << formattedSampleTemp;
|
|
|
|
|
|
|
2025-03-06 07:25:37 +00:00
|
|
|
|
QString formattedColdTemp = QString::number(serialPortData->cold_temp, 'f', 3);
|
|
|
|
|
|
qDebug() << "cold temp:" << formattedColdTemp;
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
2025-03-06 07:25:37 +00:00
|
|
|
|
#if 0
|
2025-03-05 09:33:46 +00:00
|
|
|
|
// 定义小端序的 4 字节数据
|
|
|
|
|
|
unsigned char bytesLittle[] = {0x23, 0x23, 0x01, 0x00};
|
|
|
|
|
|
// 调用函数进行转换
|
|
|
|
|
|
float result = 0.0f;
|
|
|
|
|
|
std::memcpy(&result, bytesLittle, sizeof(float));
|
|
|
|
|
|
// float floatValue = bytesToFloatLittleEndian(bytesLittle);
|
|
|
|
|
|
// 输出转换后的浮点数
|
2025-03-06 07:25:37 +00:00
|
|
|
|
std::cout << "result: " << result << std::endl;
|
|
|
|
|
|
#endif
|
2025-03-05 09:33:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-17 09:30:54 +00:00
|
|
|
|
void SerialPort::slotDeliverData(const QByteArray &ba)
|
2025-03-12 09:29:52 +00:00
|
|
|
|
{
|
|
|
|
|
|
openSp();
|
|
|
|
|
|
|
|
|
|
|
|
slotSendData(ba);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-11 09:31:15 +00:00
|
|
|
|
void SerialPort::slotSendData(const QByteArray &ba)
|
|
|
|
|
|
{
|
2025-03-17 09:30:54 +00:00
|
|
|
|
/***
|
|
|
|
|
|
* 写入的数据有
|
|
|
|
|
|
* 1、阶段设置。
|
|
|
|
|
|
*/
|
|
|
|
|
|
if(WRITE_CMD == ba.at(3)){
|
|
|
|
|
|
_lastWriteBa = ba;
|
|
|
|
|
|
}
|
2025-03-12 09:29:52 +00:00
|
|
|
|
qDebug() << "slotSendData:" << ba.size();
|
2025-03-11 09:31:15 +00:00
|
|
|
|
|
2025-03-17 09:30:54 +00:00
|
|
|
|
if (_sp != nullptr && _sp->isOpen())
|
|
|
|
|
|
{
|
2025-03-13 09:27:31 +00:00
|
|
|
|
_sp->write(ba);
|
2025-03-17 09:30:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
qDebug() << "sp not open.";
|
2025-03-13 09:27:31 +00:00
|
|
|
|
// qErrnoWarning("sp not open.");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-03-12 09:29:52 +00:00
|
|
|
|
}
|
2025-03-05 09:33:46 +00:00
|
|
|
|
|
2025-03-12 09:29:52 +00:00
|
|
|
|
void SerialPort::slotCloseSp()
|
|
|
|
|
|
{
|
2025-03-17 09:30:54 +00:00
|
|
|
|
if (_sp != nullptr && _sp->isOpen())
|
|
|
|
|
|
{
|
2025-03-13 09:27:31 +00:00
|
|
|
|
_sp->close();
|
|
|
|
|
|
}
|
2025-03-12 09:29:52 +00:00
|
|
|
|
}
|