1. mqttdds去除多余的topic。

2. 打印优化换行。
main
baocm 8 months ago
parent 1a8edf682b
commit 39c2050e8f

@ -52,9 +52,6 @@ PublisherApp::PublisherApp(
, light_topic_(nullptr) , light_topic_(nullptr)
, light_writer_(nullptr) , light_writer_(nullptr)
, light_type_(new WeighingSystem::LightsCommandUpdatePubSubType()) , light_type_(new WeighingSystem::LightsCommandUpdatePubSubType())
, summary_topic_(nullptr)
, summary_writer_(nullptr)
, summary_type_(new WeighingSystem::SummaryUpdatePubSubType())
, print_topic_(nullptr) , print_topic_(nullptr)
, print_writer_(nullptr) , print_writer_(nullptr)
, print_type_(new InternalSystem::PrintReqPubSubType()) , print_type_(new InternalSystem::PrintReqPubSubType())
@ -79,7 +76,6 @@ PublisherApp::PublisherApp(
// Register the type // Register the type
bar_type_.register_type(participant_); bar_type_.register_type(participant_);
light_type_.register_type(participant_); light_type_.register_type(participant_);
summary_type_.register_type(participant_);
print_type_.register_type(participant_); print_type_.register_type(participant_);
// Create the publisher // Create the publisher
@ -145,33 +141,6 @@ PublisherApp::PublisherApp(
throw std::runtime_error("WeighingSystem::LightsUpdate DataWriter initialization failed"); 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 // Create the topic
topic_qos = TOPIC_QOS_DEFAULT; topic_qos = TOPIC_QOS_DEFAULT;
participant_->get_default_topic_qos(topic_qos); participant_->get_default_topic_qos(topic_qos);

@ -72,9 +72,6 @@ private:
eprosima::fastdds::dds::Topic* light_topic_; eprosima::fastdds::dds::Topic* light_topic_;
eprosima::fastdds::dds::DataWriter* light_writer_; eprosima::fastdds::dds::DataWriter* light_writer_;
eprosima::fastdds::dds::TypeSupport light_type_; 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::Topic* print_topic_;
eprosima::fastdds::dds::DataWriter* print_writer_; eprosima::fastdds::dds::DataWriter* print_writer_;
eprosima::fastdds::dds::TypeSupport print_type_; eprosima::fastdds::dds::TypeSupport print_type_;

@ -444,6 +444,176 @@ void MsgHandler::GtstrLength(int iVal, char *p)
memcpy(p, szTemp.c_str(), 4); 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<uint8_t>(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<uint8_t>(str[start_byte+1]) & 0x3F) << 12) |
((static_cast<uint8_t>(str[start_byte+2]) & 0x3F) << 6) |
(static_cast<uint8_t>(str[start_byte+3]) & 0x3F);
bytes = 4;
}
else if ((c & 0xF0) == 0xE0 && start_byte + 2 < str.length()) {
code_point = ((c & 0x0F) << 12) |
((static_cast<uint8_t>(str[start_byte+1]) & 0x3F) << 6) |
(static_cast<uint8_t>(str[start_byte+2]) & 0x3F);
bytes = 3;
}
else if ((c & 0xE0) == 0xC0 && start_byte + 1 < str.length()) {
code_point = ((c & 0x1F) << 6) |
(static_cast<uint8_t>(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<uint8_t>(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<uint8_t>(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<std::string> MsgHandler::split_utf8_by_width(const std::string& str, size_t max_width) {
std::vector<std::string> 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<uint8_t>(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<uint8_t>(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打印通用称重 //HS-K33打印通用称重
void MsgHandler::Print1_3(const std::map<std::string, std::string>& msg) void MsgHandler::Print1_3(const std::map<std::string, std::string>& msg)
{ {
@ -1101,20 +1271,62 @@ void MsgHandler::Print1_3(const std::map<std::string, std::string>& msg)
//=========备注===================== //=========备注=====================
const size_t MAX_DISPLAY_WIDTH = 32; // 每行最多32个显示位置
m_newObj.clear(); m_newObj.clear();
m_newObj[1] = (""); m_newObj[1] = ("");
m_newObj[26] = ("提示"); m_newObj[26] = ("提示");
m_newObj[120] = (""); m_newObj[120] = ("");
it = msg.find("Warning"); it = msg.find("Warning");
if(it != msg.end()) if(it != msg.end())
{ {
// 检查显示宽度是否超过限制
size_t display_width = get_utf8_display_width(it->second);
if (display_width > MAX_DISPLAY_WIDTH) {
// 需要分割成多行显示
// 第一行显示前32个宽度的内容
std::vector<std::string> 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[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[528] = ("");
m_newObj[145] = "";
blTemp = BuildStringLine(m_newObj); blTemp = BuildStringLine(m_newObj);
m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end()); m_PrintData.insert(m_PrintData.end(), blTemp.begin(), blTemp.end());
m_PrintData.insert(m_PrintData.end(), {0x0A}); m_PrintData.insert(m_PrintData.end(), {0x0A});
}
m_newObj.clear(); m_newObj.clear();
m_newObj[1] = (""); m_newObj[1] = ("");

@ -44,7 +44,10 @@ private:
void Page_Cut(std::vector<uint8_t>& Data); void Page_Cut(std::vector<uint8_t>& Data);
void Page_End(std::vector<uint8_t>& Data); void Page_End(std::vector<uint8_t>& Data);
void GtstrLength(int iVal, char *p); 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<std::string> split_utf8_by_width(const std::string& str, size_t max_width);
void Print1_3(const std::map<std::string, std::string>& msg); void Print1_3(const std::map<std::string, std::string>& msg);
}; };

Loading…
Cancel
Save