From 39c2050e8f76c3496f7aa612a14a341a01014475 Mon Sep 17 00:00:00 2001 From: baocm Date: Tue, 20 Jan 2026 16:24:47 +0800 Subject: [PATCH] =?UTF-8?q?1.=20mqttdds=E5=8E=BB=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E7=9A=84topic=E3=80=82=202.=20=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8D=A2=E8=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Publisher.cxx | 2 +- mqttdds/Publisher.cxx | 31 ------ mqttdds/Publisher.hpp | 3 - print/MsgHandler.cxx | 224 ++++++++++++++++++++++++++++++++++++++++-- print/MsgHandler.hpp | 5 +- 5 files changed, 223 insertions(+), 42 deletions(-) diff --git a/core/Publisher.cxx b/core/Publisher.cxx index b351071..6aba885 100644 --- a/core/Publisher.cxx +++ b/core/Publisher.cxx @@ -490,7 +490,7 @@ void PublisherApp::run() { if ((stable_weight != 0) && (license_no != "")) { - DEBUG("send summary"); + DEBUG("send summary"); WeighingSystem::SummaryUpdate info; info.License() = license_no; info.StableValue() = stable_weight; diff --git a/mqttdds/Publisher.cxx b/mqttdds/Publisher.cxx index a9dcc87..2f414c4 100644 --- a/mqttdds/Publisher.cxx +++ b/mqttdds/Publisher.cxx @@ -52,9 +52,6 @@ PublisherApp::PublisherApp( , light_topic_(nullptr) , light_writer_(nullptr) , light_type_(new WeighingSystem::LightsCommandUpdatePubSubType()) - , summary_topic_(nullptr) - , summary_writer_(nullptr) - , summary_type_(new WeighingSystem::SummaryUpdatePubSubType()) , print_topic_(nullptr) , print_writer_(nullptr) , print_type_(new InternalSystem::PrintReqPubSubType()) @@ -79,7 +76,6 @@ PublisherApp::PublisherApp( // Register the type bar_type_.register_type(participant_); light_type_.register_type(participant_); - summary_type_.register_type(participant_); print_type_.register_type(participant_); // Create the publisher @@ -145,33 +141,6 @@ PublisherApp::PublisherApp( throw std::runtime_error("WeighingSystem::LightsUpdate DataWriter initialization failed"); } - // Create the topic - topic_qos = TOPIC_QOS_DEFAULT; - participant_->get_default_topic_qos(topic_qos); - summary_topic_ = participant_->create_topic("SummaryUpdate", summary_type_.get_type_name(), topic_qos); - if (summary_topic_ == nullptr) - { - throw std::runtime_error("SummaryUpdate Topic initialization failed"); - } - - // Create the data writer - writer_qos = DATAWRITER_QOS_DEFAULT; - publisher_->get_default_datawriter_qos(writer_qos); - writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS; - writer_qos.reliability().max_blocking_time = Duration_t(1, 0); - writer_qos.durability().kind = DurabilityQosPolicyKind::VOLATILE_DURABILITY_QOS; - writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS; - writer_qos.history().depth = 1; - writer_qos.resource_limits().max_samples = 200; - writer_qos.resource_limits().max_instances = 1; - writer_qos.resource_limits().max_samples_per_instance = 100; - writer_qos.data_sharing().off(); - summary_writer_ = publisher_->create_datawriter(summary_topic_, writer_qos, this, StatusMask::all()); - if (summary_writer_ == nullptr) - { - throw std::runtime_error("WeighingSystem::SummaryUpdate DataWriter initialization failed"); - } - // Create the topic topic_qos = TOPIC_QOS_DEFAULT; participant_->get_default_topic_qos(topic_qos); diff --git a/mqttdds/Publisher.hpp b/mqttdds/Publisher.hpp index 227dd33..40051d4 100644 --- a/mqttdds/Publisher.hpp +++ b/mqttdds/Publisher.hpp @@ -72,9 +72,6 @@ private: eprosima::fastdds::dds::Topic* light_topic_; eprosima::fastdds::dds::DataWriter* light_writer_; eprosima::fastdds::dds::TypeSupport light_type_; - eprosima::fastdds::dds::Topic* summary_topic_; - eprosima::fastdds::dds::DataWriter* summary_writer_; - eprosima::fastdds::dds::TypeSupport summary_type_; eprosima::fastdds::dds::Topic* print_topic_; eprosima::fastdds::dds::DataWriter* print_writer_; eprosima::fastdds::dds::TypeSupport print_type_; diff --git a/print/MsgHandler.cxx b/print/MsgHandler.cxx index 362e333..1a64e36 100644 --- a/print/MsgHandler.cxx +++ b/print/MsgHandler.cxx @@ -444,6 +444,176 @@ void MsgHandler::GtstrLength(int iVal, char *p) memcpy(p, szTemp.c_str(), 4); } +// 判断是否为中文字符(包括中文标点) +bool MsgHandler::is_chinese_char(const std::string& str, size_t start_byte) { + if (start_byte >= str.length()) return false; + + uint8_t c = static_cast(str[start_byte]); + + // ASCII字符不是中文 + if (c < 0x80) return false; + + // 获取Unicode码点 + uint32_t code_point = 0; + int bytes = 0; + + if ((c & 0xF8) == 0xF0 && start_byte + 3 < str.length()) { + code_point = ((c & 0x07) << 18) | + ((static_cast(str[start_byte+1]) & 0x3F) << 12) | + ((static_cast(str[start_byte+2]) & 0x3F) << 6) | + (static_cast(str[start_byte+3]) & 0x3F); + bytes = 4; + } + else if ((c & 0xF0) == 0xE0 && start_byte + 2 < str.length()) { + code_point = ((c & 0x0F) << 12) | + ((static_cast(str[start_byte+1]) & 0x3F) << 6) | + (static_cast(str[start_byte+2]) & 0x3F); + bytes = 3; + } + else if ((c & 0xE0) == 0xC0 && start_byte + 1 < str.length()) { + code_point = ((c & 0x1F) << 6) | + (static_cast(str[start_byte+1]) & 0x3F); + bytes = 2; + } + + if (bytes == 0) return false; + + // 中文汉字范围 + if ((code_point >= 0x4E00 && code_point <= 0x9FFF) || + (code_point >= 0x3400 && code_point <= 0x4DBF) || + (code_point >= 0x20000 && code_point <= 0x2A6DF) || + (code_point >= 0x2A700 && code_point <= 0x2B73F) || + (code_point >= 0x2B740 && code_point <= 0x2B81F) || + (code_point >= 0x2B820 && code_point <= 0x2CEAF) || + (code_point >= 0x2CEB0 && code_point <= 0x2EBEF) || + (code_point >= 0x30000 && code_point <= 0x3134F)) { + return true; + } + + // 中文标点范围 + if ((code_point >= 0x3000 && code_point <= 0x303F) || + (code_point >= 0xFF00 && code_point <= 0xFFEF)) { + return true; + } + + return false; +} + +// 获取单个字符的显示宽度 +int MsgHandler::get_utf8_char_width(const std::string& str, size_t start_byte) { + if (start_byte >= str.length()) return 1; + + uint8_t c = static_cast(str[start_byte]); + + // ASCII字符占1个位置 + if (c < 0x80) { + return 1; + } + + // 判断是否为中文字符 + if (is_chinese_char(str, start_byte)) { + return 2; // 中文字符占2个位置 + } + + // 其他宽字符(如全角字符、emoji等) + return 2; // 默认按2个位置处理 +} + +// 获取整个字符串的显示宽度 +size_t MsgHandler::get_utf8_display_width(const std::string& str) { + size_t width = 0; + + for (size_t i = 0; i < str.length(); ) { + uint8_t c = static_cast(str[i]); + size_t char_len = 1; + + if ((c & 0x80) == 0x00) { + char_len = 1; + } else if ((c & 0xE0) == 0xC0) { + if (i + 1 >= str.length()) break; + char_len = 2; + } else if ((c & 0xF0) == 0xE0) { + if (i + 2 >= str.length()) break; + char_len = 3; + } else if ((c & 0xF8) == 0xF0) { + if (i + 3 >= str.length()) break; + char_len = 4; + } + + width += get_utf8_char_width(str, i); + i += char_len; + } + + return width; +} + +// 按显示宽度分割字符串 +std::vector MsgHandler::split_utf8_by_width(const std::string& str, size_t max_width) { + std::vector result; + + if (str.empty() || max_width == 0) return result; + + size_t byte_pos = 0; + + while (byte_pos < str.length()) { + size_t line_start = byte_pos; + size_t line_width = 0; + size_t last_valid_pos = byte_pos; + + // 收集一行的字符 + while (byte_pos < str.length() && line_width < max_width) { + uint8_t c = static_cast(str[byte_pos]); + size_t char_len = 1; + + if ((c & 0x80) == 0x00) { + char_len = 1; + } else if ((c & 0xE0) == 0xC0) { + if (byte_pos + 1 >= str.length()) break; + char_len = 2; + } else if ((c & 0xF0) == 0xE0) { + if (byte_pos + 2 >= str.length()) break; + char_len = 3; + } else if ((c & 0xF8) == 0xF0) { + if (byte_pos + 3 >= str.length()) break; + char_len = 4; + } + + int char_width = get_utf8_char_width(str, byte_pos); + + // 检查是否还能放下这个字符 + if (line_width + char_width > max_width) { + break; + } + + last_valid_pos = byte_pos + char_len - 1; + byte_pos += char_len; + line_width += char_width; + } + + // 确保至少有一个字符 + if (last_valid_pos < line_start && byte_pos < str.length()) { + uint8_t c = static_cast(str[byte_pos]); + size_t char_len = 1; + + if ((c & 0xE0) == 0xC0) char_len = 2; + else if ((c & 0xF0) == 0xE0) char_len = 3; + else if ((c & 0xF8) == 0xF0) char_len = 4; + + last_valid_pos = byte_pos + char_len - 1; + byte_pos = last_valid_pos + 1; + } + + // 提取这一行 + if (last_valid_pos >= line_start) { + result.push_back(str.substr(line_start, last_valid_pos - line_start + 1)); + } + + byte_pos = last_valid_pos + 1; + } + + return result; +} + //HS-K33打印通用称重 void MsgHandler::Print1_3(const std::map& msg) { @@ -1101,20 +1271,62 @@ void MsgHandler::Print1_3(const std::map& msg) //=========备注===================== + const size_t MAX_DISPLAY_WIDTH = 32; // 每行最多32个显示位置 + m_newObj.clear(); m_newObj[1] = ("│"); m_newObj[26] = ("提示"); m_newObj[120] = ("│"); + it = msg.find("Warning"); if(it != msg.end()) { - m_newObj[145] = it->second; + // 检查显示宽度是否超过限制 + size_t display_width = get_utf8_display_width(it->second); + + if (display_width > MAX_DISPLAY_WIDTH) { + // 需要分割成多行显示 + + // 第一行:显示前32个宽度的内容 + std::vector lines = split_utf8_by_width(it->second, MAX_DISPLAY_WIDTH); + + // 输出第一行(包含前缀) + m_newObj[145] = lines[0]; + m_newObj[528] = ("│"); + blTemp = BuildStringLine(m_newObj); + m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end()); + m_PrintData.insert(m_PrintData.end(), {0x0A}); + + // 输出剩余行(不包含前缀,但有边框) + for (size_t i = 1; i < lines.size(); i++) { + m_newObj.clear(); + m_newObj[1] = ("│"); + m_newObj[120] = ("│"); + m_newObj[145] = lines[i]; + m_newObj[528] = ("│"); + + blTemp = BuildStringLine(m_newObj); + m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end()); + m_PrintData.insert(m_PrintData.end(), {0x0A}); + } + } + else { + // 宽度不超过限制,正常显示 + m_newObj[145] = it->second; + m_newObj[528] = ("│"); + blTemp = BuildStringLine(m_newObj); + m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end()); + m_PrintData.insert(m_PrintData.end(), {0x0A}); + } + } + else { + // 没有找到Warning,显示空内容 + m_newObj[528] = ("│"); + m_newObj[145] = ""; + blTemp = BuildStringLine(m_newObj); + m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end()); + m_PrintData.insert(m_PrintData.end(), {0x0A}); } - m_newObj[528] = ("│"); - blTemp = BuildStringLine(m_newObj); - m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end()); - m_PrintData.insert(m_PrintData.end(), {0x0A}); - m_newObj.clear(); m_newObj[1] = ("└"); diff --git a/print/MsgHandler.hpp b/print/MsgHandler.hpp index 60454ba..9627c3a 100644 --- a/print/MsgHandler.hpp +++ b/print/MsgHandler.hpp @@ -44,7 +44,10 @@ private: void Page_Cut(std::vector& Data); void Page_End(std::vector& Data); void GtstrLength(int iVal, char *p); - + bool is_chinese_char(const std::string& str, size_t start_byte); + int get_utf8_char_width(const std::string& str, size_t start_byte); + size_t get_utf8_display_width(const std::string& str); + std::vector split_utf8_by_width(const std::string& str, size_t max_width); void Print1_3(const std::map& msg); };