2026年6月22日 :大量更新
This commit is contained in:
+53
-61
@@ -8,6 +8,8 @@
|
||||
#include <QElapsedTimer>
|
||||
#include <QEventLoop>
|
||||
#include <QCoreApplication>
|
||||
#include <QTime>
|
||||
|
||||
#define MAX_LEN1 1024
|
||||
#define MAX_LEN2 224
|
||||
|
||||
@@ -31,8 +33,8 @@ Serial::Serial(const QString comport)
|
||||
}
|
||||
|
||||
Serial::~Serial(){
|
||||
if(serial)
|
||||
serial->close();
|
||||
if (serial) serial->close();
|
||||
|
||||
}
|
||||
|
||||
int baudRateValue = 0;
|
||||
@@ -153,11 +155,11 @@ void Serial::readData(){
|
||||
if(!isCommunication) return;
|
||||
if(!timerRead->isActive() && !flagOfUpgradeSTM32)
|
||||
timerRead->start(50);
|
||||
QByteArray recData = serial->readAll();
|
||||
QByteArray recDat = serial->readAll();
|
||||
if(com == "/dev/ttyAS5")
|
||||
readFPGAData(recData);
|
||||
readFPGAData(recDat);
|
||||
else if(com == "/dev/ttyAS2")
|
||||
readSTM32Data(recData);
|
||||
readSTM32Data(recDat);
|
||||
}
|
||||
|
||||
unsigned char dataFPGA_FE[12] = {0};
|
||||
@@ -169,11 +171,9 @@ unsigned char dataFPGA_1B8[12] = {0};
|
||||
|
||||
void Serial::readFPGAData(QByteArray data) {
|
||||
recData.append(data);
|
||||
|
||||
if(recData.length() < curLen+10){
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if(timerRead->isActive())
|
||||
timerRead->stop();
|
||||
@@ -189,30 +189,39 @@ void Serial::readFPGAData(QByteArray data) {
|
||||
/***************出队列***************/
|
||||
{
|
||||
QMutexLocker locker(&Mutex);
|
||||
if(recData[1]==0x01 || !set.contains(addr)){
|
||||
if(queue.size() > 0)
|
||||
queue.dequeue();
|
||||
if(recData[1]==0x01 || !set.contains(addr)){
|
||||
if(queue.size() > 0)
|
||||
queue.dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
if(queue.size() > 1){
|
||||
if(queue.size() > 0){
|
||||
qDebug()<<"send FPGA queue data 0";
|
||||
emit sendQueueData(0,0,NULL,0,0);
|
||||
}
|
||||
int headCode=recData[0];
|
||||
if(headCode != 0xAA){
|
||||
qDebug()<<"FPGA接收数据帧头错误:Frameheader"<<QString::number(headCode).toInt() << "addr:"<<hexData;
|
||||
recData.clear(); // 帧头错误,清空缓冲区防止持续累积
|
||||
return ;
|
||||
}
|
||||
if(!calCRC16CodeIn(recData)){
|
||||
FPGACRCErrorCNt++;
|
||||
QString hexString = QString(recData.mid(2, 3).toHex());
|
||||
qDebug()<<"FPGA CRC校验码错误:CRC"<<CRCRec<<"addr:"<<hexString<<hexData;
|
||||
recData.clear(); // CRC错误,清空缓冲区防止持续累积
|
||||
// CRC错误时也需要出队,否则队列会积压
|
||||
{
|
||||
QMutexLocker locker(&Mutex);
|
||||
if(queue.size() > 0)
|
||||
queue.dequeue();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if(recData[1] == 0x02 && set.contains(addr) && recData[7] != 0x00){
|
||||
qDebug()<<"FPGA写错误";
|
||||
recData.clear();
|
||||
emit resendFPGAData(2,addr,NULL,1,0);
|
||||
return;
|
||||
}
|
||||
@@ -236,8 +245,7 @@ void Serial::readFPGAData(QByteArray data) {
|
||||
memcpy(dataFPGA_100,recData.constData(),74);
|
||||
emit getFPGAPhaseDetectHighTm(dataFPGA_100);
|
||||
break;
|
||||
case 0x180:
|
||||
{
|
||||
case 0x180:{
|
||||
//dataofFPGACalandarAndDat=reinterpret_cast<unsigned char*>(recData.data());
|
||||
memcpy(dataFPGA_180,recData.constData(),26);
|
||||
emit getFPGACalandarAndDat(dataFPGA_180);
|
||||
@@ -266,8 +274,9 @@ void Serial::readFPGAData(QByteArray data) {
|
||||
emit FPGABuildDat(fpgaBuildDat);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
recData.clear(); // 正常处理完毕,清空缓冲区
|
||||
}
|
||||
|
||||
|
||||
@@ -300,16 +309,15 @@ void Serial::readSTM32Data(QByteArray data) {
|
||||
|
||||
{
|
||||
QMutexLocker locker(&Mutex);
|
||||
if(queue.size() > 0){
|
||||
queue.dequeue();
|
||||
if(queue.size() > 0){
|
||||
queue.dequeue();
|
||||
}
|
||||
}
|
||||
if(queue.size() > 2){
|
||||
if(queue.size() > 0){
|
||||
qDebug()<<"send STM32 queue data 0";
|
||||
emit sendQueueData(0,0,NULL,0,0);
|
||||
}
|
||||
|
||||
|
||||
int addr = recData[2];
|
||||
int headCode = recData[0];
|
||||
if(headCode != 0xBB){
|
||||
@@ -319,26 +327,29 @@ void Serial::readSTM32Data(QByteArray data) {
|
||||
}
|
||||
if(recData[1] != 0xF0 && recData[1] != 0x01 && recData[1] != 0x02){
|
||||
qDebug()<<"STM32接收指令字节错误."<< "addr:"<<QString::number(addr).toInt();
|
||||
recData.clear(); // 指令字节错误,清空缓冲区
|
||||
return ;
|
||||
}
|
||||
if(!calCRC16CodeIn(recData)){
|
||||
STM32CRCErrorCNt++;
|
||||
qDebug()<<"STM32 CRC校验码错误."<<"addr:"<<QString::number(addr).toInt();
|
||||
recData.clear(); // CRC错误,清空缓冲区防止持续累积
|
||||
return;
|
||||
}
|
||||
int dataLen = recData[3]; // 假设 recData[3] 是数据长度字段
|
||||
int tailIndex = dataLen + 6;
|
||||
if (tailIndex >= recData.size()) {
|
||||
qDebug() << "STM32 帧尾索引越界,数据不完整";
|
||||
recData.clear(); // 数据不完整,清空缓冲区
|
||||
return;
|
||||
}
|
||||
if(recData[recData[3]+6] != 0x55){
|
||||
qDebug()<<"STM32接收数据帧尾错误."<<"addr:"<<QString::number(addr).toInt();
|
||||
recData.clear(); // 帧尾错误,清空缓冲区
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(recData[1]==0x01){
|
||||
switch(addr){
|
||||
case 0xFF:
|
||||
@@ -363,6 +374,7 @@ void Serial::readSTM32Data(QByteArray data) {
|
||||
if(addr == 0xFE && recData[4] == 0x00)
|
||||
emit powerOffReady();
|
||||
}
|
||||
recData.clear(); // 正常处理完毕,清空缓冲区
|
||||
}
|
||||
|
||||
void Serial::sendSerialDataFromTimer(){
|
||||
@@ -372,6 +384,7 @@ void Serial::sendSerialDataFromTimer(){
|
||||
void Serial::readTimeOut(){
|
||||
timerRead->stop();
|
||||
serial->clear(QSerialPort::Input);
|
||||
recData.clear(); // 超时时清空接收缓冲区,防止残留数据污染下次接收
|
||||
if(com == "/dev/ttyAS5")
|
||||
qDebug()<<"Read FPGA Serial data Timeout";
|
||||
else if(com == "/dev/ttyAS2")
|
||||
@@ -379,47 +392,26 @@ void Serial::readTimeOut(){
|
||||
sendSerialData(0,0,NULL,0,0);
|
||||
}
|
||||
|
||||
// 每个逻辑写操作以一条记录入队;sendDataToFPGA 内部负责分包,队列只记录逻辑单元
|
||||
static constexpr int QUEUE_FPGA_MAX = 50; // queueFPGA 积压上限
|
||||
static constexpr int QUEUE_STM32_MAX = 100; // queue 积压上限
|
||||
|
||||
void Serial::enQueue(int cmd, int addr, unsigned char *data, int len, int type, bool flag){
|
||||
send_Data sendData;
|
||||
const send_Data sendData = {cmd, addr, data, len, type};
|
||||
if(flag){
|
||||
int max = 0;
|
||||
if(cmd == 0xD2)
|
||||
max = MAX_LEN1;
|
||||
else
|
||||
max = MAX_LEN2;
|
||||
int num = len/max;
|
||||
if(len <= max){
|
||||
sendData.cmd = cmd;
|
||||
sendData.addr = addr;
|
||||
sendData.data = data;
|
||||
sendData.len = len;
|
||||
sendData.type = type;
|
||||
queueFPGA.append(sendData);
|
||||
}
|
||||
else{
|
||||
for(int i=0;i<num;i++){
|
||||
sendData.cmd = cmd;
|
||||
sendData.addr = addr + i*max;
|
||||
sendData.data = data + i*max;
|
||||
sendData.len = max;
|
||||
sendData.type = type;
|
||||
queueFPGA.append(sendData);
|
||||
}
|
||||
sendData.cmd = cmd;
|
||||
sendData.addr = addr + num*max;
|
||||
sendData.data = data + num*max;
|
||||
sendData.len = len - num*max;
|
||||
sendData.type = type;
|
||||
queueFPGA.append(sendData);
|
||||
// 积压保护:FPGA 响应跟不上时丢弃旧条目,防止内存无限增长
|
||||
if(queueFPGA.size() > QUEUE_FPGA_MAX){
|
||||
qDebug()<<"queueFPGA 积压过多,清空旧数据,当前大小:"<<queueFPGA.size();
|
||||
queueFPGA.clear();
|
||||
}
|
||||
queueFPGA.append(sendData);
|
||||
}
|
||||
else{
|
||||
QMutexLocker locker(&Mutex);
|
||||
sendData.cmd = cmd;
|
||||
sendData.addr = addr;
|
||||
sendData.data = data;
|
||||
sendData.len = len;
|
||||
sendData.type = type;
|
||||
if(queue.size() > QUEUE_STM32_MAX){
|
||||
qDebug()<<"queue 积压过多,清空旧数据,当前大小:"<<queue.size();
|
||||
queue.clear();
|
||||
}
|
||||
queue.append(sendData);
|
||||
}
|
||||
}
|
||||
@@ -458,12 +450,11 @@ void Serial::sendSerialData(int cmd,int addr, unsigned char* data, int len,int t
|
||||
if(!isCommunication) return;
|
||||
if(flagOfUpgradeSTM32) return;
|
||||
|
||||
bool flag=false;
|
||||
bool flag=false; //地址标志位
|
||||
if(type == 1 && set.contains(addr)){
|
||||
flag = true;
|
||||
enQueue(cmd,addr,data,len,type,1);
|
||||
}
|
||||
|
||||
else if(type != 0){
|
||||
enQueue(cmd,addr,data,len,type,0);
|
||||
}
|
||||
@@ -495,10 +486,8 @@ void Serial::sendSerialData(int cmd,int addr, unsigned char* data, int len,int t
|
||||
|
||||
}
|
||||
else{
|
||||
|
||||
getDataFromQueue(cmd,addr,data,len,type,flag);
|
||||
sendDataToFPGA(cmd,addr,data,len);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -547,7 +536,7 @@ void Serial::sendDataToSTM32(int cmd,int addr, unsigned char* data, int len)
|
||||
serial->flush();
|
||||
serial->waitForBytesWritten(waitTime);
|
||||
if (bytesWritten == -1) {
|
||||
// qDebug() << "STM32写入数据失败。read";
|
||||
qDebug() << "STM32写入数据失败。read";
|
||||
} else {
|
||||
// qDebug() << "STM32成功发送 " << bytesWritten << " 个字节的数据。read";
|
||||
flagOfReadFinished = false;
|
||||
@@ -571,11 +560,11 @@ void Serial::sendDataToSTM32(int cmd,int addr, unsigned char* data, int len)
|
||||
sendData.resize(len+7);
|
||||
serial->clear(QSerialPort::Input);
|
||||
bytesWritten=serial->write(sendData,len+7);
|
||||
|
||||
|
||||
serial->flush();
|
||||
serial->waitForBytesWritten(waitTime);
|
||||
if (bytesWritten == -1) {
|
||||
// qDebug() << "STM32写入数据失败。";
|
||||
qDebug() << "STM32写入数据失败。";
|
||||
} else {
|
||||
flagOfReadFinished = false;
|
||||
recData.clear();
|
||||
@@ -696,6 +685,9 @@ void Serial::sendDataToFPGA(int cmd,int addr, unsigned char* data, int len){
|
||||
serial->clear(QSerialPort::Input);
|
||||
bytesWritten=serial->write(sendData,len+10);
|
||||
|
||||
if(addr == 0xB170)
|
||||
qDebug() << "Counter2 sectional group: " << sendData.toHex(' ');
|
||||
|
||||
serial->flush();
|
||||
if(cmd == 0xD2)
|
||||
serial->waitForBytesWritten(12);
|
||||
|
||||
Reference in New Issue
Block a user