123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- // // ******************************************************************
- // // /\ /| @File rs232_decoder.cc
- // // \ V/ @Brief
- // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
- // // / | @Creation 2024-06-26
- // // / \\ @Modified 2024-07-16
- // // *(__\_\
- // // ******************************************************************
- #include "rs232_decoder.h"
- #include "../BaseEnums/data_check_enums.h"
- #include "../BaseHelper/data_check_helper.h"
- #include "../BaseHelper/Loger.h"
- namespace Protocol
- {
-
- int32_t Rs232Decoder::GetStartIndex(const ProtocolRs232Options& options, TwoLevelEdgePulse*& node,
- const double count,
- double& real_count,
- const int32_t start_index,
- TwoLevelEdgePulseStatusType& start_status)
- {
- start_status = options.polarity == Polarity::POS
- ? TwoLevelEdgePulseStatusType::High
- : TwoLevelEdgePulseStatusType::Low;
- while (node != nullptr)
- {
- if (node->current_level != start_status && node->start_index >= start_index)
- {
- //计算该脉宽中,最大包括几个bit信息宽度
- const double bit_count = std::round(node->GetLength() / count);
- if (bit_count > 0 && bit_count < static_cast<int32_t>(options.data_bit_width) + 4)
- {
- real_count = node->GetLength() / bit_count;
- }
- if (bit_count > 0) break;
- }
- node++;
- }
- return node == nullptr ? -1 : node->start_index;
- }
- bool Rs232Decoder::DecodeRs232(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)
- {
- WriteLog(LogLevel::LevelDebug, " SignalType:%d, DataBitWidth:%d"
- , static_cast<int>(options.signal_type), static_cast<int>(options.data_bit_width));
- if (options.is_cancel)
- {
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
- return false;
- }
- //栈上 初始化内存
- std::vector<Rs232Packet> rs232_packets;
- rs232_packets = {};
- decode_events_ = {};
- decode_result_units_ = {};
- auto decode_result_units_ptr = &decode_result_units_;
- auto decode_events_ptr = &decode_events_;
- WriteLog(LogLevel::LevelDebug, " decodeResultUnitsPtr:0x%X\n", static_cast<void*>(decode_result_units_ptr));
- WriteLog(LogLevel::LevelDebug, " decodeEvents_Ptr:0x%X\n", static_cast<void*>(decode_events_ptr));
- if (options.is_cancel)
- {
- 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 data_bit_count = 0;
- switch (options.data_bit_width)
- {
- case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_5_BIT:
- data_bit_count = 5;
- break;
- case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_6_BIT:
- data_bit_count = 6;
- break;
- case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_7_BIT:
- data_bit_count = 7;
- break;
- case Rs232Enums::DataBitWidth::DATA_BIT_WIDTH_8_BIT:
- data_bit_count = 8;
- break;
- }
- uint64_t edge_pulse_index = 0;
- //每bit理论长度
- const double count = 1.0 / options.baud_rate * sample_rate;
- WriteLog(LogLevel::LevelDebug, " bit count:%f", count);
- //const double count = static_cast<double>(dataCount);
- //每bit实际长度
- double real_count = count;
- const int32_t stop_bit_count = options.stop_bit == Rs232Enums::StopBit::STOP_BIT_1_BIT
- ? 1
- : options.stop_bit == Rs232Enums::StopBit::STOP_BIT_2_BIT
- ? 2
- : 1;
- WriteLog(LogLevel::LevelDebug, " realCount:%f", real_count);
- WriteLog(LogLevel::LevelDebug, " edgePulsePointer:0x%x", reinterpret_cast<void*>(edge_pulses));
- //WriteLog(" edgePulsePointer:0x%x", edgePulses);
- WriteLog(LogLevel::LevelDebug, " edgePulse StartIndex:%d", edge_pulses->start_index);
- WriteLog(LogLevel::LevelDebug, " edgePulse EndIndex:%d", edge_pulses->end_index);
- WriteLog(LogLevel::LevelDebug, " edgePulseCount:%d", edge_pulse_count);
- try
- {
- int32_t start_index = 0;
- while (edge_pulses != nullptr && edge_pulse_index < edge_pulse_count)
- {
- if (count <= 2) break;
- TwoLevelEdgePulseStatusType level_state;
- //找到帧头 根据帧头得到实际bit宽度realCount 帧头起始位置startIndex 电平状态levelState
- int32_t packet_start_index =
- GetStartIndex(options, edge_pulses, count, real_count, start_index, level_state);
- //WriteLog(" GetStartIndex PacketStartIndex:%d\n", packetStartIndex);
- if (options.is_cancel)
- {
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
- return false;
- }
- if (packet_start_index == -1) break;
- //std::cout << " packetStartIndex:" << packetStartIndex << " ,realCount:" << realCount << "\n";
- //构造帧
- Rs232Packet packet = {};
- packet_start_index += static_cast<int32_t>(std::round(real_count / 2));
- if (edge_pulses->start_index >= edge_pulses->end_index)
- {
- std::ostringstream msg;
- msg << " =>178 loop break Index:" << packet_start_index << " ,dataCount:" <<
- waveform_data_count << "\n";
- WriteLog(LogLevel::LevelDebug, msg.str().c_str());
- break;
- }
- if (packet_start_index >= static_cast<int32_t>(waveform_data_count))
- {
- std::ostringstream msg;
- msg << " =>186 loop break Index:" << packet_start_index << " ,dataCount:" <<
- waveform_data_count << "\n";
- WriteLog(LogLevel::LevelDebug, msg.str().c_str());
- break;
- }
- if (start_index == static_cast<int32_t>(waveform_data_count)) break;
- packet.start_bit = level_state == TwoLevelEdgePulseStatusType::High;
- packet.start_index = packet_start_index - static_cast<int32_t>(std::round(real_count / 2));
- packet.data_index = packet_start_index + static_cast<int32_t>(std::round(real_count / 2));
- packet.per_bit_length = real_count;
- WriteLog(LogLevel::LevelDebug, "=> Tst StartIndex:%d,dataBitCount:%d", packet.start_index, data_bit_count);
- for (int32_t data_bit_index = 0; data_bit_index < data_bit_count; data_bit_index++)
- {
- if (options.is_cancel)
- {
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
- return false;
- }
- packet_start_index += static_cast<int32_t>(std::round(real_count));
- //std::cout << " P150 Test" << "\n";
- if (packet_start_index >= static_cast<int32_t>(waveform_data_count))
- {
- start_index = static_cast<int32_t>(waveform_data_count);
- WriteLog(LogLevel::LevelDebug, " =>%d break \n", __LINE__);
- break;
- }
- //std::cout << " P157 Test" << "\n";
- TwoLevelEdgePulseStatusType bit_status;
- if (!GetRs232Bit(edge_pulses, packet_start_index, bit_status))
- {
- //gMutex.unlock(); // 执行完操作后手动释放锁
- return false;
- }
- //按位构造数据
- if (options.msb_or_lsb == MSBOrLSB::MSB)
- {
- packet.data = static_cast<uint8_t>(packet.data << 1);
- packet.data |= bit_status == TwoLevelEdgePulseStatusType::High ? 1 : 0;
- }
- else
- {
- packet.data |= static_cast<uint8_t>((bit_status ==
- TwoLevelEdgePulseStatusType::High
- ? 1
- : 0) << data_bit_index);
- }
- WriteLog(LogLevel::LevelDebug, "Tst CurrentLevel:%d, bitStatus:%d", edge_pulses->current_level,
- bit_status);
- }
- //std::cout << " P177 Test" << "\n";
- if (start_index == static_cast<int32_t>(waveform_data_count)) break;
- //校验位
- if (options.odd_even_check_type != OddEvenCheck::None)
- {
- packet_start_index += static_cast<int32_t>(std::round(real_count));
- if (packet_start_index >= 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 bit_status;
- if (!GetRs232Bit(edge_pulses, packet_start_index, bit_status))
- {
- //gMutex.unlock(); // 执行完操作后手动释放锁
- return false;
- }
- packet.parity_find = true;
- packet.parity_bit = bit_status == TwoLevelEdgePulseStatusType::High;
- packet.parity_result = DataCheckHelper::CheckDataByOddEven(
- packet.data, data_bit_count, options.odd_even_check_type);
- packet.parity_index = packet.data_index + static_cast<int32_t>(
- std::round(real_count * data_bit_count));
- }
- //std::cout << " P203 Test" << "\n";
- rs232_packets.push_back(packet);
- packet_start_index += static_cast<int32_t>(real_count * stop_bit_count);
- start_index = packet_start_index;
- edge_pulse_index++;
- }
- }
- catch (const char* ex)
- {
- WriteLog(LogLevel::LevelDebug, " ParsingData L%d catch :%s", __LINE__, ex);
- return false;
- }
- {
- std::ostringstream msg;
- msg << " edgePulseIndex:" << edge_pulse_index << ",";
- 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())
- {
- decode_event_units_storage_ = {};
- //for (int64_t i = 0; i < static_cast<int64_t>(rs232Packets.size()); i++)
- for (auto packet : rs232_packets)
- {
- if (options.is_cancel)
- {
- WriteLog(LogLevel::Level2, " ParsingData canceled.\n");
- //gMutex.unlock(); // 执行完操作后手动释放锁
- return false;
- }
- Rs232DecodeResultCell result_unit;
- Rs232DecodeEvent decode_event = {};
- Rs232DecodeEvent* event_ptr;
- event_ptr = &decode_event;
- Rs232DecodeEventUnit event_start;
- Rs232DecodeEventUnit event_data;
- int64_t event_start_index = packet.start_index;
- int64_t one_bit_length = static_cast<int64_t>(packet.per_bit_length);
- event_ptr->start_index = packet.start_index;
- event_start.start_index = packet.start_index;
- event_start.length = one_bit_length;
- event_start_index += 1;
- event_start.event_type = Rs232Enums::Rs232DecodeEventType::START;
- event_data.start_index = event_start.start_index;
- event_data.length = one_bit_length * data_bit_count;
- event_data.data = packet.data;
- event_data.event_type = Rs232Enums::Rs232DecodeEventType::DATA;
- event_start_index += 8;
- //std::cout << " P249 Test I:" << i << "\n";
- if (packet.parity_find)
- {
- Rs232DecodeEventUnit event_uints[3];
- event_uints[0] = event_start;
- event_uints[1] = event_data;
- Rs232DecodeEventUnit event_parity;
- //std::cout << " P253 Test I:" << i << "\n";
- event_parity.data = packet.parity_result ? 1 : 0;
- event_parity.start_index = event_start_index;
- event_parity.length = one_bit_length;
- event_parity.event_type = Rs232Enums::Rs232DecodeEventType::PARITY;
- event_ptr->parity_result = packet.parity_result ? 1 : 0;
- event_uints[2] = event_parity;
- //eventPtr->EventData = eventUints;
- event_start_index += 8;
- event_ptr->event_data_count = 3;
- event_ptr->end_index = packet.start_index + one_bit_length * (data_bit_count + 1);
- // 将eventUints复制到decodeEventUnitsStorage中
- decode_event_units_storage_.insert(decode_event_units_storage_.end(), std::begin(event_uints),
- std::end(event_uints));
- //最后来赋值
- //decodeEvent.EventData = decodeEventUnitsStorage_.data() + decodeEventUnitsStorage_.size() - 3;
- }
- else
- {
- Rs232DecodeEventUnit event_uints[2];
- event_uints[0] = event_start;
- event_uints[1] = event_data;
- event_ptr->event_data_count = 2;
- //eventPtr->EventData = eventUints;
- //std::cout << " P263 Test I:" << i << "\n";
- event_ptr->parity_result = 0;
- event_ptr->end_index = packet.start_index + one_bit_length * (data_bit_count);
- // 将eventUints复制到decodeEventUnitsStorage中
- decode_event_units_storage_.insert(decode_event_units_storage_.end(), std::begin(event_uints),
- std::end(event_uints));
- //最后来赋值
- //decodeEvent.EventData = decodeEventUnitsStorage_.data() + decodeEventUnitsStorage_.size() - 2;
- }
- //std::cout << " P266 Test I:" << i << "\n";
- result_unit.start_index = packet.start_index;
- //最后来赋值
- //resultUnit.data = &packet.data;
- WriteLog(LogLevel::LevelDebug, " Tst Data Ptr:0x%x , Data:%d", static_cast<void*>(result_unit.data),
- packet.data);
- result_unit.data_count = 1;
- result_unit.length = event_start_index - packet.start_index;
- event_ptr->event_index = static_cast<int64_t>(decode_events_ptr->size());
- decode_events_.push_back(decode_event);
- decode_result_units_.push_back(result_unit);
- decode_result.decode_event_need_update = true;
- decode_result.decode_result_need_update = true;
- }
- }
- /////////////////////////// 指针赋值 /////////////////////////////////
- int64_t event_data_index = 0;
- for (int i = 0; i < decode_events_.size(); i++) // NOLINT(modernize-loop-convert, clang-diagnostic-sign-compare)
- {
- decode_events_[i].event_data = decode_event_units_storage_.data() + event_data_index;
- event_data_index += decode_events_[i].event_data_count;
- }
- for (int i = 0; i < decode_result_units_.size(); i++) // NOLINT(clang-diagnostic-sign-compare)
- {
- int64_t start_index = decode_result_units_[i].start_index;
- for (int x = 0; i < rs232_packets.size(); x++) // NOLINT(clang-diagnostic-sign-compare)
- {
- if (rs232_packets[x].start_index == start_index)
- {
- decode_result_units_[i].data = &(rs232_packets[x].data);
- break;
- }
- }
- }
- decode_result.decode_events_ptr = decode_events_ptr->data();
- decode_result.decode_result_cells_ptr = decode_result_units_ptr->data();
- decode_result.decode_event_count = decode_events_ptr->size();
- decode_result.decode_result_count = decode_result_units_ptr->size();
- WriteLog(LogLevel::Level1, " Cpp ParsingData Done,return True \n\n");
- return true;
- }
- std::vector<const char*> Rs232Decoder::GetEventInfoTitles()
- {
- return event_info_titles_;
- }
- bool Rs232Decoder::GetRs232Bit(TwoLevelEdgePulse*& edge_pulse,
- const int32_t target_index, TwoLevelEdgePulseStatusType& status)
- {
- status = TwoLevelEdgePulseStatusType::None;
- if (edge_pulse == nullptr)
- {
- return false;
- }
- if (target_index <= 0 || edge_pulse->start_index > target_index)
- {
- return false;
- }
- while (edge_pulse != nullptr)
- {
- if (edge_pulse->start_index <= target_index && edge_pulse->end_index > target_index)
- {
- status = edge_pulse->current_level;
- WriteLog(LogLevel::LevelDebug, "StartIndex:%d , status:%d", edge_pulse->start_index, status);
- return true;
- }
- edge_pulse++;
- }
- return false;
- }
- bool Rs232Decoder::QuantizeParamsDecodeRs232(const QuantizeParams& quantize_params, const ProtocolRs232Options& options,
- Rs232DecodeResult& decode_result)
- {
- return false;
- }
- bool Rs232Decoder::ParseRs232(const ProtocolRs232Options& options,
- const EdgePulseDataTwoLevels& edge_pulse_data,
- Rs232DecodeResult& decode_result)
- {
- return Rs232Decoder::DecodeRs232(options, edge_pulse_data.GetDataAddrPtr(),
- edge_pulse_data.edge_pulses_count,
- edge_pulse_data.waveform_data_count, edge_pulse_data.sample_rate,
- decode_result);
- }
- }
|