diff --git a/CableTool/common.h b/CableTool/common.h index c780af7..32076eb 100644 --- a/CableTool/common.h +++ b/CableTool/common.h @@ -92,7 +92,11 @@ typedef unsigned uint32_t; #define USART_BUF_SIZE 1088 #define DEBUG_DATA_SIZE 1024 #define UPDATE_DATA_SIZE DEBUG_DATA_SIZE + +#define FLASH_LOG_BUF_LEN 256 + #define FD_BUF_LEN 256 +#define FLASH_LOG_DATA_LEN 248 #define MONITOR_ADCi_NOTIFY_OK 0x101 #define MONITOR_ADC_NOTIFY_OK 0x102 @@ -451,6 +455,13 @@ typedef struct uint8_t reserve[2]; } waveParam_t; +/* Note: 必须保证这个结构体的大小是可以被flash扇区大小整除的,比如128,256... */ +typedef struct +{ + uint64_t id; + uint8_t data[FLASH_LOG_DATA_LEN]; +} falsh_log_t; + /* Note: 必须保证这个结构体的大小是可以被flash扇区大小整除的,比如128,256... */ typedef struct { diff --git a/CableTool/widget.cpp b/CableTool/widget.cpp index 912c243..e0f545e 100644 --- a/CableTool/widget.cpp +++ b/CableTool/widget.cpp @@ -52,6 +52,7 @@ Widget::Widget(QWidget *parent) is_wave_up = 0; dataIndex = 0; is_data_up = false; + historyDataSum = 0; logSum = 0; energyMode[0] = "未知"; @@ -910,6 +911,69 @@ int Widget::protoAdjustInfo(char *data) return 0; } +int Widget::protoLog(char * data) +{ + mul_head_t *header = reinterpret_cast(data + sizeof(mul_head_t)); + falsh_log_t *log; + uint32_t cnt = 0; + + /* 数据头校验. */ + if (header->index != logIndex + || header->len > UPDATE_DATA_SIZE) + { + return -1; + } + + /* 获取文件. */ + QFile file("log.txt"); + if (0 == header->index) + { + file.remove(); + } + + if (file.open(QIODevice::WriteOnly | QIODevice::Append)) + { + cnt = header->len / FLASH_LOG_BUF_LEN; + for(uint32_t i = 0; i < cnt; i++) + { + log = reinterpret_cast(data + sizeof(proto_head_t) + sizeof(mul_head_t) + i * FLASH_LOG_BUF_LEN); + file.write((char *)log->data, strlen((char *)log->data)); + ui->logTextEdit->insertPlainText((char *)log->data); + } + file.close(); + } + else + { + qDebug() << "file ERROR"; + return -1; + } + + /* 通知发包线程进入下个状态. */ + if (header->len < UPDATE_DATA_SIZE) + { + //work->multipleIndexSet(0); + //work->stateSet(CM_usart::WS_IDLE); + logIndex = 0; + buttonEnable(true); + ui->logIndexLabel->setText("下载完成"); + QMessageBox::information(this, "SUCCEED", "Log 文件下载完成.", QMessageBox::Ok); + is_log_up = false; + ui->stateLogButton->setText("下载 Log"); + } + else + { + logIndex = header->index + 1;; + QString temp; + temp.sprintf("%d / %d", logIndex, ui->stateLogEdit->text().toInt()); + ui->logIndexLabel->setText(temp); + //work->multipleIndexSet(header->index + 1); + sendMsgLogGet(); + } + + return 0; +} + + int Widget::protoHistoryData(char * data) { mul_head_t *header = reinterpret_cast(data + sizeof(proto_head_t)); @@ -993,6 +1057,7 @@ int Widget::protoHistoryData(char * data) //work->multipleIndexSet(header->index + 1); sendMsgHistoryDataGet(); } + return 0; } int Widget::paraseProtocols(char *data) @@ -1466,6 +1531,31 @@ int Widget::sendMsgWaveCal() return 0; } +int Widget::sendMsgLogGet() +{ + char buf[2048] = {0}; + uint32_t *crc = nullptr; + proto_head_t *header = reinterpret_cast(buf); + mul_head_t *m_head = reinterpret_cast(buf + sizeof(proto_head_t)); + int32_t *data = reinterpret_cast(buf + sizeof(proto_head_t) + sizeof(mul_head_t)); + + /* 初始化报文头. */ + protoHeaderInit(header, sizeof(proto_head_t) + sizeof(mul_head_t) + sizeof(uint32_t), clientDevInfo.id, DEBUG_CT_PRV_REQUEST, DEBUG_PRV_LOG); + + m_head->index = logIndex; + m_head->len = 0; + *data = logSum; + + /* 计算校验位. */ + crc = reinterpret_cast(buf + header->len); + *crc = crc32(buf, header->len); + + HexPrint(__FUNCTION__, buf, header->len + 4); + writeData(buf, header->len + 4); + return 0; +} + + int Widget::sendMsgHistoryDataGet() { char buf[2048] = {0}; @@ -1479,7 +1569,7 @@ int Widget::sendMsgHistoryDataGet() m_head->index = dataIndex; m_head->len = 0; - *data = logSum; + *data = historyDataSum; /* 计算校验位. */ crc = reinterpret_cast(buf + header->len); @@ -1854,7 +1944,7 @@ void Widget::on_stateLogButton_clicked() logIndex = 0; QString temp; temp.sprintf("0 / %d", ui->stateLogEdit->text().toInt()); - //work->logSumSet(ui->stateLogEdit->text().toInt()); + logSum = ui->stateLogEdit->text().toInt(); ui->logTextEdit->clear(); ui->logIndexLabel->setText(temp); //work->multipleIndexSet(0); @@ -1884,7 +1974,7 @@ void Widget::on_historyDataButton_clicked() dataIndex = 0; QString temp; temp.sprintf("0 / %d", ui->historyDataEdit->text().toInt()); - logSum = ui->historyDataEdit->text().toInt(); + historyDataSum = ui->historyDataEdit->text().toInt(); ui->dataIndexLabel->setText(temp); sendMsgHistoryDataGet(); diff --git a/CableTool/widget.h b/CableTool/widget.h index 9dd3e4c..dfbc34e 100644 --- a/CableTool/widget.h +++ b/CableTool/widget.h @@ -143,6 +143,8 @@ private: int protoAdjustInfo(char *data); + int protoLog(char * data); + int protoHistoryData(char *data); int paraseProtocols(char *data); @@ -172,6 +174,8 @@ private: int sendMsgWaveCal(); + int sendMsgLogGet(); + int sendMsgHistoryDataGet(); Ui::form *ui; //新增的 @@ -209,6 +213,7 @@ private: uint8_t is_data_up; uint32_t dataIndex; int32_t logSum; + int32_t historyDataSum; ReadOnlyDelegate* readOnlyDelegate;