rs232_decoder.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // // ******************************************************************
  2. // // /\ /| @File rs232_decoder.cc
  3. // // \ V/ @Brief
  4. // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
  5. // // / | @Creation 2024-06-26
  6. // // / \\ @Modified 2024-07-16
  7. // // *(__\_\
  8. // // ******************************************************************
  9. #include "rs232_decoder.h"
  10. #include "../BaseEnums/data_check_enums.h"
  11. #include "../BaseHelper/data_check_helper.h"
  12. #include "../BaseHelper/Loger.h"
  13. namespace Protocol
  14. {
  15. int32_t Rs232Decoder::GetStartIndex(const ProtocolRs232Options& options, TwoLevelEdgePulse*& node,
  16. const double count,
  17. double& real_count,
  18. const int32_t start_index,
  19. TwoLevelEdgePulseStatusType& start_status)
  20. {
  21. start_status = options.polarity == Polarity::POS
  22. ? TwoLevelEdgePulseStatusType::High
  23. : TwoLevelEdgePulseStatusType::Low;
  24. while (node != nullptr)
  25. {
  26. if (node->current_level != start_status && node->start_index >= start_index)
  27. {
  28. //计算该脉宽中,最大包括几个bit信息宽度
  29. const double bit_count = std::round(node->GetLength() / count);
  30. if (bit_count > 0 && bit_count < static_cast<int32_t>(options.data_bit_width) + 4)
  31. {
  32. real_count = node->GetLength() / bit_count;
  33. }
  34. if (bit_count > 0) break;
  35. }
  36. node++;
  37. }
  38. return node == nullptr ? -1 : node->start_index;
  39. }
  40. bool Rs232Decoder::DecodeRs232(const ProtocolRs232Options& options, TwoLevelEdgePulse* edge_pulses,
  41. const uint64_t edge_pulse_count,
  42. const uint64_t waveform_data_count, const double sample_rate,
  43. Rs232DecodeResult& decode_result)
  44. {
  45. WriteLog(LogLevel::LevelDebug, " SignalType:%d, DataBitWidth:%d"
  46. , static_cast<int>(options.signal_type), static_cast<int>(options.data_bit_width));
  47. if (options.is_cancel)
  48. {
  49. WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
  50. return false;
  51. }
  52. //栈上 初始化内存
  53. std::vector<Rs232Packet> rs232_packets;
  54. rs232_packets = {};
  55. decode_events_ = {};
  56. decode_result_units_ = {};
  57. auto decode_result_units_ptr = &decode_result_units_;
  58. auto decode_events_ptr = &decode_events_;
  59. WriteLog(LogLevel::LevelDebug, " decodeResultUnitsPtr:0x%X\n", static_cast<void*>(decode_result_units_ptr));
  60. WriteLog(LogLevel::LevelDebug, " decodeEvents_Ptr:0x%X\n", static_cast<void*>(decode_events_ptr));
  61. if (options.is_cancel)
  62. {
  63. WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
  64. return false;
  65. }
  66. if (edge_pulses == nullptr)
  67. {
  68. WriteLog(LogLevel::Level2, " EdgePulseDataPtr is nullptr\n");
  69. return false;
  70. }
  71. //if (edgePulseData.edgePulses.empty())
  72. if (waveform_data_count == 0)
  73. {
  74. std::cout << " DataCount is 0 or sampleRate error\n";
  75. WriteLog(LogLevel::Level2, " DataCount is 0 or sampleRate error\n");
  76. return false;
  77. }
  78. int32_t data_bit_count = 0;
  79. switch (options.data_bit_width)
  80. {
  81. case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_5_BIT:
  82. data_bit_count = 5;
  83. break;
  84. case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_6_BIT:
  85. data_bit_count = 6;
  86. break;
  87. case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_7_BIT:
  88. data_bit_count = 7;
  89. break;
  90. case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_8_BIT:
  91. data_bit_count = 8;
  92. break;
  93. }
  94. uint64_t edge_pulse_index = 0;
  95. //每bit理论长度
  96. const double count = 1.0 / options.baud_rate * sample_rate;
  97. WriteLog(LogLevel::LevelDebug, " bit count:%f", count);
  98. //const double count = static_cast<double>(dataCount);
  99. //每bit实际长度
  100. double real_count = count;
  101. const int32_t stop_bit_count = options.stop_bit == Rs232Enums::StopBit::STOP_BIT_1_BIT
  102. ? 1
  103. : options.stop_bit == Rs232Enums::StopBit::STOP_BIT_2_BIT
  104. ? 2
  105. : 1;
  106. WriteLog(LogLevel::LevelDebug, " realCount:%f", real_count);
  107. WriteLog(LogLevel::LevelDebug, " edgePulsePointer:0x%x", reinterpret_cast<void*>(edge_pulses));
  108. //WriteLog(" edgePulsePointer:0x%x", edgePulses);
  109. WriteLog(LogLevel::LevelDebug, " edgePulse StartIndex:%d", edge_pulses->start_index);
  110. WriteLog(LogLevel::LevelDebug, " edgePulse EndIndex:%d", edge_pulses->end_index);
  111. WriteLog(LogLevel::LevelDebug, " edgePulseCount:%d", edge_pulse_count);
  112. try
  113. {
  114. int32_t start_index = 0;
  115. while (edge_pulses != nullptr && edge_pulse_index < edge_pulse_count)
  116. {
  117. if (count <= 2) break;
  118. TwoLevelEdgePulseStatusType level_state;
  119. //找到帧头 根据帧头得到实际bit宽度realCount 帧头起始位置startIndex 电平状态levelState
  120. int32_t packet_start_index =
  121. GetStartIndex(options, edge_pulses, count, real_count, start_index, level_state);
  122. //WriteLog(" GetStartIndex PacketStartIndex:%d\n", packetStartIndex);
  123. if (options.is_cancel)
  124. {
  125. WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
  126. return false;
  127. }
  128. if (packet_start_index == -1) break;
  129. //std::cout << " packetStartIndex:" << packetStartIndex << " ,realCount:" << realCount << "\n";
  130. //构造帧
  131. Rs232Packet packet = {};
  132. packet_start_index += static_cast<int32_t>(std::round(real_count / 2));
  133. if (edge_pulses->start_index >= edge_pulses->end_index)
  134. {
  135. std::ostringstream msg;
  136. msg << " =>178 loop break Index:" << packet_start_index << " ,dataCount:" <<
  137. waveform_data_count << "\n";
  138. WriteLog(LogLevel::LevelDebug, msg.str().c_str());
  139. break;
  140. }
  141. if (packet_start_index >= static_cast<int32_t>(waveform_data_count))
  142. {
  143. std::ostringstream msg;
  144. msg << " =>186 loop break Index:" << packet_start_index << " ,dataCount:" <<
  145. waveform_data_count << "\n";
  146. WriteLog(LogLevel::LevelDebug, msg.str().c_str());
  147. break;
  148. }
  149. if (start_index == static_cast<int32_t>(waveform_data_count)) break;
  150. packet.start_bit = level_state == TwoLevelEdgePulseStatusType::High;
  151. packet.start_index = packet_start_index - static_cast<int32_t>(std::round(real_count / 2));
  152. packet.data_index = packet_start_index + static_cast<int32_t>(std::round(real_count / 2));
  153. packet.per_bit_length = real_count;
  154. WriteLog(LogLevel::LevelDebug, "=> Tst StartIndex:%d,dataBitCount:%d", packet.start_index, data_bit_count);
  155. for (int32_t data_bit_index = 0; data_bit_index < data_bit_count; data_bit_index++)
  156. {
  157. if (options.is_cancel)
  158. {
  159. WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
  160. return false;
  161. }
  162. packet_start_index += static_cast<int32_t>(std::round(real_count));
  163. //std::cout << " P150 Test" << "\n";
  164. if (packet_start_index >= static_cast<int32_t>(waveform_data_count))
  165. {
  166. start_index = static_cast<int32_t>(waveform_data_count);
  167. WriteLog(LogLevel::LevelDebug, " =>%d break \n", __LINE__);
  168. break;
  169. }
  170. //std::cout << " P157 Test" << "\n";
  171. TwoLevelEdgePulseStatusType bit_status;
  172. if (!GetRs232Bit(edge_pulses, packet_start_index, bit_status))
  173. {
  174. //gMutex.unlock(); // 执行完操作后手动释放锁
  175. return false;
  176. }
  177. //按位构造数据
  178. if (options.msb_or_lsb == MSBOrLSB::MSB)
  179. {
  180. packet.data = static_cast<uint8_t>(packet.data << 1);
  181. packet.data |= bit_status == TwoLevelEdgePulseStatusType::High ? 1 : 0;
  182. }
  183. else
  184. {
  185. packet.data |= static_cast<uint8_t>((bit_status ==
  186. TwoLevelEdgePulseStatusType::High
  187. ? 1
  188. : 0) << data_bit_index);
  189. }
  190. WriteLog(LogLevel::LevelDebug, "Tst CurrentLevel:%d, bitStatus:%d", edge_pulses->current_level,
  191. bit_status);
  192. }
  193. //std::cout << " P177 Test" << "\n";
  194. if (start_index == static_cast<int32_t>(waveform_data_count)) break;
  195. //校验位
  196. if (options.odd_even_check_type != OddEvenCheck::None)
  197. {
  198. packet_start_index += static_cast<int32_t>(std::round(real_count));
  199. if (packet_start_index >= static_cast<int32_t>(waveform_data_count))
  200. {
  201. //startIndex = static_cast<int32_t>(bufferLength);
  202. rs232_packets.push_back(packet);
  203. WriteLog(LogLevel::LevelDebug, " =>%d break \n", __LINE__);
  204. break;
  205. }
  206. TwoLevelEdgePulseStatusType bit_status;
  207. if (!GetRs232Bit(edge_pulses, packet_start_index, bit_status))
  208. {
  209. //gMutex.unlock(); // 执行完操作后手动释放锁
  210. return false;
  211. }
  212. packet.parity_find = true;
  213. packet.parity_bit = bit_status == TwoLevelEdgePulseStatusType::High;
  214. packet.parity_result = DataCheckHelper::CheckDataByOddEven(
  215. packet.data, data_bit_count, options.odd_even_check_type);
  216. packet.parity_index = packet.data_index + static_cast<int32_t>(
  217. std::round(real_count * data_bit_count));
  218. }
  219. //std::cout << " P203 Test" << "\n";
  220. rs232_packets.push_back(packet);
  221. packet_start_index += static_cast<int32_t>(real_count * stop_bit_count);
  222. start_index = packet_start_index;
  223. edge_pulse_index++;
  224. }
  225. }
  226. catch (const char* ex)
  227. {
  228. WriteLog(LogLevel::LevelDebug, " ParsingData L%d catch :%s", __LINE__, ex);
  229. return false;
  230. }
  231. {
  232. std::ostringstream msg;
  233. msg << " edgePulseIndex:" << edge_pulse_index << ",";
  234. msg << " rs232Packets count:" << rs232_packets.size() << ",";
  235. msg << " get events && results";
  236. WriteLog(LogLevel::Level2, msg.str().c_str());
  237. }
  238. //get events && results
  239. //std::cout << " P218 Test rs232Packets Count:" << rs232Packets.size() << "\n";
  240. if (!rs232_packets.empty())
  241. {
  242. decode_event_units_storage_ = {};
  243. //for (int64_t i = 0; i < static_cast<int64_t>(rs232Packets.size()); i++)
  244. for (auto packet : rs232_packets)
  245. {
  246. if (options.is_cancel)
  247. {
  248. WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
  249. //gMutex.unlock(); // 执行完操作后手动释放锁
  250. return false;
  251. }
  252. Rs232DecodeResultCell result_unit;
  253. Rs232DecodeEvent decode_event = {};
  254. Rs232DecodeEvent* event_ptr;
  255. event_ptr = &decode_event;
  256. Rs232DecodeEventUnit event_start;
  257. Rs232DecodeEventUnit event_data;
  258. int64_t event_start_index = packet.start_index;
  259. int64_t one_bit_length = static_cast<int64_t>(packet.per_bit_length);
  260. event_ptr->start_index = packet.start_index;
  261. event_start.start_index = packet.start_index;
  262. event_start.length = one_bit_length;
  263. event_start_index += 1;
  264. event_start.event_type = Rs232Enums::Rs232DecodeEventType::START;
  265. event_data.start_index = event_start.start_index;
  266. event_data.length = one_bit_length * data_bit_count;
  267. event_data.data = packet.data;
  268. event_data.event_type = Rs232Enums::Rs232DecodeEventType::DATA;
  269. event_start_index += 8;
  270. //std::cout << " P249 Test I:" << i << "\n";
  271. if (packet.parity_find)
  272. {
  273. Rs232DecodeEventUnit event_uints[3];
  274. event_uints[0] = event_start;
  275. event_uints[1] = event_data;
  276. Rs232DecodeEventUnit event_parity;
  277. //std::cout << " P253 Test I:" << i << "\n";
  278. event_parity.data = packet.parity_result ? 1 : 0;
  279. event_parity.start_index = event_start_index;
  280. event_parity.length = one_bit_length;
  281. event_parity.event_type = Rs232Enums::Rs232DecodeEventType::PARITY;
  282. event_ptr->parity_result = packet.parity_result ? 1 : 0;
  283. event_uints[2] = event_parity;
  284. //eventPtr->EventData = eventUints;
  285. event_start_index += 8;
  286. event_ptr->event_data_count = 3;
  287. event_ptr->end_index = packet.start_index + one_bit_length * (data_bit_count + 1);
  288. // 将eventUints复制到decodeEventUnitsStorage中
  289. decode_event_units_storage_.insert(decode_event_units_storage_.end(), std::begin(event_uints),
  290. std::end(event_uints));
  291. //最后来赋值
  292. //decodeEvent.EventData = decodeEventUnitsStorage_.data() + decodeEventUnitsStorage_.size() - 3;
  293. }
  294. else
  295. {
  296. Rs232DecodeEventUnit event_uints[2];
  297. event_uints[0] = event_start;
  298. event_uints[1] = event_data;
  299. event_ptr->event_data_count = 2;
  300. //eventPtr->EventData = eventUints;
  301. //std::cout << " P263 Test I:" << i << "\n";
  302. event_ptr->parity_result = 0;
  303. event_ptr->end_index = packet.start_index + one_bit_length * (data_bit_count);
  304. // 将eventUints复制到decodeEventUnitsStorage中
  305. decode_event_units_storage_.insert(decode_event_units_storage_.end(), std::begin(event_uints),
  306. std::end(event_uints));
  307. //最后来赋值
  308. //decodeEvent.EventData = decodeEventUnitsStorage_.data() + decodeEventUnitsStorage_.size() - 2;
  309. }
  310. //std::cout << " P266 Test I:" << i << "\n";
  311. result_unit.start_index = packet.start_index;
  312. //最后来赋值
  313. //resultUnit.data = &packet.data;
  314. WriteLog(LogLevel::LevelDebug, " Tst Data Ptr:0x%x , Data:%d", static_cast<void*>(result_unit.data),
  315. packet.data);
  316. result_unit.data_count = 1;
  317. result_unit.length = event_start_index - packet.start_index;
  318. event_ptr->event_index = static_cast<int64_t>(decode_events_ptr->size());
  319. decode_events_.push_back(decode_event);
  320. decode_result_units_.push_back(result_unit);
  321. decode_result.decode_event_need_update = true;
  322. decode_result.decode_result_need_update = true;
  323. }
  324. }
  325. /////////////////////////// 指针赋值 /////////////////////////////////
  326. int64_t event_data_index = 0;
  327. for (int i = 0; i < decode_events_.size(); i++) // NOLINT(modernize-loop-convert, clang-diagnostic-sign-compare)
  328. {
  329. decode_events_[i].event_data = decode_event_units_storage_.data() + event_data_index;
  330. event_data_index += decode_events_[i].event_data_count;
  331. }
  332. for (int i = 0; i < decode_result_units_.size(); i++) // NOLINT(clang-diagnostic-sign-compare)
  333. {
  334. int64_t start_index = decode_result_units_[i].start_index;
  335. for (int x = 0; i < rs232_packets.size(); x++) // NOLINT(clang-diagnostic-sign-compare)
  336. {
  337. if (rs232_packets[x].start_index == start_index)
  338. {
  339. decode_result_units_[i].data = &(rs232_packets[x].data);
  340. break;
  341. }
  342. }
  343. }
  344. decode_result.decode_events_ptr = decode_events_ptr->data();
  345. decode_result.decode_result_cells_ptr = decode_result_units_ptr->data();
  346. decode_result.decode_event_count = decode_events_ptr->size();
  347. decode_result.decode_result_count = decode_result_units_ptr->size();
  348. WriteLog(LogLevel::Level1, " Cpp ParsingData Done,return True \n\n");
  349. return true;
  350. }
  351. std::vector<const char*> Rs232Decoder::GetEventInfoTitles()
  352. {
  353. return event_info_titles_;
  354. }
  355. bool Rs232Decoder::GetRs232Bit(TwoLevelEdgePulse*& edge_pulse,
  356. const int32_t target_index, TwoLevelEdgePulseStatusType& status)
  357. {
  358. status = TwoLevelEdgePulseStatusType::None;
  359. if (edge_pulse == nullptr)
  360. {
  361. return false;
  362. }
  363. if (target_index <= 0 || edge_pulse->start_index > target_index)
  364. {
  365. return false;
  366. }
  367. while (edge_pulse != nullptr)
  368. {
  369. if (edge_pulse->start_index <= target_index && edge_pulse->end_index > target_index)
  370. {
  371. status = edge_pulse->current_level;
  372. WriteLog(LogLevel::LevelDebug, "StartIndex:%d , status:%d", edge_pulse->start_index, status);
  373. return true;
  374. }
  375. edge_pulse++;
  376. }
  377. return false;
  378. }
  379. bool Rs232Decoder::QuantizeParamsDecodeRs232(const QuantizeParams& quantize_params, const ProtocolRs232Options& options,
  380. Rs232DecodeResult& decode_result)
  381. {
  382. return false;
  383. }
  384. bool Rs232Decoder::ParseRs232(const ProtocolRs232Options& options,
  385. const EdgePulseDataTwoLevels& edge_pulse_data,
  386. Rs232DecodeResult& decode_result)
  387. {
  388. return Rs232Decoder::DecodeRs232(options, edge_pulse_data.GetDataAddrPtr(),
  389. edge_pulse_data.edge_pulses_count,
  390. edge_pulse_data.waveform_data_count, edge_pulse_data.sample_rate,
  391. decode_result);
  392. }
  393. }