RS232Decode.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // // ******************************************************************
  2. // // /\ /| @File RS232Decode.cpp
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-5-7
  6. // // / \\ @Modified 2024-5-13
  7. // // *(__\_\
  8. // // ******************************************************************
  9. #include "RS232Decode.h"
  10. #include "../BaseEnums/DataCheckEnums.h"
  11. #include "../BaseHelper/DataCheckHelper.h"
  12. #include "../BaseHelper/Logger.h"
  13. namespace Protocol::ProtocolRS232
  14. {
  15. int32_t RS232Decode::GetStartIndex(const RS232DecodeParams& options, TwoLevelEdgePulse*& node,
  16. double count,
  17. double& realCount,
  18. const int32_t startIndex,
  19. TwoLevelEdgePulseStatusType& startStatus)
  20. {
  21. startStatus = options.Polarity == Polarity::Pos
  22. ? TwoLevelEdgePulseStatusType::High
  23. : TwoLevelEdgePulseStatusType::Low;
  24. while (node != nullptr)
  25. {
  26. if (node->CurrentLevel != startStatus && node->StartIndex >= startIndex)
  27. {
  28. //计算该脉宽中,最大包括几个bit信息宽度
  29. const double bitCount = std::round(node->GetLength() / count);
  30. if (bitCount > 0 && bitCount < static_cast<int32_t>(options.DataBitWidth) + 4)
  31. {
  32. realCount = node->GetLength() / bitCount;
  33. }
  34. if (bitCount > 0) break;
  35. }
  36. node++;
  37. }
  38. return node == nullptr ? -1 : node->StartIndex;
  39. }
  40. bool RS232Decode::ParseData(const RS232DecodeParams& options)
  41. {
  42. WriteLog(LogLevel::LevelDebug," SignalType:%d, DataBitWidth:%d"
  43. , static_cast<int>(options.SignalType), static_cast<int>(options.DataBitWidth));
  44. if (canceled_)
  45. {
  46. WriteLog(LogLevel::Level2," ParsingData canceled.\n");
  47. //gMutex.unlock(); // 执行完操作后手动释放锁
  48. return false;
  49. }
  50. //栈上 初始化内存
  51. RS232Packets_ = {};
  52. DecodeEvents_ = {};
  53. DecodeResultUnits_ = {};
  54. DecodeResultBase_ = {};
  55. RS232DecodeResult_ = {};
  56. RS232DecodeResult* rs232DecodeResultPtr = &RS232DecodeResult_;
  57. DecodeResultBase_.ProtocolType = SerialProtocolType::RS232;
  58. DecodeResultBase_.ResultDataPtr = rs232DecodeResultPtr;
  59. DecodeResultBase_.ResultValid = false;
  60. auto decodeResultUnitsPtr = &DecodeResultUnits_;
  61. auto decodeEventsPtr = &DecodeEvents_;
  62. WriteLog(LogLevel::LevelDebug," decodeResultUnitsPtr:0x%X\n", static_cast<void*>(decodeResultUnitsPtr));
  63. WriteLog(LogLevel::LevelDebug," decodeEventsPtr:0x%X\n", static_cast<void*>(decodeEventsPtr));
  64. canceled_ = false;
  65. if (canceled_)
  66. {
  67. WriteLog(LogLevel::Level2," ParsingData canceled.\n");
  68. return false;
  69. }
  70. if (options.EdgePulseData == nullptr)
  71. {
  72. WriteLog(LogLevel::Level2," EdgePulseDataPtr is nullptr\n");
  73. return false;
  74. }
  75. //if (edgePulseData.edgePulses.empty())
  76. if (options.EdgePulseData->WaveformDataCount == 0)
  77. {
  78. std::cout << " DataCount is 0 or sampleRate error\n";
  79. WriteLog(LogLevel::Level2," DataCount is 0 or sampleRate error\n");
  80. return false;
  81. }
  82. TwoLevelEdgePulse* edgePulses = options.EdgePulseData->GetDataAddrPtr();
  83. int32_t dataBitCount = 0;
  84. switch (options.DataBitWidth)
  85. {
  86. case Enums::DataBitWidth::DataBitWidth_5Bit:
  87. dataBitCount = 5;
  88. break;
  89. case Enums::DataBitWidth::DataBitWidth_6Bit:
  90. dataBitCount = 6;
  91. break;
  92. case Enums::DataBitWidth::DataBitWidth_7Bit:
  93. dataBitCount = 7;
  94. break;
  95. case Enums::DataBitWidth::DataBitWidth_8Bit:
  96. dataBitCount = 8;
  97. break;
  98. }
  99. uint64_t edgePulseIndex = 0;
  100. //每bit理论长度
  101. const double count = 1.0 / options.BaudRate * options.EdgePulseData->SampleRate;
  102. WriteLog(LogLevel::LevelDebug," bit count:%f", count);
  103. //const double count = static_cast<double>(dataCount);
  104. //每bit实际长度
  105. double realCount = count;
  106. const int32_t stopBitCount = options.StopBit == Enums::StopBit::StopBit_1bit
  107. ? 1
  108. : options.StopBit == Enums::StopBit::StopBit_2bit
  109. ? 2
  110. : 1;
  111. WriteLog(LogLevel::LevelDebug," realCount:%f", realCount);
  112. WriteLog(LogLevel::LevelDebug," edgePulsePointer:0x%x", reinterpret_cast<void*>(edgePulses));
  113. //WriteLog(" edgePulsePointer:0x%x", edgePulses);
  114. WriteLog(LogLevel::LevelDebug," edgePulse StartIndex:%d", edgePulses->StartIndex);
  115. WriteLog(LogLevel::LevelDebug," edgePulse EndIndex:%d", edgePulses->EndIndex);
  116. WriteLog(LogLevel::LevelDebug," edgePulseCount:%d", options.EdgePulseData->EdgePulsesCount);
  117. try
  118. {
  119. int32_t startIndex = 0;
  120. while (edgePulses != nullptr && edgePulseIndex < options.EdgePulseData->EdgePulsesCount)
  121. {
  122. if (count <= 2) break;
  123. TwoLevelEdgePulseStatusType levelState;
  124. //找到帧头 根据帧头得到实际bit宽度realCount 帧头起始位置startIndex 电平状态levelState
  125. int32_t packetStartIndex =
  126. GetStartIndex(options, edgePulses, count, realCount, startIndex, levelState);
  127. //WriteLog(" GetStartIndex PacketStartIndex:%d\n", packetStartIndex);
  128. if (canceled_)
  129. {
  130. WriteLog(LogLevel::Level2," ParsingData canceled.\n");
  131. return false;
  132. }
  133. if (packetStartIndex == -1) break;
  134. //std::cout << " packetStartIndex:" << packetStartIndex << " ,realCount:" << realCount << "\n";
  135. //构造帧
  136. RS232Packet packet = {};
  137. packetStartIndex += static_cast<int32_t>(std::round(realCount / 2));
  138. if (edgePulses->StartIndex >= edgePulses->EndIndex || !CommonHelper::EnumIsDefined<
  139. TwoLevelEdgePulseStatusType>(static_cast<int>(edgePulses->CurrentLevel)))
  140. {
  141. std::ostringstream msg;
  142. msg << " =>178 loop break Index:" << packetStartIndex << " ,dataCount:" << options.
  143. EdgePulseData->WaveformDataCount << "\n";
  144. WriteLog(LogLevel::LevelDebug,msg.str().c_str());
  145. break;
  146. }
  147. if (packetStartIndex >= static_cast<int32_t>(options.EdgePulseData->WaveformDataCount))
  148. {
  149. std::ostringstream msg;
  150. msg << " =>186 loop break Index:" << packetStartIndex << " ,dataCount:" << options.
  151. EdgePulseData->WaveformDataCount << "\n";
  152. WriteLog(LogLevel::LevelDebug,msg.str().c_str());
  153. break;
  154. }
  155. if (startIndex == static_cast<int32_t>(options.EdgePulseData->WaveformDataCount)) break;
  156. packet.StartBit = levelState == TwoLevelEdgePulseStatusType::High;
  157. packet.StartIndex = packetStartIndex - static_cast<int32_t>(std::round(realCount / 2));
  158. packet.DataIndex = packetStartIndex + static_cast<int32_t>(std::round(realCount / 2));
  159. packet.PerBitLenght = realCount;
  160. WriteLog(LogLevel::LevelDebug,"=> Tst StartIndex:%d,dataBitCount:%d", packet.StartIndex, dataBitCount);
  161. for (int32_t dataBitIndex = 0; dataBitIndex < dataBitCount; dataBitIndex++)
  162. {
  163. if (canceled_)
  164. {
  165. WriteLog(LogLevel::Level2," ParsingData canceled.\n");
  166. return false;
  167. }
  168. packetStartIndex += static_cast<int32_t>(std::round(realCount));
  169. //std::cout << " P150 Test" << "\n";
  170. if (packetStartIndex >= static_cast<int32_t>(options.EdgePulseData->WaveformDataCount))
  171. {
  172. startIndex = static_cast<int32_t>(options.EdgePulseData->WaveformDataCount);
  173. WriteLog(LogLevel::LevelDebug," =>%d break \n", __LINE__);
  174. break;
  175. }
  176. //std::cout << " P157 Test" << "\n";
  177. TwoLevelEdgePulseStatusType bitStatus;
  178. if (!GetRS232Bit(edgePulses, packetStartIndex, bitStatus))
  179. {
  180. //gMutex.unlock(); // 执行完操作后手动释放锁
  181. return false;
  182. }
  183. //按位构造数据
  184. if (options.MSBOrLSB == MSBOrLSB::MSB)
  185. {
  186. packet.Data = static_cast<uint8_t>(packet.Data << 1);
  187. packet.Data |= bitStatus == TwoLevelEdgePulseStatusType::High ? 1 : 0;
  188. }
  189. else
  190. {
  191. packet.Data |= static_cast<uint8_t>((bitStatus ==
  192. TwoLevelEdgePulseStatusType::High
  193. ? 1
  194. : 0) << dataBitIndex);
  195. }
  196. WriteLog(LogLevel::LevelDebug,"Tst CurrentLevel:%d, bitStatus:%d", edgePulses->CurrentLevel, bitStatus);
  197. }
  198. //std::cout << " P177 Test" << "\n";
  199. if (startIndex == static_cast<int32_t>(options.EdgePulseData->WaveformDataCount)) break;
  200. //校验位
  201. if (options.OddEvenCheckType != OddEvenCheck::None)
  202. {
  203. packetStartIndex += static_cast<int32_t>(std::round(realCount));
  204. if (packetStartIndex >= static_cast<int32_t>(options.EdgePulseData->WaveformDataCount))
  205. {
  206. //startIndex = static_cast<int32_t>(bufferLength);
  207. RS232Packets_.push_back(packet);
  208. WriteLog(LogLevel::LevelDebug," =>%d break \n", __LINE__);
  209. break;
  210. }
  211. TwoLevelEdgePulseStatusType bitStatus;
  212. if (!GetRS232Bit(edgePulses, packetStartIndex, bitStatus))
  213. {
  214. //gMutex.unlock(); // 执行完操作后手动释放锁
  215. return false;
  216. }
  217. packet.ParityFind = true;
  218. packet.ParityBit = bitStatus == TwoLevelEdgePulseStatusType::High;
  219. packet.ParityResult = DataCheckHelper::CheckDataByOddEven(
  220. packet.Data, dataBitCount, options.OddEvenCheckType);
  221. packet.ParityIndex = packet.DataIndex + static_cast<int32_t>(
  222. std::round(realCount * dataBitCount));
  223. }
  224. //std::cout << " P203 Test" << "\n";
  225. RS232Packets_.push_back(packet);
  226. packetStartIndex += static_cast<int32_t>(realCount * stopBitCount);
  227. startIndex = packetStartIndex;
  228. edgePulseIndex++;
  229. }
  230. }
  231. catch (const char* ex)
  232. {
  233. WriteLog(LogLevel::LevelDebug," ParsingData L%d catch :%s", __LINE__, ex);
  234. return false;
  235. }
  236. {
  237. std::ostringstream msg;
  238. msg << " edgePulseIndex:" << edgePulseIndex << ",";
  239. msg << " rs232Packets count:" << RS232Packets_.size() << ",";
  240. msg << " get events && results";
  241. WriteLog(LogLevel::Level2,msg.str().c_str());
  242. }
  243. //get events && results
  244. //std::cout << " P218 Test rs232Packets Count:" << rs232Packets.size() << "\n";
  245. if (!RS232Packets_.empty())
  246. {
  247. DecodeEventUnitsStorage_ = {};
  248. //for (int64_t i = 0; i < static_cast<int64_t>(rs232Packets.size()); i++)
  249. for (auto packet : RS232Packets_)
  250. {
  251. if (canceled_)
  252. {
  253. WriteLog(LogLevel::Level2," ParsingData canceled.\n");
  254. //gMutex.unlock(); // 执行完操作后手动释放锁
  255. return false;
  256. }
  257. RS232DecodeResultUnit resultUnit;
  258. RS232DecodeEvent decodeEvent = {};
  259. auto eventPtr = &decodeEvent;
  260. RS232DecodeEventUnit eventStart;
  261. RS232DecodeEventUnit eventData;
  262. int64_t eventStartIndex = packet.StartIndex;
  263. int64_t oneBitLength = static_cast<int64_t>(packet.PerBitLenght);
  264. eventPtr->StartIndex = packet.StartIndex;
  265. eventStart.StartIndex = packet.StartIndex;
  266. eventStart.Length = oneBitLength;
  267. eventStartIndex += 1;
  268. eventStart.EventType = Enums::RS232DecodeEventType::Start;
  269. eventData.StartIndex = eventStart.StartIndex;
  270. eventData.Length = oneBitLength * dataBitCount;
  271. eventData.Data = packet.Data;
  272. eventData.EventType = Enums::RS232DecodeEventType::Data;
  273. eventStartIndex += 8;
  274. //std::cout << " P249 Test I:" << i << "\n";
  275. if (packet.ParityFind)
  276. {
  277. RS232DecodeEventUnit eventUints[3];
  278. eventUints[0] = eventStart;
  279. eventUints[1] = eventData;
  280. RS232DecodeEventUnit eventParity;
  281. //std::cout << " P253 Test I:" << i << "\n";
  282. eventParity.Data = packet.ParityResult ? 1 : 0;
  283. eventParity.StartIndex = eventStartIndex;
  284. eventParity.Length = oneBitLength;
  285. eventParity.EventType = Enums::RS232DecodeEventType::Parity;
  286. eventPtr->ParityResult = packet.ParityResult ? 1 : 0;
  287. eventUints[2] = eventParity;
  288. //eventPtr->EventData = eventUints;
  289. eventStartIndex += 8;
  290. eventPtr->EventDataCount = 3;
  291. eventPtr->EndIndex = packet.StartIndex + oneBitLength * (dataBitCount + 1);
  292. // 将eventUints复制到decodeEventUnitsStorage中
  293. DecodeEventUnitsStorage_.insert(DecodeEventUnitsStorage_.end(), std::begin(eventUints),
  294. std::end(eventUints));
  295. //最后来赋值
  296. //decodeEvent.EventData = decodeEventUnitsStorage.data() + decodeEventUnitsStorage.size() - 3;
  297. }
  298. else
  299. {
  300. RS232DecodeEventUnit eventUints[2];
  301. eventUints[0] = eventStart;
  302. eventUints[1] = eventData;
  303. eventPtr->EventDataCount = 2;
  304. //eventPtr->EventData = eventUints;
  305. //std::cout << " P263 Test I:" << i << "\n";
  306. eventPtr->ParityResult = 0;
  307. eventPtr->EndIndex = packet.StartIndex + oneBitLength * (dataBitCount);
  308. // 将eventUints复制到decodeEventUnitsStorage中
  309. DecodeEventUnitsStorage_.insert(DecodeEventUnitsStorage_.end(), std::begin(eventUints),
  310. std::end(eventUints));
  311. //最后来赋值
  312. //decodeEvent.EventData = decodeEventUnitsStorage.data() + decodeEventUnitsStorage.size() - 2;
  313. }
  314. //std::cout << " P266 Test I:" << i << "\n";
  315. resultUnit.StartIndex = packet.StartIndex;
  316. //最后来赋值
  317. //resultUnit.Data = &packet.Data;
  318. WriteLog(LogLevel::LevelDebug," Tst Data Ptr:0x%x , Data:%d", static_cast<void*>(resultUnit.Data), packet.Data);
  319. resultUnit.DataCount = 1;
  320. resultUnit.Length = eventStartIndex - packet.StartIndex;
  321. eventPtr->EventIndex = static_cast<int64_t>(decodeEventsPtr->size());
  322. DecodeEvents_.push_back(decodeEvent);
  323. DecodeResultUnits_.push_back(resultUnit);
  324. rs232DecodeResultPtr->DecodeEventNeedUpdate = true;
  325. rs232DecodeResultPtr->DecodeResultNeedUpdate = true;
  326. }
  327. }
  328. /////////////////////////// 指针赋值 /////////////////////////////////
  329. int64_t eventDataIndex = 0;
  330. for (int i = 0; i < DecodeEvents_.size(); i++) // NOLINT(modernize-loop-convert, clang-diagnostic-sign-compare)
  331. {
  332. DecodeEvents_[i].EventData = DecodeEventUnitsStorage_.data() + eventDataIndex;
  333. eventDataIndex += DecodeEvents_[i].EventDataCount;
  334. }
  335. for (int i = 0; i < DecodeResultUnits_.size(); i++) // NOLINT(clang-diagnostic-sign-compare)
  336. {
  337. int64_t startIndex = DecodeResultUnits_[i].StartIndex;
  338. for (int x = 0; i < RS232Packets_.size(); x++) // NOLINT(clang-diagnostic-sign-compare)
  339. {
  340. if (RS232Packets_[x].StartIndex == startIndex)
  341. {
  342. DecodeResultUnits_[i].Data = &(RS232Packets_[x].Data);
  343. break;
  344. }
  345. }
  346. }
  347. rs232DecodeResultPtr->DecodeEventsPtr = decodeEventsPtr->data();
  348. rs232DecodeResultPtr->DecodeResultUnitsPtr = decodeResultUnitsPtr->data();
  349. rs232DecodeResultPtr->DecodeEventCount = decodeEventsPtr->size();
  350. rs232DecodeResultPtr->DecodeResultCount = decodeResultUnitsPtr->size();
  351. DecodeResultBase_.ResultValid = true;
  352. DecodeResultBase_.ResultDataPtr = rs232DecodeResultPtr;
  353. WriteLog(LogLevel::Level1," Cpp ParsingData Done,return True \n\n");
  354. return true;
  355. }
  356. void RS232Decode::Decode(const DecodeParams* decodeParams)
  357. {
  358. status_ = ProtocolStatus::DecodeInProgress;
  359. WriteLog(LogLevel::Level2," RS232 Decode Start\n");
  360. WriteLog(LogLevel::Level2," Type: %d", decodeParams->ProtocolType);
  361. WriteLog(LogLevel::Level2," AutoClock: %d", decodeParams->AutomaticClockRecovery);
  362. try
  363. {
  364. if (decodeParams == nullptr || decodeParams->ProtocolParamsPtr == nullptr)
  365. {
  366. WriteLog(LogLevel::LevelDebug," DecodeParams or pointer is null\n");
  367. status_ = ProtocolStatus::DecodeFailure;
  368. return;
  369. }
  370. //WriteLog(" Decode ProtocolParamsPtr:0x%x", reinterpret_cast<uint64_t>(decodeParams->ProtocolParamsPtr));
  371. WriteLog(LogLevel::LevelDebug," Decode ProtocolParamsPtr:0x%x", decodeParams->ProtocolParamsPtr);
  372. if (static_cast<const RS232DecodeParams*>(decodeParams->ProtocolParamsPtr) == nullptr)
  373. {
  374. WriteLog(LogLevel::LevelDebug," DecodeParams is null\n");
  375. status_ = ProtocolStatus::DecodeFailure;
  376. return;
  377. }
  378. // 安全的向下转型
  379. const RS232DecodeParams options = static_cast<const RS232DecodeParams*>(decodeParams->ProtocolParamsPtr)[0];
  380. WriteLog(LogLevel::LevelDebug," Decode Get RS232DecodeParams BaudRate:%d", options.BaudRate);
  381. //test 测试成功
  382. const uint64_t edgePulsesCount = options.EdgePulseData->EdgePulsesCount;
  383. const TwoLevelEdgePulse* edgePulsesPtr = options.EdgePulseData->GetDataAddrPtr();
  384. if (edgePulsesCount > 0 && edgePulsesPtr != nullptr)
  385. {
  386. for (uint64_t i = 0; i < edgePulsesCount; i++)
  387. {
  388. const TwoLevelEdgePulse edgePulses = edgePulsesPtr[i];
  389. WriteLog(LogLevel::LevelDebug,"Index: %d, Level:%d", edgePulses.StartIndex, edgePulses.CurrentLevel);
  390. }
  391. }
  392. const bool result = ParseData(options);
  393. WriteLog(LogLevel::Level2," Decode ParseData Result:%d", result);
  394. if (!result)
  395. {
  396. status_ = ProtocolStatus::DecodeFailure;
  397. WriteLog(LogLevel::Level1," RS232 Decode Failure\n");
  398. return;
  399. }
  400. }
  401. catch (const std::runtime_error& e)
  402. {
  403. WriteLog(LogLevel::LevelDebug,"Caught exception: %s", e.what());
  404. status_ = ProtocolStatus::DecodeFailure;
  405. return;
  406. }
  407. catch (const std::exception& e)
  408. {
  409. // 捕获所有标准库异常
  410. WriteLog(LogLevel::LevelDebug,"Caught exception: %s", e.what());
  411. status_ = ProtocolStatus::DecodeFailure;
  412. return;
  413. }
  414. catch (...)
  415. {
  416. // 捕获所有非标准库异常
  417. WriteLog(LogLevel::LevelDebug,"Caught unknown exception");
  418. status_ = ProtocolStatus::DecodeFailure;
  419. return;
  420. }
  421. WriteLog(LogLevel::Level1," RS232 Decode Succeed\n");
  422. status_ = ProtocolStatus::Success;
  423. }
  424. void RS232Decode::QuantizeDecode(const QuantizeParams& decodeParams)
  425. {
  426. //todo 整形
  427. }
  428. DecodeResult* RS232Decode::GetResult()
  429. {
  430. return &DecodeResultBase_;
  431. }
  432. RS232Decode::~RS232Decode() = default;
  433. std::vector<const char*> RS232Decode::GetEventInfoTitles()
  434. {
  435. return EventInfoTitles_;
  436. }
  437. bool RS232Decode::GetRS232Bit(TwoLevelEdgePulse*& edgePulse,
  438. int32_t targetIndex, TwoLevelEdgePulseStatusType& status)
  439. {
  440. status = TwoLevelEdgePulseStatusType::None;
  441. if (edgePulse == nullptr)
  442. {
  443. return false;
  444. }
  445. if (targetIndex <= 0 || edgePulse->StartIndex > targetIndex)
  446. {
  447. return false;
  448. }
  449. while (edgePulse != nullptr)
  450. {
  451. if (edgePulse->StartIndex <= targetIndex && edgePulse->EndIndex > targetIndex)
  452. {
  453. status = edgePulse->CurrentLevel;
  454. //test
  455. WriteLog(LogLevel::Level2,"StartIndex:%d , status:%d", edgePulse->StartIndex, status);
  456. return true;
  457. }
  458. edgePulse++;
  459. }
  460. return false;
  461. }
  462. }