zhongfuqiang上传最新代码20250325
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
// SerialPortHandler.cpp
|
||||
#include "SerialPortHandler.h"
|
||||
#include "crc32.h"
|
||||
|
||||
SerialPortHandler::SerialPortHandler(QComboBox* comPortComboBox, QPushButton* connectButton,
|
||||
SerialPortHandler::SerialPortHandler(const UI_config& config, QComboBox* comPortComboBox, QPushButton* connectButton,
|
||||
QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest, QJsonArray frontBoardFuncConfig,
|
||||
QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
|
||||
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig, QJsonArray backBoardDevInfoJson,
|
||||
@@ -14,7 +15,7 @@ SerialPortHandler::SerialPortHandler(QComboBox* comPortComboBox, QPushButton* co
|
||||
backBoardFuncConfig(backBoardFuncConfig), backBoardDevInfoJson(backBoardDevInfoJson),
|
||||
backBoardUuidJson(backBoardUuidJson), getPicJson(getPicJson), getVideoJson(getVideoJson),
|
||||
currentItemIndex(0), isManualSend(false), isSingleSend(false), isClickedSend(false),
|
||||
isFirstDataReceived(true),
|
||||
isFirstDataReceived(true), TOOL_UI(config),
|
||||
isDataStuck(false), dataProcessingActive(false), isRecvVideoData(true),
|
||||
currentFrontBoardIndex(0), currentLicenseIndex(0)
|
||||
{
|
||||
@@ -51,6 +52,11 @@ SerialPortHandler::~SerialPortHandler()
|
||||
delete portDetectionTimer;
|
||||
portDetectionTimer = nullptr;
|
||||
}
|
||||
if (sendHeartBeatTimer) {
|
||||
sendHeartBeatTimer->stop();
|
||||
delete sendHeartBeatTimer;
|
||||
sendHeartBeatTimer = nullptr;
|
||||
}
|
||||
if (worker) {
|
||||
worker->stop();
|
||||
worker->wait(); // 确保线程已停止
|
||||
@@ -60,6 +66,7 @@ SerialPortHandler::~SerialPortHandler()
|
||||
if (serialPort->isOpen()) {
|
||||
serialPort->close();
|
||||
}
|
||||
qDebug() << "SerialPortHandler 被销毁,所有定时器已停止";
|
||||
}
|
||||
|
||||
void SerialPortHandler::initializeSerialPort()
|
||||
@@ -71,27 +78,40 @@ void SerialPortHandler::initializeSerialPort()
|
||||
|
||||
void SerialPortHandler::initializeTimers()
|
||||
{
|
||||
qDebug() << "---- SerialPortHandler::initializeTimers()";
|
||||
// 确保定时器在当前线程中创建
|
||||
serialTimer = new QTimer(this);
|
||||
serialTimer->setSingleShot(true);
|
||||
//serialTimer->moveToThread(this->thread());
|
||||
connect(serialTimer, &QTimer::timeout, this, &SerialPortHandler::onTimeout);
|
||||
|
||||
portDetectionTimer = new QTimer();
|
||||
portDetectionTimer = new QTimer(this);
|
||||
//portDetectionTimer->moveToThread(this->thread());
|
||||
connect(portDetectionTimer, &QTimer::timeout, this, &SerialPortHandler::updateAvailablePorts);
|
||||
QMetaObject::invokeMethod(portDetectionTimer, "start", Qt::QueuedConnection, Q_ARG(int, 2000));
|
||||
qDebug() << "Current thread in initializeTimers:" << QThread::currentThread();
|
||||
qDebug() << "SerialPortHandler thread:" << this->thread();
|
||||
qDebug() << "serialTimer thread:" << serialTimer->thread();
|
||||
qDebug() << "portDetectionTimer thread:" << portDetectionTimer->thread();
|
||||
|
||||
sendHeartBeatTimer = new QTimer(this);
|
||||
//portDetectionTimer->moveToThread(this->thread());
|
||||
connect(sendHeartBeatTimer, &QTimer::timeout, this, &SerialPortHandler::updateSendHeartBeat);
|
||||
QMetaObject::invokeMethod(sendHeartBeatTimer, "start", Qt::QueuedConnection, Q_ARG(int, 1400));
|
||||
//qDebug() << "Current thread in initializeTimers:" << QThread::currentThread();
|
||||
//qDebug() << "SerialPortHandler thread:" << this->thread();
|
||||
//qDebug() << "serialTimer thread:" << serialTimer->thread();
|
||||
//qDebug() << "portDetectionTimer thread:" << portDetectionTimer->thread();
|
||||
}
|
||||
|
||||
void SerialPortHandler::updateSendHeartBeat() {
|
||||
QByteArray payload;
|
||||
payload.append(static_cast<char>(0x00)); // 构造发送的数据内容
|
||||
bool heartBeat = true;
|
||||
onSendHeartBeatData(payload, heartBeat);
|
||||
}
|
||||
|
||||
void SerialPortHandler::OnStartTimeout(int timeout)
|
||||
{
|
||||
qDebug() << "---------------> onStartTimeout timeout :" << timeout;
|
||||
qDebug() << "Current thread:" << QThread::currentThread();
|
||||
qDebug() << "serialTimer thread:" << serialTimer->thread();
|
||||
//qDebug() << "Current thread:" << QThread::currentThread();
|
||||
//qDebug() << "serialTimer thread:" << serialTimer->thread();
|
||||
// 确保在正确线程中启动或停止定时器
|
||||
if (timeout > 0) {
|
||||
qDebug() << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");
|
||||
@@ -124,18 +144,34 @@ void SerialPortHandler::onSendHeartBeatData(const QByteArray& data, bool heartBe
|
||||
stream << quint16(0x0138); // 回复心跳
|
||||
stream << quint32(data.size()); // 数据长度
|
||||
prefix.append(data); // 将实际数据追加到前缀后
|
||||
|
||||
qDebug() << "Send heartBeat data:----->" << prefix.toHex();
|
||||
if (serialPort->isOpen()) {
|
||||
qDebug() << "Send heartBeat data:----->" << prefix.toHex();
|
||||
serialPort->write(prefix);
|
||||
serialPort->flush(); // 确保数据立即发送
|
||||
}
|
||||
else {
|
||||
qDebug() << "Serial port is not open!";
|
||||
}
|
||||
qDebug() << "当前时间:" << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");
|
||||
}
|
||||
|
||||
void SerialPortHandler::sendData(const QByteArray& data, bool heartBeat) {
|
||||
void readBmpFile_1(const QString& filePath) {
|
||||
QFile file(filePath);
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
BMPHeader header;
|
||||
file.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
|
||||
// 确保是 BMP 文件
|
||||
if (header.bfType[0] == 'B' && header.bfType[1] == 'M') {
|
||||
qDebug() << "Width :" << header.biWidth;
|
||||
qDebug() << "Height :" << header.biHeight;
|
||||
qDebug() << "Bit Count:" << header.biBitCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if !UPDATE_LOGO
|
||||
void SerialPortHandler::sendData(QByteArray data, bool heartBeat, const QByteArray& bmpData) {
|
||||
QByteArray prefix;
|
||||
QDataStream stream(&prefix, QIODevice::WriteOnly);
|
||||
stream.setByteOrder(QDataStream::LittleEndian);
|
||||
@@ -148,10 +184,75 @@ void SerialPortHandler::sendData(const QByteArray& data, bool heartBeat) {
|
||||
else {
|
||||
stream << quint16(0x0420); // 回复产测指令
|
||||
}
|
||||
stream << quint32(data.size()); // 数据长度
|
||||
prefix.append(data); // 将实际数据追加到前缀后
|
||||
qDebug() << "heartBeat:----->" << heartBeat;
|
||||
qDebug() << "Send data:----->" << prefix.toHex();
|
||||
|
||||
// 读取 BMP 文件数据
|
||||
//QByteArray bmpData;
|
||||
QString bmpFilePath = "./logo.bmp";
|
||||
#if 0
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
|
||||
if (!jsonDoc.isObject()) {
|
||||
qWarning() << "Invalid JSON format in data.";
|
||||
return;
|
||||
}
|
||||
QJsonObject jsonObj = jsonDoc.object();
|
||||
if (jsonObj.contains("cmd") && jsonObj["cmd"].toString() == "UPDATE_LOGO") {
|
||||
qDebug() << "cmd is UPDATE_LOGO";
|
||||
QMetaObject::invokeMethod(qApp, [&]() {
|
||||
bmpFilePath = QFileDialog::getOpenFileName(nullptr, "选择 BMP 图片", "", "BMP Files (*.bmp);;All Files (*)");
|
||||
}, Qt::QueuedConnection); // 让它异步执行
|
||||
|
||||
if (bmpFilePath.isEmpty()) {
|
||||
qDebug() << "No image selected.";
|
||||
return;
|
||||
}
|
||||
readBmpFile_1(bmpFilePath);
|
||||
if (!bmpFilePath.isEmpty()) {
|
||||
QFile file(bmpFilePath);
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
bmpData = file.readAll();
|
||||
file.close();
|
||||
}
|
||||
else {
|
||||
qWarning() << "无法打开 BMP 文件:" << bmpFilePath;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// **解析 JSON 并修改字段**
|
||||
jsonObj["offset"] = bmpData.size();
|
||||
quint32 crc32Value = crc32(0, bmpData.constData(), bmpData.size());
|
||||
qDebug() << "----- crc32 :" << crc32Value;
|
||||
qDebug() << "----- offset:" << bmpData.size();
|
||||
jsonObj["crc32"] = static_cast<int>(crc32Value);
|
||||
|
||||
// 重新转换为 QByteArray
|
||||
data = QJsonDocument(jsonObj).toJson(QJsonDocument::Compact);
|
||||
}
|
||||
else {
|
||||
qDebug() << "cmd is not UPDATE_LOGO";
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// 计算总数据长度(修改后的 JSON 数据 + BMP 数据)
|
||||
quint32 totalSize = data.size() + bmpData.size();
|
||||
//quint32 totalSize = data.size();
|
||||
stream << totalSize; // 发送数据总长度
|
||||
|
||||
// 追加数据部分
|
||||
prefix.append(data);
|
||||
if (!bmpData.isEmpty()) {
|
||||
prefix.append(bmpData);
|
||||
qDebug() << "Send data:-----> 图片数据";
|
||||
}
|
||||
else {
|
||||
qDebug() << "bmpData.isEmpty()----->";
|
||||
qDebug() << "Send data:----->" << prefix.toHex();
|
||||
}
|
||||
|
||||
|
||||
qDebug() << "Current working directory:" << QDir::currentPath();
|
||||
// 发送数据
|
||||
qDebug() << "heartBeat:----->" << heartBeat;
|
||||
if (serialPort->isOpen()) {
|
||||
serialPort->write(prefix);
|
||||
serialPort->flush(); // 确保数据立即发送
|
||||
@@ -160,6 +261,104 @@ void SerialPortHandler::sendData(const QByteArray& data, bool heartBeat) {
|
||||
qDebug() << "Serial port is not open!";
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
void SerialPortHandler::sendData(QByteArray data, bool heartBeat) {
|
||||
QByteArray prefix;
|
||||
QDataStream stream(&prefix, QIODevice::WriteOnly);
|
||||
stream.setByteOrder(QDataStream::LittleEndian);
|
||||
|
||||
// 构造前缀
|
||||
stream << quint32(0x55AA55AA);
|
||||
if (heartBeat) {
|
||||
stream << quint16(0x0138); // 回复心跳
|
||||
}
|
||||
else {
|
||||
stream << quint16(0x0420); // 回复产测指令
|
||||
}
|
||||
|
||||
// 读取 BMP 文件数据
|
||||
QByteArray bmpData;
|
||||
QString bmpFilePath = "./logo.bmp";
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
|
||||
if (!jsonDoc.isObject()) {
|
||||
qWarning() << "Invalid JSON format in data.";
|
||||
return;
|
||||
}
|
||||
QJsonObject jsonObj = jsonDoc.object();
|
||||
|
||||
if (jsonObj.contains("cmd") && jsonObj["cmd"].toString() == "UPDATE_LOGO") {
|
||||
qDebug() << "cmd is UPDATE_LOGO";
|
||||
lastData = data; // 先存储 JSON 数据
|
||||
// **发射信号,主线程会弹出文件选择框**
|
||||
emit selectBmpFile();
|
||||
return; // **这里 return,让子线程等待文件选择完成后再继续**
|
||||
}
|
||||
|
||||
// **继续发送数据**
|
||||
sendFinalData(data, heartBeat, bmpData);
|
||||
}
|
||||
|
||||
// **槽函数,在文件选择后被调用**
|
||||
void SerialPortHandler::onBmpFileSelected(QString path) {
|
||||
if (path.isEmpty()) {
|
||||
qDebug() << "No image selected.";
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray bmpData;
|
||||
QFile file(path);
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
bmpData = file.readAll();
|
||||
file.close();
|
||||
}
|
||||
else {
|
||||
qWarning() << "无法打开 BMP 文件:" << path;
|
||||
return;
|
||||
}
|
||||
|
||||
// **解析 JSON 并修改字段**
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(lastData);
|
||||
QJsonObject jsonObj = jsonDoc.object();
|
||||
jsonObj["offset"] = bmpData.size();
|
||||
|
||||
quint32 crc32Value = crc32(0xffffffff, bmpData.constData(), bmpData.size());
|
||||
jsonObj["crc32"] = static_cast<int>(crc32Value);
|
||||
|
||||
lastData = QJsonDocument(jsonObj).toJson(QJsonDocument::Compact);
|
||||
|
||||
// **继续发送数据**
|
||||
sendFinalData(lastData, false, bmpData);
|
||||
}
|
||||
|
||||
// **最终发送数据**
|
||||
void SerialPortHandler::sendFinalData(QByteArray data, bool heartBeat, QByteArray bmpData) {
|
||||
QByteArray prefix;
|
||||
QDataStream stream(&prefix, QIODevice::WriteOnly);
|
||||
stream.setByteOrder(QDataStream::LittleEndian);
|
||||
// 构造前缀
|
||||
stream << quint32(0x55AA55AA);
|
||||
if (heartBeat) {
|
||||
stream << quint16(0x0138); // 回复心跳
|
||||
}
|
||||
else {
|
||||
stream << quint16(0x0420); // 回复产测指令
|
||||
}
|
||||
quint32 totalSize = data.size() + bmpData.size();
|
||||
stream << totalSize;
|
||||
prefix.append(data);
|
||||
prefix.append(bmpData);
|
||||
|
||||
if (serialPort->isOpen()) {
|
||||
serialPort->write(prefix);
|
||||
serialPort->flush();
|
||||
}
|
||||
else {
|
||||
qDebug() << "Serial port is not open!";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void SerialPortHandler::handleSerialData(const QByteArray& data) {
|
||||
//qDebug() << "------------------------ Received data:" << data;
|
||||
@@ -228,6 +427,8 @@ int SerialPortHandler::connectPort() {
|
||||
if (selectedPort.isEmpty()) {
|
||||
return ret; // 如果没有选择串口,直接返回
|
||||
}
|
||||
QMetaObject::invokeMethod(portDetectionTimer, "start", Qt::QueuedConnection, Q_ARG(int, 2000));
|
||||
QMetaObject::invokeMethod(sendHeartBeatTimer, "start", Qt::QueuedConnection, Q_ARG(int, 1400));
|
||||
serialPort->setPortName(selectedPort);
|
||||
if (serialPort->open(QIODevice::ReadWrite)) {
|
||||
ret = 1;
|
||||
@@ -257,7 +458,11 @@ void SerialPortHandler::disconnectPort() {
|
||||
if (serialPort->isOpen()) {
|
||||
serialPort->close();
|
||||
}
|
||||
connectButton->setText("连接");
|
||||
resetCurrentItemIndex();
|
||||
//QMetaObject::invokeMethod(portDetectionTimer, "stop", Qt::QueuedConnection);
|
||||
QMetaObject::invokeMethod(sendHeartBeatTimer, "stop", Qt::QueuedConnection);
|
||||
//connectButton->setText("连接前板");
|
||||
connectButton->setText(TOOL_UI.CONNECT_FRONT_BOARD);
|
||||
comPortComboBox->setEnabled(true);
|
||||
}
|
||||
|
||||
@@ -280,18 +485,21 @@ void SerialPortHandler::resetCurrentItemIndex()
|
||||
currentItemIndex = 0;
|
||||
currentFuncItemIndex = 0;
|
||||
itemsProcessedCount = 0;
|
||||
dataProcessingActive = false;
|
||||
isRecvVideoData = true;
|
||||
isPowerOnSend = false;
|
||||
isClickedSend = false;
|
||||
isSingleSend = false;
|
||||
isRecvImgData = false;
|
||||
//OnStartTimeout(0);
|
||||
emit startTimeout(0);
|
||||
}
|
||||
|
||||
void SerialPortHandler::onPicRecvFinished()
|
||||
{
|
||||
/*if (currentJson != getPicJson && currentJson != frontBoardTest) {
|
||||
currentItemIndex++;
|
||||
itemsProcessedCount++;
|
||||
qDebug() << "------> onPicRecvFinished :" << __FUNCTION__;
|
||||
sendNextItem(currentJson);
|
||||
}*/
|
||||
#if CONNECT_COM_TO_VERIFY
|
||||
//toggleConnection();
|
||||
#endif
|
||||
}
|
||||
|
||||
void SerialPortHandler::onCommandError() {
|
||||
@@ -326,7 +534,7 @@ QString SerialPortHandler::getCurrentFuncItemLable() const
|
||||
return lable; // 返回当前项的 "data" 字段
|
||||
}
|
||||
|
||||
void SerialPortHandler::sendJsonItem(const QJsonArray& jsonArray, int itemIndex, const QString text, const QString backBoardSn, const QString& itemType)
|
||||
void SerialPortHandler::sendJsonItem(const QJsonArray& jsonArray, int itemIndex, const QString text, const QString backBoardSn, const QString& itemType, const QByteArray& bmpData)
|
||||
{
|
||||
//startReadVideoDataTimer(preVideoClientId);
|
||||
currentJson = jsonArray;
|
||||
@@ -428,12 +636,32 @@ void SerialPortHandler::sendJsonItem(const QJsonArray& jsonArray, int itemIndex,
|
||||
currentItem["SN"] = backBoardSn;
|
||||
}
|
||||
}
|
||||
else if (currentItem.contains("offset") && currentItem.contains("crc32")) {
|
||||
qDebug() << "sendJsonItem contains(offset)";
|
||||
QJsonValue originalOffset = currentItem["offset"];
|
||||
QJsonValue originalCrc32 = currentItem["crc32"];
|
||||
if (originalOffset.isDouble()) {
|
||||
currentItem["offset"] = bmpData.size();
|
||||
qDebug() << "offset:" << bmpData.size();
|
||||
}
|
||||
else if (originalOffset.isString()) {
|
||||
qDebug() << "请将 offset 字段改为整型";
|
||||
}
|
||||
if (originalCrc32.isDouble()) {
|
||||
quint32 crc32Value = crc32(0, bmpData.constData(), bmpData.size());
|
||||
currentItem["crc32"] = static_cast<int>(crc32Value);
|
||||
qDebug() << "crc32:" << static_cast<int>(crc32Value);
|
||||
}
|
||||
else if (originalCrc32.isString()) {
|
||||
qDebug() << "请将 crc32 字段改为整型";
|
||||
}
|
||||
}
|
||||
QString itemData = QJsonDocument(currentItem).toJson();
|
||||
emit currentSendItem(currentItem["cmd"].toString());
|
||||
isTimeout = false;
|
||||
emit updateTimeout(isTimeout);
|
||||
// 发送 JSON
|
||||
sendData(itemData.toUtf8(), 0);
|
||||
sendData(itemData.toUtf8(), 0, bmpData);
|
||||
if (currentItem.contains("timeout")) {
|
||||
qDebug() << "sendJsonItem currentItem.contains(\"timeout\")";
|
||||
int timeout = currentItem.value("timeout").toInt();
|
||||
@@ -448,13 +676,13 @@ void SerialPortHandler::sendJsonItem(const QJsonArray& jsonArray, int itemIndex,
|
||||
// 发送获取设备信息按键
|
||||
void SerialPortHandler::sendGetDevInfoItem(int itemIndex)
|
||||
{
|
||||
sendJsonItem(getDevInfoJson, itemIndex, "", "", "devInfo");
|
||||
sendJsonItem(getDevInfoJson, itemIndex, "", "", "devInfo", "");
|
||||
}
|
||||
|
||||
// 发送取图按键
|
||||
void SerialPortHandler::sendGetPicItem(int itemIndex, int GetPicCamIndex)
|
||||
{
|
||||
sendJsonItem(getPicJson, itemIndex, QString::number(GetPicCamIndex), "", "getPic");
|
||||
sendJsonItem(getPicJson, itemIndex, QString::number(GetPicCamIndex), "", "getPic", "");
|
||||
}
|
||||
|
||||
// 发送拉视频按键
|
||||
@@ -464,7 +692,7 @@ void SerialPortHandler::sendGetVideoItem(int itemIndex, int GetVideoCamIndex)
|
||||
isRecvVideoData = true;
|
||||
isStartVideo = true;
|
||||
qDebug() << " isRecvVideoData:" << isRecvVideoData;
|
||||
sendJsonItem(getVideoJson, itemIndex, QString::number(GetVideoCamIndex), "", "handleVideo");
|
||||
sendJsonItem(getVideoJson, itemIndex, QString::number(GetVideoCamIndex), "", "handleVideo", "");
|
||||
}
|
||||
|
||||
// 发送License处理按键
|
||||
@@ -479,11 +707,11 @@ void SerialPortHandler::sendLicenseItem(int itemIndex, const QString text)
|
||||
qDebug() << "Invalid itemIndex";
|
||||
return;
|
||||
}
|
||||
sendJsonItem(frontBoardLicenseJson, itemIndex, text, "", "License");
|
||||
sendJsonItem(frontBoardLicenseJson, itemIndex, text, "", "License", "");
|
||||
}
|
||||
}
|
||||
|
||||
void SerialPortHandler::sendFrontFuncItem(int itemIndex, const QString text)
|
||||
void SerialPortHandler::sendFrontFuncItem(int itemIndex, const QString text, const QByteArray& bmpData)
|
||||
{
|
||||
if (isBackBoardOrAllBoard != 0) {
|
||||
emit HandleInvalidOperate("当前连接的是后板或整机,请勿操作前板页面的按键!!!");
|
||||
@@ -491,8 +719,8 @@ void SerialPortHandler::sendFrontFuncItem(int itemIndex, const QString text)
|
||||
}
|
||||
else
|
||||
{
|
||||
//qDebug() << "sendFuncItem Text:" << text;
|
||||
sendJsonItem(frontBoardFuncConfig, itemIndex, text, "", "func");
|
||||
qDebug() << "sendFuncItem Text:" << text;
|
||||
sendJsonItem(frontBoardFuncConfig, itemIndex, text, "", "func", bmpData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -502,7 +730,7 @@ void SerialPortHandler::sendFrontFuncItem(int itemIndex, const QString text)
|
||||
// emit DelUserDialogResultReceived(input); // 通知事件循环退出
|
||||
//}
|
||||
|
||||
void SerialPortHandler::sendFrontItem(int itemIndex)
|
||||
void SerialPortHandler::sendFrontItem(int itemIndex, const QByteArray& bmpData)
|
||||
{
|
||||
/*if (isBackBoardOrAllBoard != 0) {
|
||||
emit HandleInvalidOperate("当前连接的是后板或整机,请勿操作前板页面的按键!!!");
|
||||
@@ -527,6 +755,7 @@ void SerialPortHandler::sendFrontItem(int itemIndex)
|
||||
text = downloadUrl;
|
||||
}
|
||||
else if (currentItem.contains("cmd") && currentItem["cmd"].toString() == "DEL_USER") {
|
||||
qDebug() << "------1---------DEL_USER----------------";
|
||||
QEventLoop loop;
|
||||
QString userInput = "";
|
||||
DelUserWindow dialog;
|
||||
@@ -539,6 +768,7 @@ void SerialPortHandler::sendFrontItem(int itemIndex)
|
||||
else {
|
||||
return;
|
||||
}
|
||||
qDebug() << "------2---------DEL_USER----------------";
|
||||
}
|
||||
else if (currentItem.contains("cmd") && currentItem["cmd"].toString() == "PASSWD_ENROLL") {
|
||||
PasswordEnrollWindow dialog;
|
||||
@@ -549,7 +779,8 @@ void SerialPortHandler::sendFrontItem(int itemIndex)
|
||||
return;
|
||||
}
|
||||
}
|
||||
sendJsonItem(frontBoardTest, itemIndex, text, "", "test");
|
||||
qDebug() << "---------------sendJsonItem----------------";
|
||||
sendJsonItem(frontBoardTest, itemIndex, text, "", "test", bmpData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -564,7 +795,7 @@ void SerialPortHandler::sendDevInfoJsonItem(const QJsonObject& jsonItem, int ite
|
||||
QString itemData = QJsonDocument(jsonItem).toJson();
|
||||
//emit sendData(itemData.toUtf8());
|
||||
// 发送 JSON
|
||||
sendData(itemData.toUtf8(), 0);
|
||||
sendData(itemData.toUtf8(), 0, "");
|
||||
if (jsonItem.contains("timeout")) {
|
||||
int timeout = jsonItem.value("timeout").toInt();
|
||||
if (timeout > 0) {
|
||||
@@ -579,11 +810,6 @@ void SerialPortHandler::sendDevInfoJsonItem(const QJsonObject& jsonItem, int ite
|
||||
|
||||
void SerialPortHandler::sendDevInfoItem()
|
||||
{
|
||||
qDebug() << "----------- currentFrontBoardIndex:" << currentFrontBoardIndex;
|
||||
qDebug() << "----------- currentFrontBoardIndex:" << currentFrontBoardIndex;
|
||||
qDebug() << "----------- currentFrontBoardIndex:" << currentFrontBoardIndex;
|
||||
qDebug() << "----------- currentFrontBoardIndex:" << currentFrontBoardIndex;
|
||||
qDebug() << "----------- currentFrontBoardIndex:" << currentFrontBoardIndex;
|
||||
currentJson = frontBoardDevInfoJson;
|
||||
currentJsonItem = currentFrontBoardIndex;
|
||||
if (currentFrontBoardIndex < frontBoardDevInfoJson.size()) {
|
||||
@@ -593,6 +819,26 @@ void SerialPortHandler::sendDevInfoItem()
|
||||
isPowerOnSend = false;
|
||||
}
|
||||
|
||||
void SerialPortHandler::onUserInputReceived(const QString& userInput) {
|
||||
if (!userInput.isEmpty() && currentItem.contains("val")) {
|
||||
currentItem["val"] = userInput;
|
||||
QString itemData = QJsonDocument(currentItem).toJson();
|
||||
isTimeout = false;
|
||||
emit updateTimeout(isTimeout);
|
||||
sendData(itemData.toUtf8(), 0, "");
|
||||
if (currentItem.contains("timeout")) {
|
||||
int timeout = currentItem.value("timeout").toInt();
|
||||
if (timeout > 0) {
|
||||
//OnStartTimeout(timeout);
|
||||
emit startTimeout(timeout);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 如果没有 timeout 字段,则不设置超时处理,一直等待数据接收
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SerialPortHandler::sendNextItem(QJsonArray& currentOneClickedItem)
|
||||
{
|
||||
QMutexLocker locker(&mutex);
|
||||
@@ -607,36 +853,47 @@ void SerialPortHandler::sendNextItem(QJsonArray& currentOneClickedItem)
|
||||
QString itemData = QJsonDocument(currentItem).toJson();
|
||||
//qDebug() << "Sending item index:" << currentItemIndex << "data:" << itemData;
|
||||
if (currentItem.contains("cmd") && currentItem["cmd"].toString() == "DEL_USER") {
|
||||
QString userInput;
|
||||
// 使用 QMetaObject::invokeMethod 将窗口操作移到主线程
|
||||
QMetaObject::invokeMethod(QApplication::instance(), [&userInput]() {
|
||||
DelUserWindow dialog;
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
userInput = dialog.getUserInput();
|
||||
DelUserWindow dialog;
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
QString userInput = dialog.getUserInput();
|
||||
if (!userInput.isEmpty() && currentItem.contains("val")) {
|
||||
currentItem["val"] = userInput;
|
||||
itemData = QJsonDocument(currentItem).toJson();
|
||||
isTimeout = false;
|
||||
emit updateTimeout(isTimeout);
|
||||
// 发送 JSON
|
||||
sendData(itemData.toUtf8(), 0, "");
|
||||
if (currentItem.contains("timeout")) {
|
||||
int timeout = currentItem.value("timeout").toInt();
|
||||
if (timeout > 0) {
|
||||
//OnStartTimeout(timeout);
|
||||
emit startTimeout(timeout);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 如果没有 timeout 字段,则不设置超时处理,一直等待数据接收
|
||||
}
|
||||
}
|
||||
}, Qt::BlockingQueuedConnection); // 阻塞子线程,直到窗口操作完成
|
||||
|
||||
if (!userInput.isEmpty() && currentItem.contains("val")) {
|
||||
currentItem["val"] = userInput;
|
||||
itemData = QJsonDocument(currentItem).toJson();
|
||||
}
|
||||
else {
|
||||
return; // 用户取消或输入无效,直接返回
|
||||
}
|
||||
}
|
||||
isTimeout = false;
|
||||
emit updateTimeout(isTimeout);
|
||||
// 发送 JSON
|
||||
sendData(itemData.toUtf8(), 0);
|
||||
if (currentItem.contains("timeout")) {
|
||||
int timeout = currentItem.value("timeout").toInt();
|
||||
if (timeout > 0) {
|
||||
//OnStartTimeout(timeout);
|
||||
emit startTimeout(timeout);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 如果没有 timeout 字段,则不设置超时处理,一直等待数据接收
|
||||
isTimeout = false;
|
||||
emit updateTimeout(isTimeout);
|
||||
// 发送 JSON
|
||||
sendData(itemData.toUtf8(), 0, "");
|
||||
if (currentItem.contains("timeout")) {
|
||||
int timeout = currentItem.value("timeout").toInt();
|
||||
if (timeout > 0) {
|
||||
//OnStartTimeout(timeout);
|
||||
emit startTimeout(timeout);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// 如果没有 timeout 字段,则不设置超时处理,一直等待数据接收
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (isManualSend) {
|
||||
@@ -651,22 +908,9 @@ void SerialPortHandler::sendNextItem(QJsonArray& currentOneClickedItem)
|
||||
|
||||
void SerialPortHandler::onRecvNormalDataFlag(bool flag)
|
||||
{
|
||||
|
||||
if (!(protocolDataFlag = flag)) {
|
||||
return;
|
||||
}
|
||||
/*if (isPowerOnSend && protocolDataFlag) {
|
||||
if (currentFrontBoardIndex < frontBoardDevInfoJson.size()) {
|
||||
qDebug() << "-----" << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");
|
||||
sendDevInfoItem();
|
||||
}
|
||||
else {
|
||||
qDebug() << "All items processed in onDataReceived.";
|
||||
emit allItemsProcessed(isBackBoardOrAllBoard, "", currentFrontBoardIndex);
|
||||
isPowerOnSend = false;
|
||||
}
|
||||
protocolDataFlag = false;
|
||||
}*/
|
||||
QJsonObject currentTempItem = currentJson[currentItemIndex].toObject();
|
||||
if (!isSingleSend && !isPowerOnSend && (currentTempItem["cmd"] != "GET_IMG")) {
|
||||
qDebug() << "------------------- onRecvNormalDataFlag ";
|
||||
@@ -681,7 +925,7 @@ void SerialPortHandler::onRecvNormalDataFlag(bool flag)
|
||||
}
|
||||
else if (isPowerOnSend) {
|
||||
if (currentFrontBoardIndex < frontBoardDevInfoJson.size()) {
|
||||
qDebug() << "-----" << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");
|
||||
qDebug() << "--2---" << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");
|
||||
sendDevInfoItem();
|
||||
}
|
||||
else {
|
||||
@@ -689,6 +933,9 @@ void SerialPortHandler::onRecvNormalDataFlag(bool flag)
|
||||
emit allItemsProcessed(isBackBoardOrAllBoard, "", currentFrontBoardIndex);
|
||||
currentFrontBoardIndex = 0;
|
||||
isPowerOnSend = false;
|
||||
#if CONNECT_COM_TO_VERIFY
|
||||
//sendFrontItem(3, "");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else if (isClickedSend && (currentItemIndex < currentJson.size())) {
|
||||
@@ -711,7 +958,8 @@ void SerialPortHandler::onDataReceived()
|
||||
qDebug() << "isPowerOnSend:" << isPowerOnSend;
|
||||
qDebug() << "isClickedSend:" << isClickedSend;
|
||||
qDebug() << "isSingleSend:" << isSingleSend;
|
||||
qDebug() << "isRecvImgData:" << isRecvImgData;
|
||||
qDebug() << "isRecvImgData:" << isRecvImgData;
|
||||
qDebug() << "dataProcessingActive:" << dataProcessingActive;
|
||||
#if 1
|
||||
if (!isRecvVideoData &&
|
||||
(isRecvImgData || isPowerOnSend || isClickedSend || (isSingleSend && (currentItemIndex < currentJson.size())))) {
|
||||
@@ -742,41 +990,8 @@ void SerialPortHandler::onDataReceived()
|
||||
QJsonObject currentTempItem = currentJson[currentItemIndex].toObject();
|
||||
//qDebug() << "---Received allData size:" << allData.size();
|
||||
if (!allData.isEmpty()) {
|
||||
emit dataReceived("", allData, 0xFF, currentItemIndex, currentFuncItemIndex, getCurrentItemLable(), "", currentJson, currentJsonItem, isTimeout);
|
||||
/*if (!isSingleSend && !isPowerOnSend && (currentTempItem["cmd"] != "GET_IMG")) {
|
||||
currentItemIndex ++;
|
||||
itemsProcessedCount ++;
|
||||
}*/
|
||||
emit dataReceived("", allData, 0xFF, currentItemIndex, currentFuncItemIndex, getCurrentItemLable(), "", currentJson, currentJsonItem, isTimeout);
|
||||
}
|
||||
/*if (isSingleSend) {
|
||||
if (currentTempItem["cmd"] != "GET_IMG") {
|
||||
isSingleSend = false;
|
||||
}
|
||||
isClickedSend = false;
|
||||
}
|
||||
else if (isPowerOnSend && protocolDataFlag) {
|
||||
if (currentFrontBoardIndex < frontBoardDevInfoJson.size()) {
|
||||
qDebug() << "-----" << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz");
|
||||
sendDevInfoItem();
|
||||
}
|
||||
else {
|
||||
qDebug() << "All items processed in onDataReceived.";
|
||||
emit allItemsProcessed(isBackBoardOrAllBoard, "", currentFrontBoardIndex);
|
||||
isPowerOnSend = false;
|
||||
}
|
||||
protocolDataFlag = false;
|
||||
}
|
||||
else if (isClickedSend && (currentItemIndex < currentJson.size())) {
|
||||
// 判断是否是取图或者其他会接收很多数据的指令
|
||||
if (currentTempItem["cmd"] != "GET_IMG") {
|
||||
sendNextItem(currentJson);
|
||||
}
|
||||
}
|
||||
else if (isClickedSend) {
|
||||
emit allItemsProcessed(isBackBoardOrAllBoard, "", itemsProcessedCount);
|
||||
isClickedSend = false;
|
||||
//resetCurrentItemIndex();
|
||||
}*/
|
||||
}
|
||||
else if (isRecvVideoData && (!dataProcessingActive)) {
|
||||
dataProcessingActive = true;
|
||||
@@ -885,6 +1100,9 @@ void SerialPortHandler::onTimeout()
|
||||
emit allItemsProcessed(isBackBoardOrAllBoard, "", currentFrontBoardIndex);
|
||||
currentFrontBoardIndex = 0;
|
||||
isPowerOnSend = false;
|
||||
#if CONNECT_COM_TO_VERIFY
|
||||
//sendFrontItem(3, "");
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,10 +13,14 @@
|
||||
#include <QEventLoop>
|
||||
#include <QApplication>
|
||||
|
||||
//#include <QFile>
|
||||
//#include <QDataStream>
|
||||
|
||||
#include "SerialPortWorker.h"
|
||||
#include "../../Network/DelUserWindows.h"
|
||||
#include "../../Network/ImageEnrollWindow.h"
|
||||
#include "../../Network/PasswordEnrollWindow.h"
|
||||
#include "../UI_Widget/UI_Name.h"
|
||||
|
||||
#define TEST_TCP_MOVE_TO_MAIN 0
|
||||
#define GET_PIC_DATA_SIZE (480 * 640 * 1.5)
|
||||
@@ -25,11 +29,14 @@
|
||||
#define CHANGE_THEME_W 720
|
||||
#define CHANGE_THEME_H 1280
|
||||
|
||||
#define UPDATE_LOGO 0
|
||||
#define CONNECT_COM_TO_VERIFY 1
|
||||
|
||||
class SerialPortHandler : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SerialPortHandler(QComboBox* comPortComboBox, QPushButton* connectButton,
|
||||
explicit SerialPortHandler(const UI_config& config, QComboBox* comPortComboBox, QPushButton* connectButton,
|
||||
QJsonArray frontBoardOneClickTest, QJsonArray frontBoardTest, QJsonArray frontBoardFuncConfig,
|
||||
QJsonArray frontBoardDevInfoJson, QJsonArray frontBoardLicenseJson,
|
||||
QJsonArray backBoardOneClickTest, QJsonArray backBoardTest, QJsonArray backBoardFuncConfig, QJsonArray backBoardDevInfoJson,
|
||||
@@ -37,13 +44,15 @@ public:
|
||||
QObject* parent = nullptr);
|
||||
~SerialPortHandler();
|
||||
|
||||
void sendData(const QByteArray& data, bool heartBeat);
|
||||
//void sendData(const QByteArray& data, bool heartBeat);
|
||||
void sendData(QByteArray data, bool heartBeat, const QByteArray& bmpData);
|
||||
void sendFinalData(QByteArray data, bool heartBeat, QByteArray bmpData);
|
||||
void onPicRecvFinished();
|
||||
void onCommandError();
|
||||
|
||||
void sendDevInfoItem();
|
||||
|
||||
void sendJsonItem(const QJsonArray& jsonArray, int itemIndex, const QString text, const QString backBoardSn, const QString& itemType);
|
||||
void sendJsonItem(const QJsonArray& jsonArray, int itemIndex, const QString text, const QString backBoardSn, const QString& itemType, const QByteArray& bmpData);
|
||||
// 发送获取设备信息按键
|
||||
void sendGetDevInfoItem(int itemIndex);
|
||||
// 发送取图按键
|
||||
@@ -53,9 +62,9 @@ public:
|
||||
// 发送License处理按键
|
||||
void sendLicenseItem(int itemIndex, const QString text);
|
||||
// 发送下一个功能配置 JSON 项目
|
||||
void sendFrontFuncItem(int itemIndex, QString text);
|
||||
void sendFrontFuncItem(int itemIndex, QString text, const QByteArray& bmpData);
|
||||
// 处理发送单独指令
|
||||
void sendFrontItem(int itemIndex);
|
||||
void sendFrontItem(int itemIndex, const QByteArray& bmpData);
|
||||
// 发送下一个 JSON 项目
|
||||
void sendNextItem(QJsonArray& currentOneClickedItem);
|
||||
// 获取json文件中当前发送的 data 字段项
|
||||
@@ -72,6 +81,9 @@ public:
|
||||
|
||||
|
||||
public slots:
|
||||
void onUserInputReceived(const QString& userInput);
|
||||
void updateSendHeartBeat();
|
||||
//void userInputEntered(const QString&); // 主线程返回用户输入
|
||||
void handleSerialError(QSerialPort::SerialPortError error);
|
||||
void updateAvailablePorts();
|
||||
void toggleConnection();
|
||||
@@ -90,9 +102,17 @@ public slots:
|
||||
void initializeTimers();
|
||||
|
||||
void onRecvNormalDataFlag(bool flag);
|
||||
|
||||
//void onDelUserDialogResult(const QString& input);
|
||||
#if UPDATE_LOGO
|
||||
void onBmpFileSelected(QString path); // 处理文件路径
|
||||
#endif
|
||||
|
||||
signals:
|
||||
void requestUserInput(); // 请求主线程打开对话框
|
||||
void userInputReceived(QString userInput);
|
||||
void selectBmpFile(); // 请求选择 BMP 文件
|
||||
void bmpFileSelected(QString path); // 选中文件后返回路径
|
||||
void serialDataReceived(const QByteArray& data);
|
||||
void serialComPortConnected();
|
||||
void serialComPortDisconnected();
|
||||
@@ -160,10 +180,12 @@ private:
|
||||
QPushButton* connectButton;
|
||||
QSerialPort* serialPort;
|
||||
QTimer* portDetectionTimer;
|
||||
QTimer* sendHeartBeatTimer;
|
||||
QTimer* serialTimer;
|
||||
QStringList comPortList; // 当前已检测到的串口列表
|
||||
SerialPortWorker* worker;
|
||||
|
||||
UI_config TOOL_UI;
|
||||
QByteArray lastData; // 存储 JSON 数据,等待 BMP 文件选择完成后继续处理
|
||||
|
||||
void sendDevInfoJsonItem(const QJsonObject& jsonItem, int itemIndex);
|
||||
};
|
||||
|
||||
113
FactoryTestTool/SourceCode/Widget/SerialPortHandler/crc32.cpp
Normal file
113
FactoryTestTool/SourceCode/Widget/SerialPortHandler/crc32.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
|
||||
static const unsigned int crc_table[256] = {
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
|
||||
0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
|
||||
0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
|
||||
0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
|
||||
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
|
||||
0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
|
||||
0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
|
||||
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
|
||||
0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
|
||||
0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
|
||||
0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
|
||||
0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
|
||||
0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
|
||||
0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
|
||||
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
|
||||
0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
||||
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
|
||||
0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
|
||||
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
|
||||
0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
|
||||
0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
|
||||
0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
||||
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
|
||||
0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
|
||||
0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
|
||||
0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
|
||||
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
|
||||
0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
|
||||
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
|
||||
0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
|
||||
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
|
||||
0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
|
||||
0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
|
||||
0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
|
||||
0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
|
||||
0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
|
||||
0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
|
||||
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
|
||||
0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
|
||||
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
|
||||
0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
|
||||
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
|
||||
0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
|
||||
0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
|
||||
0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
|
||||
0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
|
||||
0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
|
||||
0x2d02ef8d
|
||||
};
|
||||
|
||||
#define DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
|
||||
#define DO2(buf) do {DO1(buf); DO1(buf); } while (0)
|
||||
#define DO4(buf) do {DO2(buf); DO2(buf); } while (0)
|
||||
#define DO8(buf) do {DO4(buf); DO4(buf); } while (0)
|
||||
|
||||
unsigned int crc32(unsigned int crc, const void * b, size_t len)
|
||||
{
|
||||
if (len == 0)
|
||||
return crc;
|
||||
const unsigned char * buf = (const unsigned char *)b;
|
||||
crc = crc ^ 0xffffffff;
|
||||
while (len >= 8) {
|
||||
DO8(buf);
|
||||
len -= 8;
|
||||
}
|
||||
|
||||
if (len) {
|
||||
do {
|
||||
DO1(buf);
|
||||
} while (--len);
|
||||
}
|
||||
return crc ^ 0xffffffff;
|
||||
}
|
||||
|
||||
void crc32init(unsigned int *crc)
|
||||
{
|
||||
*crc = 0;
|
||||
(*crc) = (*crc) ^ 0xffffffff;
|
||||
}
|
||||
|
||||
void crc32update(unsigned int *crc, const void * b, size_t len)
|
||||
{
|
||||
const unsigned char * buf = (const unsigned char *)b;
|
||||
do {
|
||||
*crc = crc_table[((int)(*crc) ^ (*buf++)) & 0xff] ^ ((*crc) >> 8);
|
||||
} while (--len);
|
||||
}
|
||||
void crc32final(unsigned int *crc)
|
||||
{
|
||||
*crc = (*crc) ^ 0xffffffff;
|
||||
}
|
||||
|
||||
unsigned int crcsimple(const void *buf, size_t len){
|
||||
unsigned int CRC = 0;
|
||||
unsigned int POLYNOMIAL = 0x0100a001;
|
||||
size_t i;
|
||||
size_t sli = 1;
|
||||
if (len > 100){
|
||||
sli = len /100;
|
||||
}
|
||||
char *p = (char*)buf;
|
||||
for (i = 0; i < len; i+= sli) {
|
||||
CRC ^= p[i] & 0x000000ff;
|
||||
}
|
||||
return CRC ;
|
||||
}
|
||||
37
FactoryTestTool/SourceCode/Widget/SerialPortHandler/crc32.h
Normal file
37
FactoryTestTool/SourceCode/Widget/SerialPortHandler/crc32.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef _crc32_h_
|
||||
#define _crc32_h_
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct BMPHeader {
|
||||
char bfType[2];
|
||||
uint32_t bfSize;
|
||||
uint16_t bfReserved1;
|
||||
uint16_t bfReserved2;
|
||||
uint32_t bfOffBits;
|
||||
uint32_t biSize;
|
||||
int32_t biWidth;
|
||||
int32_t biHeight;
|
||||
uint16_t biPlanes;
|
||||
uint16_t biBitCount;
|
||||
uint32_t biCompression;
|
||||
uint32_t biSizeImage;
|
||||
int32_t biXPelsPerMeter;
|
||||
int32_t biYPelsPerMeter;
|
||||
uint32_t biClrUsed;
|
||||
uint32_t biClrImportant;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
unsigned int crc32(unsigned int crc, const void *buf, size_t len);
|
||||
void crc32init(unsigned int *crc);
|
||||
void crc32update(unsigned int *crc, const void * b, size_t len);
|
||||
void crc32final(unsigned int *crc);
|
||||
unsigned int crcsimple(const void *buf, size_t len);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user