|
@@ -3,7 +3,7 @@
|
|
|
// // \ V/ @Brief
|
|
|
// // | "") @Author lijinwen, ghz005@uni-trend.com.cn
|
|
|
// // / | @Creation 2024-05-16
|
|
|
-// // / \\ @Modified 2024-05-16
|
|
|
+// // / \\ @Modified 2024-05-30
|
|
|
// // *(__\_\
|
|
|
// // ******************************************************************
|
|
|
|
|
@@ -15,439 +15,437 @@
|
|
|
|
|
|
namespace Protocol
|
|
|
{
|
|
|
-
|
|
|
- std::vector<RS232DecodeResultCell> decodeResultUnits;
|
|
|
- std::vector<RS232DecodeEventUnit> decodeEventUnitsStorage;
|
|
|
- std::vector<RS232DecodeEvent> decodeEvents;
|
|
|
-
|
|
|
-
|
|
|
- int32_t getStartIndex(const ProtocolRS232Options& options, TwoLevelEdgePulse*& node,
|
|
|
- double count,
|
|
|
- double& realCount,
|
|
|
- const int32_t startIndex,
|
|
|
- TwoLevelEdgePulseStatusType& startStatus)
|
|
|
- {
|
|
|
- startStatus = options.Polarity == Polarity::Pos
|
|
|
- ? TwoLevelEdgePulseStatusType::High
|
|
|
- : TwoLevelEdgePulseStatusType::Low;
|
|
|
-
|
|
|
- while (node != nullptr)
|
|
|
- {
|
|
|
- if (node->CurrentLevel != startStatus && node->StartIndex >= startIndex)
|
|
|
- {
|
|
|
- //计算该脉宽中,最大包括几个bit信息宽度
|
|
|
- const double bitCount = std::round(node->GetLength() / count);
|
|
|
- if (bitCount > 0 && bitCount < static_cast<int32_t>(options.DataBitWidth) + 4)
|
|
|
- {
|
|
|
- realCount = node->GetLength() / bitCount;
|
|
|
- }
|
|
|
- if (bitCount > 0) break;
|
|
|
- }
|
|
|
-
|
|
|
- node++;
|
|
|
- }
|
|
|
- return node == nullptr ? -1 : node->StartIndex;
|
|
|
- }
|
|
|
-
|
|
|
- bool pauseDataRS232(const ProtocolRS232Options& options, TwoLevelEdgePulse* edgePulses,
|
|
|
- const uint64_t edgePulseCount,
|
|
|
- const uint64_t waveformDataCount, const double sampleRate,
|
|
|
- RS232DecodeResult& decodeResult)
|
|
|
-
|
|
|
- {
|
|
|
- canceled = false;
|
|
|
- WriteLog(LogLevel::LevelDebug, " SignalType:%d, DataBitWidth:%d"
|
|
|
- , static_cast<int>(options.SignalType), static_cast<int>(options.DataBitWidth));
|
|
|
- if (canceled)
|
|
|
- {
|
|
|
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- //栈上 初始化内存
|
|
|
- std::vector<RS232Packet> rs232Packets;
|
|
|
- rs232Packets = {};
|
|
|
- decodeEvents = {};
|
|
|
- decodeResultUnits = {};
|
|
|
-
|
|
|
- auto decodeResultUnitsPtr = &decodeResultUnits;
|
|
|
- auto decodeEventsPtr = &decodeEvents;
|
|
|
- WriteLog(LogLevel::LevelDebug, " decodeResultUnitsPtr:0x%X\n", static_cast<void*>(decodeResultUnitsPtr));
|
|
|
- WriteLog(LogLevel::LevelDebug, " decodeEventsPtr:0x%X\n", static_cast<void*>(decodeEventsPtr));
|
|
|
- canceled = false;
|
|
|
-
|
|
|
- if (canceled)
|
|
|
- {
|
|
|
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (edgePulses == nullptr)
|
|
|
- {
|
|
|
- WriteLog(LogLevel::Level2, " EdgePulseDataPtr is nullptr\n");
|
|
|
- return false;
|
|
|
- }
|
|
|
- //if (edgePulseData.edgePulses.empty())
|
|
|
- if (waveformDataCount == 0)
|
|
|
- {
|
|
|
- std::cout << " DataCount is 0 or sampleRate error\n";
|
|
|
- WriteLog(LogLevel::Level2, " DataCount is 0 or sampleRate error\n");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- int32_t dataBitCount = 0;
|
|
|
- switch (options.DataBitWidth)
|
|
|
- {
|
|
|
- case RS232Enums::DataBitWidth::DataBitWidth_5Bit:
|
|
|
- dataBitCount = 5;
|
|
|
- break;
|
|
|
- case RS232Enums::DataBitWidth::DataBitWidth_6Bit:
|
|
|
- dataBitCount = 6;
|
|
|
- break;
|
|
|
- case RS232Enums::DataBitWidth::DataBitWidth_7Bit:
|
|
|
- dataBitCount = 7;
|
|
|
- break;
|
|
|
- case RS232Enums::DataBitWidth::DataBitWidth_8Bit:
|
|
|
- dataBitCount = 8;
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- uint64_t edgePulseIndex = 0;
|
|
|
-
|
|
|
- //每bit理论长度
|
|
|
- const double count = 1.0 / options.BaudRate * sampleRate;
|
|
|
- WriteLog(LogLevel::LevelDebug, " bit count:%f", count);
|
|
|
- //const double count = static_cast<double>(dataCount);
|
|
|
- //每bit实际长度
|
|
|
- double realCount = count;
|
|
|
-
|
|
|
- const int32_t stopBitCount = options.StopBit == RS232Enums::StopBit::StopBit_1bit
|
|
|
- ? 1
|
|
|
- : options.StopBit == RS232Enums::StopBit::StopBit_2bit
|
|
|
- ? 2
|
|
|
- : 1;
|
|
|
-
|
|
|
- WriteLog(LogLevel::LevelDebug, " realCount:%f", realCount);
|
|
|
- WriteLog(LogLevel::LevelDebug, " edgePulsePointer:0x%x", reinterpret_cast<void*>(edgePulses));
|
|
|
- //WriteLog(" edgePulsePointer:0x%x", edgePulses);
|
|
|
- WriteLog(LogLevel::LevelDebug, " edgePulse StartIndex:%d", edgePulses->StartIndex);
|
|
|
- WriteLog(LogLevel::LevelDebug, " edgePulse EndIndex:%d", edgePulses->EndIndex);
|
|
|
- WriteLog(LogLevel::LevelDebug, " edgePulseCount:%d", edgePulseCount);
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
- int32_t startIndex = 0;
|
|
|
-
|
|
|
- while (edgePulses != nullptr && edgePulseIndex < edgePulseCount)
|
|
|
- {
|
|
|
- if (count <= 2) break;
|
|
|
- TwoLevelEdgePulseStatusType levelState;
|
|
|
- //找到帧头 根据帧头得到实际bit宽度realCount 帧头起始位置startIndex 电平状态levelState
|
|
|
- int32_t packetStartIndex =
|
|
|
- getStartIndex(options, edgePulses, count, realCount, startIndex, levelState);
|
|
|
- //WriteLog(" GetStartIndex PacketStartIndex:%d\n", packetStartIndex);
|
|
|
- if (canceled)
|
|
|
- {
|
|
|
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (packetStartIndex == -1) break;
|
|
|
- //std::cout << " packetStartIndex:" << packetStartIndex << " ,realCount:" << realCount << "\n";
|
|
|
- //构造帧
|
|
|
- RS232Packet packet = {};
|
|
|
- packetStartIndex += static_cast<int32_t>(std::round(realCount / 2));
|
|
|
-
|
|
|
- if (edgePulses->StartIndex >= edgePulses->EndIndex || !CommonHelper::EnumIsDefined<
|
|
|
- TwoLevelEdgePulseStatusType>(static_cast<int>(edgePulses->CurrentLevel)))
|
|
|
- {
|
|
|
- std::ostringstream msg;
|
|
|
- msg << " =>178 loop break Index:" << packetStartIndex << " ,dataCount:" <<
|
|
|
- waveformDataCount << "\n";
|
|
|
- WriteLog(LogLevel::LevelDebug, msg.str().c_str());
|
|
|
- break;
|
|
|
- }
|
|
|
- if (packetStartIndex >= static_cast<int32_t>(waveformDataCount))
|
|
|
- {
|
|
|
- std::ostringstream msg;
|
|
|
- msg << " =>186 loop break Index:" << packetStartIndex << " ,dataCount:" <<
|
|
|
- waveformDataCount << "\n";
|
|
|
- WriteLog(LogLevel::LevelDebug, msg.str().c_str());
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- if (startIndex == static_cast<int32_t>(waveformDataCount)) break;
|
|
|
-
|
|
|
- packet.StartBit = levelState == TwoLevelEdgePulseStatusType::High;
|
|
|
- packet.StartIndex = packetStartIndex - static_cast<int32_t>(std::round(realCount / 2));
|
|
|
- packet.DataIndex = packetStartIndex + static_cast<int32_t>(std::round(realCount / 2));
|
|
|
- packet.PerBitLenght = realCount;
|
|
|
-
|
|
|
- WriteLog(LogLevel::LevelDebug, "=> Tst StartIndex:%d,dataBitCount:%d", packet.StartIndex, dataBitCount);
|
|
|
- for (int32_t dataBitIndex = 0; dataBitIndex < dataBitCount; dataBitIndex++)
|
|
|
- {
|
|
|
- if (canceled)
|
|
|
- {
|
|
|
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- packetStartIndex += static_cast<int32_t>(std::round(realCount));
|
|
|
-
|
|
|
- //std::cout << " P150 Test" << "\n";
|
|
|
- if (packetStartIndex >= static_cast<int32_t>(waveformDataCount))
|
|
|
- {
|
|
|
- startIndex = static_cast<int32_t>(waveformDataCount);
|
|
|
- WriteLog(LogLevel::LevelDebug, " =>%d break \n", __LINE__);
|
|
|
- break;
|
|
|
- }
|
|
|
- //std::cout << " P157 Test" << "\n";
|
|
|
- TwoLevelEdgePulseStatusType bitStatus;
|
|
|
- if (!getRS232Bit(edgePulses, packetStartIndex, bitStatus))
|
|
|
- {
|
|
|
- //gMutex.unlock(); // 执行完操作后手动释放锁
|
|
|
- return false;
|
|
|
- }
|
|
|
- //按位构造数据
|
|
|
- if (options.MSBOrLSB == MSBOrLSB::MSB)
|
|
|
- {
|
|
|
- packet.Data = static_cast<uint8_t>(packet.Data << 1);
|
|
|
- packet.Data |= bitStatus == TwoLevelEdgePulseStatusType::High ? 1 : 0;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- packet.Data |= static_cast<uint8_t>((bitStatus ==
|
|
|
- TwoLevelEdgePulseStatusType::High
|
|
|
- ? 1
|
|
|
- : 0) << dataBitIndex);
|
|
|
- }
|
|
|
- WriteLog(LogLevel::LevelDebug, "Tst CurrentLevel:%d, bitStatus:%d", edgePulses->CurrentLevel, bitStatus);
|
|
|
- }
|
|
|
- //std::cout << " P177 Test" << "\n";
|
|
|
- if (startIndex == static_cast<int32_t>(waveformDataCount)) break;
|
|
|
- //校验位
|
|
|
- if (options.OddEvenCheckType != OddEvenCheck::None)
|
|
|
- {
|
|
|
- packetStartIndex += static_cast<int32_t>(std::round(realCount));
|
|
|
-
|
|
|
- if (packetStartIndex >= static_cast<int32_t>(waveformDataCount))
|
|
|
- {
|
|
|
- //startIndex = static_cast<int32_t>(bufferLength);
|
|
|
- rs232Packets.push_back(packet);
|
|
|
- WriteLog(LogLevel::LevelDebug, " =>%d break \n", __LINE__);
|
|
|
- break;
|
|
|
- }
|
|
|
- TwoLevelEdgePulseStatusType bitStatus;
|
|
|
- if (!getRS232Bit(edgePulses, packetStartIndex, bitStatus))
|
|
|
- {
|
|
|
- //gMutex.unlock(); // 执行完操作后手动释放锁
|
|
|
- return false;
|
|
|
- }
|
|
|
- packet.ParityFind = true;
|
|
|
- packet.ParityBit = bitStatus == TwoLevelEdgePulseStatusType::High;
|
|
|
- packet.ParityResult = DataCheckHelper::CheckDataByOddEven(
|
|
|
- packet.Data, dataBitCount, options.OddEvenCheckType);
|
|
|
- packet.ParityIndex = packet.DataIndex + static_cast<int32_t>(
|
|
|
- std::round(realCount * dataBitCount));
|
|
|
- }
|
|
|
- //std::cout << " P203 Test" << "\n";
|
|
|
- rs232Packets.push_back(packet);
|
|
|
-
|
|
|
- packetStartIndex += static_cast<int32_t>(realCount * stopBitCount);
|
|
|
- startIndex = packetStartIndex;
|
|
|
- edgePulseIndex++;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (const char* ex)
|
|
|
- {
|
|
|
- WriteLog(LogLevel::LevelDebug, " ParsingData L%d catch :%s", __LINE__, ex);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- {
|
|
|
- std::ostringstream msg;
|
|
|
- msg << " edgePulseIndex:" << edgePulseIndex << ",";
|
|
|
- msg << " rs232Packets count:" << rs232Packets.size() << ",";
|
|
|
- msg << " get events && results";
|
|
|
- WriteLog(LogLevel::Level2, msg.str().c_str());
|
|
|
- }
|
|
|
- //get events && results
|
|
|
- //std::cout << " P218 Test rs232Packets Count:" << rs232Packets.size() << "\n";
|
|
|
- if (!rs232Packets.empty())
|
|
|
- {
|
|
|
- decodeEventUnitsStorage = {};
|
|
|
- //for (int64_t i = 0; i < static_cast<int64_t>(rs232Packets.size()); i++)
|
|
|
- for (auto packet : rs232Packets)
|
|
|
- {
|
|
|
- if (canceled)
|
|
|
- {
|
|
|
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
- //gMutex.unlock(); // 执行完操作后手动释放锁
|
|
|
- return false;
|
|
|
- }
|
|
|
- RS232DecodeResultCell resultUnit;
|
|
|
- RS232DecodeEvent decodeEvent = {};
|
|
|
- auto eventPtr = &decodeEvent;
|
|
|
- RS232DecodeEventUnit eventStart;
|
|
|
- RS232DecodeEventUnit eventData;
|
|
|
- int64_t eventStartIndex = packet.StartIndex;
|
|
|
- int64_t oneBitLength = static_cast<int64_t>(packet.PerBitLenght);
|
|
|
-
|
|
|
- eventPtr->StartIndex = packet.StartIndex;
|
|
|
-
|
|
|
- eventStart.StartIndex = packet.StartIndex;
|
|
|
- eventStart.Length = oneBitLength;
|
|
|
- eventStartIndex += 1;
|
|
|
- eventStart.EventType = RS232Enums::RS232DecodeEventType::Start;
|
|
|
-
|
|
|
- eventData.StartIndex = eventStart.StartIndex;
|
|
|
- eventData.Length = oneBitLength * dataBitCount;
|
|
|
- eventData.Data = packet.Data;
|
|
|
- eventData.EventType = RS232Enums::RS232DecodeEventType::Data;
|
|
|
- eventStartIndex += 8;
|
|
|
-
|
|
|
-
|
|
|
- //std::cout << " P249 Test I:" << i << "\n";
|
|
|
- if (packet.ParityFind)
|
|
|
- {
|
|
|
- RS232DecodeEventUnit eventUints[3];
|
|
|
- eventUints[0] = eventStart;
|
|
|
- eventUints[1] = eventData;
|
|
|
- RS232DecodeEventUnit eventParity;
|
|
|
- //std::cout << " P253 Test I:" << i << "\n";
|
|
|
- eventParity.Data = packet.ParityResult ? 1 : 0;
|
|
|
- eventParity.StartIndex = eventStartIndex;
|
|
|
- eventParity.Length = oneBitLength;
|
|
|
- eventParity.EventType = RS232Enums::RS232DecodeEventType::Parity;
|
|
|
- eventPtr->ParityResult = packet.ParityResult ? 1 : 0;
|
|
|
- eventUints[2] = eventParity;
|
|
|
- //eventPtr->EventData = eventUints;
|
|
|
- eventStartIndex += 8;
|
|
|
- eventPtr->EventDataCount = 3;
|
|
|
- eventPtr->EndIndex = packet.StartIndex + oneBitLength * (dataBitCount + 1);
|
|
|
- // 将eventUints复制到decodeEventUnitsStorage中
|
|
|
- decodeEventUnitsStorage.insert(decodeEventUnitsStorage.end(), std::begin(eventUints),
|
|
|
- std::end(eventUints));
|
|
|
- //最后来赋值
|
|
|
- //decodeEvent.EventData = decodeEventUnitsStorage.data() + decodeEventUnitsStorage.size() - 3;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- RS232DecodeEventUnit eventUints[2];
|
|
|
- eventUints[0] = eventStart;
|
|
|
- eventUints[1] = eventData;
|
|
|
- eventPtr->EventDataCount = 2;
|
|
|
-
|
|
|
- //eventPtr->EventData = eventUints;
|
|
|
- //std::cout << " P263 Test I:" << i << "\n";
|
|
|
- eventPtr->ParityResult = 0;
|
|
|
- eventPtr->EndIndex = packet.StartIndex + oneBitLength * (dataBitCount);
|
|
|
- // 将eventUints复制到decodeEventUnitsStorage中
|
|
|
- decodeEventUnitsStorage.insert(decodeEventUnitsStorage.end(), std::begin(eventUints),
|
|
|
- std::end(eventUints));
|
|
|
- //最后来赋值
|
|
|
- //decodeEvent.EventData = decodeEventUnitsStorage.data() + decodeEventUnitsStorage.size() - 2;
|
|
|
- }
|
|
|
-
|
|
|
- //std::cout << " P266 Test I:" << i << "\n";
|
|
|
- resultUnit.StartIndex = packet.StartIndex;
|
|
|
- //最后来赋值
|
|
|
- //resultUnit.Data = &packet.Data;
|
|
|
- WriteLog(LogLevel::LevelDebug, " Tst Data Ptr:0x%x , Data:%d", static_cast<void*>(resultUnit.Data),
|
|
|
- packet.Data);
|
|
|
- resultUnit.DataCount = 1;
|
|
|
- resultUnit.Length = eventStartIndex - packet.StartIndex;
|
|
|
-
|
|
|
- eventPtr->EventIndex = static_cast<int64_t>(decodeEventsPtr->size());
|
|
|
-
|
|
|
- decodeEvents.push_back(decodeEvent);
|
|
|
- decodeResultUnits.push_back(resultUnit);
|
|
|
-
|
|
|
- decodeResult.DecodeEventNeedUpdate = true;
|
|
|
- decodeResult.DecodeResultNeedUpdate = true;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /////////////////////////// 指针赋值 /////////////////////////////////
|
|
|
- int64_t eventDataIndex = 0;
|
|
|
- for (int i = 0; i < decodeEvents.size(); i++) // NOLINT(modernize-loop-convert, clang-diagnostic-sign-compare)
|
|
|
- {
|
|
|
- decodeEvents[i].EventData = decodeEventUnitsStorage.data() + eventDataIndex;
|
|
|
- eventDataIndex += decodeEvents[i].EventDataCount;
|
|
|
- }
|
|
|
-
|
|
|
- for (int i = 0; i < decodeResultUnits.size(); i++) // NOLINT(clang-diagnostic-sign-compare)
|
|
|
- {
|
|
|
- int64_t startIndex = decodeResultUnits[i].StartIndex;
|
|
|
- for (int x = 0; i < rs232Packets.size(); x++) // NOLINT(clang-diagnostic-sign-compare)
|
|
|
- {
|
|
|
- if (rs232Packets[x].StartIndex == startIndex)
|
|
|
- {
|
|
|
- decodeResultUnits[i].Data = &(rs232Packets[x].Data);
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- decodeResult.DecodeEventsPtr = decodeEventsPtr->data();
|
|
|
- decodeResult.DecodeResultUnitsPtr = decodeResultUnitsPtr->data();
|
|
|
- decodeResult.DecodeEventCount = decodeEventsPtr->size();
|
|
|
- decodeResult.DecodeResultCount = decodeResultUnitsPtr->size();
|
|
|
-
|
|
|
- WriteLog(LogLevel::Level1, " Cpp ParsingData Done,return True \n\n");
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- std::vector<const char*> getEventInfoTitles()
|
|
|
- {
|
|
|
- return eventInfoTitles;
|
|
|
- }
|
|
|
-
|
|
|
- bool getRS232Bit(TwoLevelEdgePulse*& edgePulse,
|
|
|
- int32_t targetIndex, TwoLevelEdgePulseStatusType& status)
|
|
|
- {
|
|
|
- status = TwoLevelEdgePulseStatusType::None;
|
|
|
- if (edgePulse == nullptr)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (targetIndex <= 0 || edgePulse->StartIndex > targetIndex)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- while (edgePulse != nullptr)
|
|
|
- {
|
|
|
- if (edgePulse->StartIndex <= targetIndex && edgePulse->EndIndex > targetIndex)
|
|
|
- {
|
|
|
- status = edgePulse->CurrentLevel;
|
|
|
- //test
|
|
|
- WriteLog(LogLevel::Level2, "StartIndex:%d , status:%d", edgePulse->StartIndex, status);
|
|
|
- return true;
|
|
|
- }
|
|
|
- edgePulse++;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- bool quantizeParamsDecodeRS232(const QuantizeParams& quantizeParams, const ProtocolRS232Options& options,
|
|
|
- RS232DecodeResult& decodeResult)
|
|
|
- {
|
|
|
- canceled = false;
|
|
|
- //todo
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- bool Parse_RS232(const ProtocolRS232Options& options,
|
|
|
- const EdgePulseDataTwoLevels& edgePulseData,
|
|
|
- RS232DecodeResult& decodeResult)
|
|
|
- {
|
|
|
- return pauseDataRS232(options, edgePulseData.GetDataAddrPtr(),
|
|
|
- edgePulseData.EdgePulsesCount,
|
|
|
- edgePulseData.WaveformDataCount, edgePulseData.SampleRate,
|
|
|
- decodeResult);
|
|
|
- }
|
|
|
+
|
|
|
+ std::vector<RS232DecodeResultCell> decodeResultUnits;
|
|
|
+ std::vector<RS232DecodeEventUnit> decodeEventUnitsStorage;
|
|
|
+ std::vector<RS232DecodeEvent> decodeEvents;
|
|
|
+
|
|
|
+
|
|
|
+ int32_t getStartIndex(const ProtocolRS232Options& options, TwoLevelEdgePulse*& node,
|
|
|
+ double count,
|
|
|
+ double& realCount,
|
|
|
+ const int32_t startIndex,
|
|
|
+ TwoLevelEdgePulseStatusType& startStatus)
|
|
|
+ {
|
|
|
+ startStatus = options.Polarity == Polarity::Pos
|
|
|
+ ? TwoLevelEdgePulseStatusType::High
|
|
|
+ : TwoLevelEdgePulseStatusType::Low;
|
|
|
+
|
|
|
+ while (node != nullptr)
|
|
|
+ {
|
|
|
+ if (node->CurrentLevel != startStatus && node->StartIndex >= startIndex)
|
|
|
+ {
|
|
|
+ //计算该脉宽中,最大包括几个bit信息宽度
|
|
|
+ const double bitCount = std::round(node->GetLength() / count);
|
|
|
+ if (bitCount > 0 && bitCount < static_cast<int32_t>(options.DataBitWidth) + 4)
|
|
|
+ {
|
|
|
+ realCount = node->GetLength() / bitCount;
|
|
|
+ }
|
|
|
+ if (bitCount > 0) break;
|
|
|
+ }
|
|
|
+
|
|
|
+ node++;
|
|
|
+ }
|
|
|
+ return node == nullptr ? -1 : node->StartIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool pauseDataRS232(const ProtocolRS232Options& options, TwoLevelEdgePulse* edge_pulses,
|
|
|
+ const uint64_t edge_pulse_count,
|
|
|
+ const uint64_t waveform_data_count, const double sample_rate,
|
|
|
+ RS232DecodeResult& decode_result)
|
|
|
+
|
|
|
+ {
|
|
|
+ canceled = false;
|
|
|
+ WriteLog(LogLevel::LevelDebug, " SignalType:%d, DataBitWidth:%d"
|
|
|
+ , static_cast<int>(options.SignalType), static_cast<int>(options.DataBitWidth));
|
|
|
+ if (canceled)
|
|
|
+ {
|
|
|
+ WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //栈上 初始化内存
|
|
|
+ std::vector<RS232Packet> rs232_packets;
|
|
|
+ rs232_packets = {};
|
|
|
+ decodeEvents = {};
|
|
|
+ decodeResultUnits = {};
|
|
|
+
|
|
|
+ auto decodeResultUnitsPtr = &decodeResultUnits;
|
|
|
+ auto decodeEventsPtr = &decodeEvents;
|
|
|
+ WriteLog(LogLevel::LevelDebug, " decodeResultUnitsPtr:0x%X\n", static_cast<void*>(decodeResultUnitsPtr));
|
|
|
+ WriteLog(LogLevel::LevelDebug, " decodeEventsPtr:0x%X\n", static_cast<void*>(decodeEventsPtr));
|
|
|
+ canceled = false;
|
|
|
+
|
|
|
+ if (canceled)
|
|
|
+ {
|
|
|
+ WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (edge_pulses == nullptr)
|
|
|
+ {
|
|
|
+ WriteLog(LogLevel::Level2, " EdgePulseDataPtr is nullptr\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //if (edgePulseData.edgePulses.empty())
|
|
|
+ if (waveform_data_count == 0)
|
|
|
+ {
|
|
|
+ std::cout << " DataCount is 0 or sampleRate error\n";
|
|
|
+ WriteLog(LogLevel::Level2, " DataCount is 0 or sampleRate error\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ int32_t dataBitCount = 0;
|
|
|
+ switch (options.DataBitWidth)
|
|
|
+ {
|
|
|
+ case RS232Enums::DataBitWidth::DataBitWidth_5Bit:
|
|
|
+ dataBitCount = 5;
|
|
|
+ break;
|
|
|
+ case RS232Enums::DataBitWidth::DataBitWidth_6Bit:
|
|
|
+ dataBitCount = 6;
|
|
|
+ break;
|
|
|
+ case RS232Enums::DataBitWidth::DataBitWidth_7Bit:
|
|
|
+ dataBitCount = 7;
|
|
|
+ break;
|
|
|
+ case RS232Enums::DataBitWidth::DataBitWidth_8Bit:
|
|
|
+ dataBitCount = 8;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ uint64_t edgePulseIndex = 0;
|
|
|
+
|
|
|
+ //每bit理论长度
|
|
|
+ const double count = 1.0 / options.BaudRate * sample_rate;
|
|
|
+ WriteLog(LogLevel::LevelDebug, " bit count:%f", count);
|
|
|
+ //const double count = static_cast<double>(dataCount);
|
|
|
+ //每bit实际长度
|
|
|
+ double realCount = count;
|
|
|
+
|
|
|
+ const int32_t stopBitCount = options.StopBit == RS232Enums::StopBit::StopBit_1bit
|
|
|
+ ? 1
|
|
|
+ : options.StopBit == RS232Enums::StopBit::StopBit_2bit
|
|
|
+ ? 2
|
|
|
+ : 1;
|
|
|
+
|
|
|
+ WriteLog(LogLevel::LevelDebug, " realCount:%f", realCount);
|
|
|
+ WriteLog(LogLevel::LevelDebug, " edgePulsePointer:0x%x", reinterpret_cast<void*>(edge_pulses));
|
|
|
+ //WriteLog(" edgePulsePointer:0x%x", edgePulses);
|
|
|
+ WriteLog(LogLevel::LevelDebug, " edgePulse StartIndex:%d", edge_pulses->StartIndex);
|
|
|
+ WriteLog(LogLevel::LevelDebug, " edgePulse EndIndex:%d", edge_pulses->EndIndex);
|
|
|
+ WriteLog(LogLevel::LevelDebug, " edgePulseCount:%d", edge_pulse_count);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ int32_t startIndex = 0;
|
|
|
+
|
|
|
+ while (edge_pulses != nullptr && edgePulseIndex < edge_pulse_count)
|
|
|
+ {
|
|
|
+ if (count <= 2) break;
|
|
|
+ TwoLevelEdgePulseStatusType levelState;
|
|
|
+ //找到帧头 根据帧头得到实际bit宽度realCount 帧头起始位置startIndex 电平状态levelState
|
|
|
+ int32_t packetStartIndex =
|
|
|
+ getStartIndex(options, edge_pulses, count, realCount, startIndex, levelState);
|
|
|
+ //WriteLog(" GetStartIndex PacketStartIndex:%d\n", packetStartIndex);
|
|
|
+ if (canceled)
|
|
|
+ {
|
|
|
+ WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (packetStartIndex == -1) break;
|
|
|
+ //std::cout << " packetStartIndex:" << packetStartIndex << " ,realCount:" << realCount << "\n";
|
|
|
+ //构造帧
|
|
|
+ RS232Packet packet = {};
|
|
|
+ packetStartIndex += static_cast<int32_t>(std::round(realCount / 2));
|
|
|
+
|
|
|
+ if (edge_pulses->StartIndex >= edge_pulses->EndIndex)
|
|
|
+ {
|
|
|
+ std::ostringstream msg;
|
|
|
+ msg << " =>178 loop break Index:" << packetStartIndex << " ,dataCount:" <<
|
|
|
+ waveform_data_count << "\n";
|
|
|
+ WriteLog(LogLevel::LevelDebug, msg.str().c_str());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (packetStartIndex >= static_cast<int32_t>(waveform_data_count))
|
|
|
+ {
|
|
|
+ std::ostringstream msg;
|
|
|
+ msg << " =>186 loop break Index:" << packetStartIndex << " ,dataCount:" <<
|
|
|
+ waveform_data_count << "\n";
|
|
|
+ WriteLog(LogLevel::LevelDebug, msg.str().c_str());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (startIndex == static_cast<int32_t>(waveform_data_count)) break;
|
|
|
+
|
|
|
+ packet.StartBit = levelState == TwoLevelEdgePulseStatusType::High;
|
|
|
+ packet.StartIndex = packetStartIndex - static_cast<int32_t>(std::round(realCount / 2));
|
|
|
+ packet.DataIndex = packetStartIndex + static_cast<int32_t>(std::round(realCount / 2));
|
|
|
+ packet.PerBitLenght = realCount;
|
|
|
+
|
|
|
+ WriteLog(LogLevel::LevelDebug, "=> Tst StartIndex:%d,dataBitCount:%d", packet.StartIndex, dataBitCount);
|
|
|
+ for (int32_t dataBitIndex = 0; dataBitIndex < dataBitCount; dataBitIndex++)
|
|
|
+ {
|
|
|
+ if (canceled)
|
|
|
+ {
|
|
|
+ WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ packetStartIndex += static_cast<int32_t>(std::round(realCount));
|
|
|
+
|
|
|
+ //std::cout << " P150 Test" << "\n";
|
|
|
+ if (packetStartIndex >= static_cast<int32_t>(waveform_data_count))
|
|
|
+ {
|
|
|
+ startIndex = static_cast<int32_t>(waveform_data_count);
|
|
|
+ WriteLog(LogLevel::LevelDebug, " =>%d break \n", __LINE__);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ //std::cout << " P157 Test" << "\n";
|
|
|
+ TwoLevelEdgePulseStatusType bitStatus;
|
|
|
+ if (!getRS232Bit(edge_pulses, packetStartIndex, bitStatus))
|
|
|
+ {
|
|
|
+ //gMutex.unlock(); // 执行完操作后手动释放锁
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //按位构造数据
|
|
|
+ if (options.MSBOrLSB == MSBOrLSB::MSB)
|
|
|
+ {
|
|
|
+ packet.Data = static_cast<uint8_t>(packet.Data << 1);
|
|
|
+ packet.Data |= bitStatus == TwoLevelEdgePulseStatusType::High ? 1 : 0;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ packet.Data |= static_cast<uint8_t>((bitStatus ==
|
|
|
+ TwoLevelEdgePulseStatusType::High
|
|
|
+ ? 1
|
|
|
+ : 0) << dataBitIndex);
|
|
|
+ }
|
|
|
+ WriteLog(LogLevel::LevelDebug, "Tst CurrentLevel:%d, bitStatus:%d", edge_pulses->CurrentLevel, bitStatus);
|
|
|
+ }
|
|
|
+ //std::cout << " P177 Test" << "\n";
|
|
|
+ if (startIndex == static_cast<int32_t>(waveform_data_count)) break;
|
|
|
+ //校验位
|
|
|
+ if (options.OddEvenCheckType != OddEvenCheck::None)
|
|
|
+ {
|
|
|
+ packetStartIndex += static_cast<int32_t>(std::round(realCount));
|
|
|
+
|
|
|
+ if (packetStartIndex >= static_cast<int32_t>(waveform_data_count))
|
|
|
+ {
|
|
|
+ //startIndex = static_cast<int32_t>(bufferLength);
|
|
|
+ rs232_packets.push_back(packet);
|
|
|
+ WriteLog(LogLevel::LevelDebug, " =>%d break \n", __LINE__);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ TwoLevelEdgePulseStatusType bitStatus;
|
|
|
+ if (!getRS232Bit(edge_pulses, packetStartIndex, bitStatus))
|
|
|
+ {
|
|
|
+ //gMutex.unlock(); // 执行完操作后手动释放锁
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ packet.ParityFind = true;
|
|
|
+ packet.ParityBit = bitStatus == TwoLevelEdgePulseStatusType::High;
|
|
|
+ packet.ParityResult = DataCheckHelper::CheckDataByOddEven(
|
|
|
+ packet.Data, dataBitCount, options.OddEvenCheckType);
|
|
|
+ packet.ParityIndex = packet.DataIndex + static_cast<int32_t>(
|
|
|
+ std::round(realCount * dataBitCount));
|
|
|
+ }
|
|
|
+ //std::cout << " P203 Test" << "\n";
|
|
|
+ rs232_packets.push_back(packet);
|
|
|
+
|
|
|
+ packetStartIndex += static_cast<int32_t>(realCount * stopBitCount);
|
|
|
+ startIndex = packetStartIndex;
|
|
|
+ edgePulseIndex++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (const char* ex)
|
|
|
+ {
|
|
|
+ WriteLog(LogLevel::LevelDebug, " ParsingData L%d catch :%s", __LINE__, ex);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ std::ostringstream msg;
|
|
|
+ msg << " edgePulseIndex:" << edgePulseIndex << ",";
|
|
|
+ msg << " rs232Packets count:" << rs232_packets.size() << ",";
|
|
|
+ msg << " get events && results";
|
|
|
+ WriteLog(LogLevel::Level2, msg.str().c_str());
|
|
|
+ }
|
|
|
+ //get events && results
|
|
|
+ //std::cout << " P218 Test rs232Packets Count:" << rs232Packets.size() << "\n";
|
|
|
+ if (!rs232_packets.empty())
|
|
|
+ {
|
|
|
+ decodeEventUnitsStorage = {};
|
|
|
+ //for (int64_t i = 0; i < static_cast<int64_t>(rs232Packets.size()); i++)
|
|
|
+ for (auto packet : rs232_packets)
|
|
|
+ {
|
|
|
+ if (canceled)
|
|
|
+ {
|
|
|
+ WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
|
|
|
+ //gMutex.unlock(); // 执行完操作后手动释放锁
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ RS232DecodeResultCell resultUnit;
|
|
|
+ RS232DecodeEvent decodeEvent = {};
|
|
|
+ auto eventPtr = &decodeEvent;
|
|
|
+ RS232DecodeEventUnit eventStart;
|
|
|
+ RS232DecodeEventUnit eventData;
|
|
|
+ int64_t eventStartIndex = packet.StartIndex;
|
|
|
+ int64_t oneBitLength = static_cast<int64_t>(packet.PerBitLenght);
|
|
|
+
|
|
|
+ eventPtr->StartIndex = packet.StartIndex;
|
|
|
+
|
|
|
+ eventStart.StartIndex = packet.StartIndex;
|
|
|
+ eventStart.Length = oneBitLength;
|
|
|
+ eventStartIndex += 1;
|
|
|
+ eventStart.EventType = RS232Enums::RS232DecodeEventType::Start;
|
|
|
+
|
|
|
+ eventData.StartIndex = eventStart.StartIndex;
|
|
|
+ eventData.Length = oneBitLength * dataBitCount;
|
|
|
+ eventData.Data = packet.Data;
|
|
|
+ eventData.EventType = RS232Enums::RS232DecodeEventType::Data;
|
|
|
+ eventStartIndex += 8;
|
|
|
+
|
|
|
+
|
|
|
+ //std::cout << " P249 Test I:" << i << "\n";
|
|
|
+ if (packet.ParityFind)
|
|
|
+ {
|
|
|
+ RS232DecodeEventUnit eventUints[3];
|
|
|
+ eventUints[0] = eventStart;
|
|
|
+ eventUints[1] = eventData;
|
|
|
+ RS232DecodeEventUnit eventParity;
|
|
|
+ //std::cout << " P253 Test I:" << i << "\n";
|
|
|
+ eventParity.Data = packet.ParityResult ? 1 : 0;
|
|
|
+ eventParity.StartIndex = eventStartIndex;
|
|
|
+ eventParity.Length = oneBitLength;
|
|
|
+ eventParity.EventType = RS232Enums::RS232DecodeEventType::Parity;
|
|
|
+ eventPtr->ParityResult = packet.ParityResult ? 1 : 0;
|
|
|
+ eventUints[2] = eventParity;
|
|
|
+ //eventPtr->EventData = eventUints;
|
|
|
+ eventStartIndex += 8;
|
|
|
+ eventPtr->EventDataCount = 3;
|
|
|
+ eventPtr->EndIndex = packet.StartIndex + oneBitLength * (dataBitCount + 1);
|
|
|
+ // 将eventUints复制到decodeEventUnitsStorage中
|
|
|
+ decodeEventUnitsStorage.insert(decodeEventUnitsStorage.end(), std::begin(eventUints),
|
|
|
+ std::end(eventUints));
|
|
|
+ //最后来赋值
|
|
|
+ //decodeEvent.EventData = decodeEventUnitsStorage.data() + decodeEventUnitsStorage.size() - 3;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ RS232DecodeEventUnit eventUints[2];
|
|
|
+ eventUints[0] = eventStart;
|
|
|
+ eventUints[1] = eventData;
|
|
|
+ eventPtr->EventDataCount = 2;
|
|
|
+
|
|
|
+ //eventPtr->EventData = eventUints;
|
|
|
+ //std::cout << " P263 Test I:" << i << "\n";
|
|
|
+ eventPtr->ParityResult = 0;
|
|
|
+ eventPtr->EndIndex = packet.StartIndex + oneBitLength * (dataBitCount);
|
|
|
+ // 将eventUints复制到decodeEventUnitsStorage中
|
|
|
+ decodeEventUnitsStorage.insert(decodeEventUnitsStorage.end(), std::begin(eventUints),
|
|
|
+ std::end(eventUints));
|
|
|
+ //最后来赋值
|
|
|
+ //decodeEvent.EventData = decodeEventUnitsStorage.data() + decodeEventUnitsStorage.size() - 2;
|
|
|
+ }
|
|
|
+
|
|
|
+ //std::cout << " P266 Test I:" << i << "\n";
|
|
|
+ resultUnit.StartIndex = packet.StartIndex;
|
|
|
+ //最后来赋值
|
|
|
+ //resultUnit.Data = &packet.Data;
|
|
|
+ WriteLog(LogLevel::LevelDebug, " Tst Data Ptr:0x%x , Data:%d", static_cast<void*>(resultUnit.Data),
|
|
|
+ packet.Data);
|
|
|
+ resultUnit.DataCount = 1;
|
|
|
+ resultUnit.Length = eventStartIndex - packet.StartIndex;
|
|
|
+
|
|
|
+ eventPtr->EventIndex = static_cast<int64_t>(decodeEventsPtr->size());
|
|
|
+
|
|
|
+ decodeEvents.push_back(decodeEvent);
|
|
|
+ decodeResultUnits.push_back(resultUnit);
|
|
|
+
|
|
|
+ decode_result.DecodeEventNeedUpdate = true;
|
|
|
+ decode_result.DecodeResultNeedUpdate = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /////////////////////////// 指针赋值 /////////////////////////////////
|
|
|
+ int64_t eventDataIndex = 0;
|
|
|
+ for (int i = 0; i < decodeEvents.size(); i++) // NOLINT(modernize-loop-convert, clang-diagnostic-sign-compare)
|
|
|
+ {
|
|
|
+ decodeEvents[i].EventData = decodeEventUnitsStorage.data() + eventDataIndex;
|
|
|
+ eventDataIndex += decodeEvents[i].EventDataCount;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < decodeResultUnits.size(); i++) // NOLINT(clang-diagnostic-sign-compare)
|
|
|
+ {
|
|
|
+ int64_t startIndex = decodeResultUnits[i].StartIndex;
|
|
|
+ for (int x = 0; i < rs232_packets.size(); x++) // NOLINT(clang-diagnostic-sign-compare)
|
|
|
+ {
|
|
|
+ if (rs232_packets[x].StartIndex == startIndex)
|
|
|
+ {
|
|
|
+ decodeResultUnits[i].Data = &(rs232_packets[x].Data);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ decode_result.DecodeEventsPtr = decodeEventsPtr->data();
|
|
|
+ decode_result.DecodeResultCellsPtr = decodeResultUnitsPtr->data();
|
|
|
+ decode_result.DecodeEventCount = decodeEventsPtr->size();
|
|
|
+ decode_result.DecodeResultCount = decodeResultUnitsPtr->size();
|
|
|
+
|
|
|
+ WriteLog(LogLevel::Level1, " Cpp ParsingData Done,return True \n\n");
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ std::vector<const char*> getEventInfoTitles()
|
|
|
+ {
|
|
|
+ return eventInfoTitles;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool getRS232Bit(TwoLevelEdgePulse*& edge_pulse,
|
|
|
+ int32_t targetIndex, TwoLevelEdgePulseStatusType& status)
|
|
|
+ {
|
|
|
+ status = TwoLevelEdgePulseStatusType::None;
|
|
|
+ if (edge_pulse == nullptr)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (targetIndex <= 0 || edge_pulse->StartIndex > targetIndex)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ while (edge_pulse != nullptr)
|
|
|
+ {
|
|
|
+ if (edge_pulse->StartIndex <= targetIndex && edge_pulse->EndIndex > targetIndex)
|
|
|
+ {
|
|
|
+ status = edge_pulse->CurrentLevel;
|
|
|
+
|
|
|
+ WriteLog(LogLevel::LevelDebug, "StartIndex:%d , status:%d", edge_pulse->StartIndex, status);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ edge_pulse++;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool quantizeParamsDecodeRS232(const QuantizeParams& quantizeParams, const ProtocolRS232Options& options,
|
|
|
+ RS232DecodeResult& decodeResult)
|
|
|
+ {
|
|
|
+ canceled = false;
|
|
|
+ //todo
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool Parse_RS232(const ProtocolRS232Options& options,
|
|
|
+ const EdgePulseDataTwoLevels& edgePulseData,
|
|
|
+ RS232DecodeResult& decodeResult)
|
|
|
+ {
|
|
|
+ return pauseDataRS232(options, edgePulseData.GetDataAddrPtr(),
|
|
|
+ edgePulseData.EdgePulsesCount,
|
|
|
+ edgePulseData.WaveformDataCount, edgePulseData.SampleRate,
|
|
|
+ decodeResult);
|
|
|
+ }
|
|
|
}
|