diff --git a/src/system/libmodbus_m/src/modbus_m.cpp b/src/system/libmodbus_m/src/modbus_m.cpp index ab56d0f..0caadd8 100644 --- a/src/system/libmodbus_m/src/modbus_m.cpp +++ b/src/system/libmodbus_m/src/modbus_m.cpp @@ -20,20 +20,30 @@ LOCAL std::vector g_write_queue; LOCAL std::string get_base_path() { - char proc_dir[512] = {0}; - if(0 != func_proc_self_dir(proc_dir, sizeof(proc_dir))) + char buf[512] = {0}; + + /* 优先使用 WORK_PATH 环境变量(RK3568 装置运行时设置) */ + if (0 == func_get_work_path(buf, sizeof(buf))) { - MY_LOG_E("func_get_process_self_dir failed"); + return std::string(buf) + "/"; + } + + /* fallback: 从可执行文件路径推导(本地调试用) */ + if (0 != func_proc_self_dir(buf, sizeof(buf))) + { + LOG_E("func_proc_self_dir failed"); + return ""; } - return std::string(proc_dir); + + return std::string(buf) + "/"; } // ==================== 数据类型映射 ==================== LOCAL uint8_t modbus_type_to_local_type(uint8_t type) { - switch(type) + switch (type) { case 2: return DATA_TYPE_B; // bit/线圈 case 4: return DATA_TYPE_U16; // uint16 @@ -46,7 +56,7 @@ LOCAL uint8_t modbus_type_to_local_type(uint8_t type) LOCAL uint8_t dc_data_type_size(uint8_t data_type) { - switch(data_type) + switch (data_type) { case DATA_TYPE_B: return 1; case DATA_TYPE_U8: return 1; @@ -65,7 +75,7 @@ LOCAL uint8_t dc_data_type_size(uint8_t data_type) LOCAL int parse_channels(XMLElement *p_root) { XMLElement *p_ch = p_root->FirstChildElement("Channel"); - while(NULL != p_ch) + while (NULL != p_ch) { stru_modbus_m_channel ch; ch.id = (uint32_t)p_ch->IntAttribute("id", 0); @@ -83,14 +93,14 @@ LOCAL int parse_channels(XMLElement *p_root) ch.connected = false; g_modbus_m_cfg.channels.push_back(ch); - MY_LOG_I("channel %d: mode=%s, host=%s:%d", ch.id, ch.mode.c_str(), ch.host.c_str(), ch.port); + LOG_I("channel %d: mode=%s, host=%s:%d", ch.id, ch.mode.c_str(), ch.host.c_str(), ch.port); p_ch = p_ch->NextSiblingElement("Channel"); } - if(g_modbus_m_cfg.channels.empty()) + if (g_modbus_m_cfg.channels.empty()) { - MY_LOG_E("no channel configured"); + LOG_E("no channel configured"); return -1; } return 0; @@ -122,33 +132,33 @@ LOCAL int parse_point_item(XMLElement *p_item, std::vector & item.factor = (float)p_item->FloatAttribute("factor", 1.0f); item.offset = (float)p_item->FloatAttribute("offset", 0.0f); - if(NULL != p_item->Attribute("saddr")) + if (NULL != p_item->Attribute("saddr")) item.saddr = p_item->Attribute("saddr"); - if(NULL != p_item->Attribute("desc")) + if (NULL != p_item->Attribute("desc")) item.desc = p_item->Attribute("desc"); // 验证通道ID bool ch_found = false; - for(auto &ch : g_modbus_m_cfg.channels) + for (auto &ch : g_modbus_m_cfg.channels) { - if(ch.id == item.channel) + if (ch.id == item.channel) { ch_found = true; break; } } - if(!ch_found) + if (!ch_found) { - MY_LOG_E("item no=%d: channel %d not found", item.no, item.channel); + LOG_E("item no=%d: channel %d not found", item.no, item.channel); return -1; } // 创建数据指针 uint8_t local_type = modbus_type_to_local_type(item.type); item.p_val = dc_create_data(local_type); - if(NULL == item.p_val) + if (NULL == item.p_val) { - MY_LOG_E("dc_create_data_ptr_by_type failed, type=%d", local_type); + LOG_E("dc_create_data_ptr_by_type failed, type=%d", local_type); return -1; } @@ -159,77 +169,77 @@ LOCAL int parse_point_item(XMLElement *p_item, std::vector & LOCAL int parse_points(XMLElement *p_root) { XMLElement *p_point = p_root->FirstChildElement("Point"); - if(NULL == p_point) + if (NULL == p_point) { - MY_LOG_E("Point not found"); + LOG_E("Point not found"); return -1; } - if(NULL != p_point->Attribute("desc")) + if (NULL != p_point->Attribute("desc")) g_modbus_m_cfg.point.desc = p_point->Attribute("desc"); // 解析 St(遥信) XMLElement *p_st = p_point->FirstChildElement("St"); - if(NULL != p_st) + if (NULL != p_st) { XMLElement *p_item = p_st->FirstChildElement("Item"); - while(NULL != p_item) + while (NULL != p_item) { - if(0 != parse_point_item(p_item, g_modbus_m_cfg.point.st_vec)) + if (0 != parse_point_item(p_item, g_modbus_m_cfg.point.st_vec)) { return -1; } p_item = p_item->NextSiblingElement("Item"); } - MY_LOG_I("parsed %zu st items", g_modbus_m_cfg.point.st_vec.size()); + LOG_I("parsed %zu st items", g_modbus_m_cfg.point.st_vec.size()); } // 解析 Mx(遥测) XMLElement *p_mx = p_point->FirstChildElement("Mx"); - if(NULL != p_mx) + if (NULL != p_mx) { XMLElement *p_item = p_mx->FirstChildElement("Item"); - while(NULL != p_item) + while (NULL != p_item) { - if(0 != parse_point_item(p_item, g_modbus_m_cfg.point.mx_vec)) + if (0 != parse_point_item(p_item, g_modbus_m_cfg.point.mx_vec)) { return -1; } p_item = p_item->NextSiblingElement("Item"); } - MY_LOG_I("parsed %zu mx items", g_modbus_m_cfg.point.mx_vec.size()); + LOG_I("parsed %zu mx items", g_modbus_m_cfg.point.mx_vec.size()); } // 解析 Co(遥控) XMLElement *p_co = p_point->FirstChildElement("Co"); - if(NULL != p_co) + if (NULL != p_co) { XMLElement *p_item = p_co->FirstChildElement("Item"); - while(NULL != p_item) + while (NULL != p_item) { - if(0 != parse_point_item(p_item, g_modbus_m_cfg.point.co_vec)) + if (0 != parse_point_item(p_item, g_modbus_m_cfg.point.co_vec)) { return -1; } p_item = p_item->NextSiblingElement("Item"); } - MY_LOG_I("parsed %zu co items", g_modbus_m_cfg.point.co_vec.size()); + LOG_I("parsed %zu co items", g_modbus_m_cfg.point.co_vec.size()); } // 解析 Ao(参数) XMLElement *p_ao = p_point->FirstChildElement("Ao"); - if(NULL != p_ao) + if (NULL != p_ao) { XMLElement *p_item = p_ao->FirstChildElement("Item"); - while(NULL != p_item) + while (NULL != p_item) { - if(0 != parse_point_item(p_item, g_modbus_m_cfg.point.ao_vec)) + if (0 != parse_point_item(p_item, g_modbus_m_cfg.point.ao_vec)) { return -1; } p_item = p_item->NextSiblingElement("Item"); } - MY_LOG_I("parsed %zu ao items", g_modbus_m_cfg.point.ao_vec.size()); + LOG_I("parsed %zu ao items", g_modbus_m_cfg.point.ao_vec.size()); } return 0; @@ -238,32 +248,32 @@ LOCAL int parse_points(XMLElement *p_root) int modbus_m_cfg_parse(const std::string &path) { XMLDocument doc; - if(XML_SUCCESS != doc.LoadFile(path.c_str())) + if (XML_SUCCESS != doc.LoadFile(path.c_str())) { - MY_LOG_E("load xml failed: %s", path.c_str()); + LOG_E("load xml failed: %s", path.c_str()); return -1; } XMLElement *p_root = doc.RootElement(); - if(NULL == p_root) + if (NULL == p_root) { - MY_LOG_E("root element not found"); + LOG_E("root element not found"); return -1; } - if(0 != parse_channels(p_root)) + if (0 != parse_channels(p_root)) { - MY_LOG_E("parse channels failed"); + LOG_E("parse channels failed"); return -1; } - if(0 != parse_points(p_root)) + if (0 != parse_points(p_root)) { - MY_LOG_E("parse points failed"); + LOG_E("parse points failed"); return -1; } - MY_LOG_I("modbus_m cfg parsed: %zu channels, st=%zu, mx=%zu, co=%zu, ao=%zu", + LOG_I("modbus_m cfg parsed: %zu channels, st=%zu, mx=%zu, co=%zu, ao=%zu", g_modbus_m_cfg.channels.size(), g_modbus_m_cfg.point.st_vec.size(), g_modbus_m_cfg.point.mx_vec.size(), @@ -278,13 +288,13 @@ int modbus_m_cfg_parse(const std::string &path) LOCAL int channel_connect(stru_modbus_m_channel *p_ch) { - if(NULL == p_ch) + if (NULL == p_ch) { return -1; } // 释放旧连接 - if(NULL != p_ch->ctx) + if (NULL != p_ch->ctx) { modbus_close(p_ch->ctx); modbus_free(p_ch->ctx); @@ -292,29 +302,29 @@ LOCAL int channel_connect(stru_modbus_m_channel *p_ch) } // 创建上下文 - if(p_ch->mode == "tcp") + if (p_ch->mode == "tcp") { p_ch->ctx = modbus_new_tcp(p_ch->host.c_str(), p_ch->port); - if(NULL == p_ch->ctx) + if (NULL == p_ch->ctx) { - MY_LOG_E("modbus_new_tcp failed: %s:%d", p_ch->host.c_str(), p_ch->port); + LOG_E("modbus_new_tcp failed: %s:%d", p_ch->host.c_str(), p_ch->port); return -1; } // TCP 模式下设置默认从站地址 modbus_set_slave(p_ch->ctx, MODBUS_TCP_SLAVE); } - else if(p_ch->mode == "rtu") + else if (p_ch->mode == "rtu") { p_ch->ctx = modbus_new_rtu(p_ch->device.c_str(), p_ch->baud, p_ch->parity, p_ch->data_bit, p_ch->stop_bit); - if(NULL == p_ch->ctx) + if (NULL == p_ch->ctx) { - MY_LOG_E("modbus_new_rtu failed: %s", p_ch->device.c_str()); + LOG_E("modbus_new_rtu failed: %s", p_ch->device.c_str()); return -1; } } else { - MY_LOG_E("unknown mode: %s", p_ch->mode.c_str()); + LOG_E("unknown mode: %s", p_ch->mode.c_str()); return -1; } @@ -325,9 +335,9 @@ LOCAL int channel_connect(stru_modbus_m_channel *p_ch) modbus_set_error_recovery(p_ch->ctx, (modbus_error_recovery_mode)(MODBUS_ERROR_RECOVERY_LINK | MODBUS_ERROR_RECOVERY_PROTOCOL)); // 建立连接 - if(0 != modbus_connect(p_ch->ctx)) + if (0 != modbus_connect(p_ch->ctx)) { - MY_LOG_E("modbus_connect failed: ch=%d, mode=%s", p_ch->id, p_ch->mode.c_str()); + LOG_E("modbus_connect failed: ch=%d, mode=%s", p_ch->id, p_ch->mode.c_str()); modbus_free(p_ch->ctx); p_ch->ctx = NULL; p_ch->connected = false; @@ -335,13 +345,13 @@ LOCAL int channel_connect(stru_modbus_m_channel *p_ch) } p_ch->connected = true; - MY_LOG_I("channel %d connected: mode=%s", p_ch->id, p_ch->mode.c_str()); + LOG_I("channel %d connected: mode=%s", p_ch->id, p_ch->mode.c_str()); return 0; } LOCAL int channels_init() { - for(auto &ch : g_modbus_m_cfg.channels) + for (auto &ch : g_modbus_m_cfg.channels) { channel_connect(&ch); } @@ -350,9 +360,9 @@ LOCAL int channels_init() LOCAL void channels_close() { - for(auto &ch : g_modbus_m_cfg.channels) + for (auto &ch : g_modbus_m_cfg.channels) { - if(NULL != ch.ctx) + if (NULL != ch.ctx) { modbus_close(ch.ctx); modbus_free(ch.ctx); @@ -368,7 +378,7 @@ LOCAL void channels_close() // 读取线圈 (FC 0x01) 或离散输入 (FC 0x02) LOCAL int read_bits(stru_modbus_m_channel *p_ch, uint8_t slave, uint8_t fc, uint16_t addr, uint16_t count, uint8_t *p_dest) { - if(NULL == p_ch || NULL == p_ch->ctx || !p_ch->connected) + if (NULL == p_ch || NULL == p_ch->ctx || !p_ch->connected) { return -1; } @@ -376,11 +386,11 @@ LOCAL int read_bits(stru_modbus_m_channel *p_ch, uint8_t slave, uint8_t fc, uint modbus_set_slave(p_ch->ctx, slave); int ret = -1; - if(fc == 0x01) + if (fc == 0x01) { ret = modbus_read_bits(p_ch->ctx, addr, count, p_dest); } - else if(fc == 0x02) + else if (fc == 0x02) { ret = modbus_read_input_bits(p_ch->ctx, addr, count, p_dest); } @@ -391,7 +401,7 @@ LOCAL int read_bits(stru_modbus_m_channel *p_ch, uint8_t slave, uint8_t fc, uint // 读取寄存器 (FC 0x03 或 0x04) LOCAL int read_registers(stru_modbus_m_channel *p_ch, uint8_t slave, uint8_t fc, uint16_t addr, uint16_t count, uint16_t *p_dest) { - if(NULL == p_ch || NULL == p_ch->ctx || !p_ch->connected) + if (NULL == p_ch || NULL == p_ch->ctx || !p_ch->connected) { return -1; } @@ -399,11 +409,11 @@ LOCAL int read_registers(stru_modbus_m_channel *p_ch, uint8_t slave, uint8_t fc, modbus_set_slave(p_ch->ctx, slave); int ret = -1; - if(fc == 0x03) + if (fc == 0x03) { ret = modbus_read_registers(p_ch->ctx, addr, count, p_dest); } - else if(fc == 0x04) + else if (fc == 0x04) { ret = modbus_read_input_registers(p_ch->ctx, addr, count, p_dest); } @@ -415,7 +425,7 @@ LOCAL int read_registers(stru_modbus_m_channel *p_ch, uint8_t slave, uint8_t fc, // 读取单个 Item 值(按 fc 分发) LOCAL int read_item_value(stru_modbus_m_channel *p_ch, stru_modbus_m_item *p_item) { - if(NULL == p_ch || NULL == p_item) + if (NULL == p_ch || NULL == p_item) { return -1; } @@ -423,11 +433,11 @@ LOCAL int read_item_value(stru_modbus_m_channel *p_ch, stru_modbus_m_item *p_ite uint8_t local_type = modbus_type_to_local_type(p_item->type); // FC 0x01/0x02: 读线圈/离散输入(位) - if(p_item->fc == 0x01 || p_item->fc == 0x02) + if (p_item->fc == 0x01 || p_item->fc == 0x02) { uint8_t bit_val = 0; int ret = read_bits(p_ch, p_item->slave, p_item->fc, p_item->addr, p_item->count, &bit_val); - if(ret > 0) + if (ret > 0) { uint8_t val = bit_val ? 1 : 0; memcpy(p_item->p_val, &val, 1); @@ -436,20 +446,20 @@ LOCAL int read_item_value(stru_modbus_m_channel *p_ch, stru_modbus_m_item *p_ite } // FC 0x03/0x04: 读保持/输入寄存器 - if(p_item->fc == 0x03 || p_item->fc == 0x04) + if (p_item->fc == 0x03 || p_item->fc == 0x04) { // 临时缓冲区(最大 125 个寄存器) uint16_t reg_buf[256]; uint16_t read_cnt = p_item->count > 125 ? 125 : p_item->count; int ret = read_registers(p_ch, p_item->slave, p_item->fc, p_item->addr, read_cnt, reg_buf); - if(ret <= 0) + if (ret <= 0) { return ret; } // type=2: 从寄存器提取位(FC03/04 读线圈模式) - if(p_item->type == 2 && p_item->bit < 16) + if (p_item->type == 2 && p_item->bit < 16) { uint8_t val = (reg_buf[0] >> p_item->bit) & 0x01; memcpy(p_item->p_val, &val, 1); @@ -457,10 +467,10 @@ LOCAL int read_item_value(stru_modbus_m_channel *p_ch, stru_modbus_m_item *p_ite } // type=4 (uint16) - if(p_item->type == 4) + if (p_item->type == 4) { uint16_t val = reg_buf[0]; - if(p_item->factor != 1.0f || p_item->offset != 0.0f) + if (p_item->factor != 1.0f || p_item->offset != 0.0f) { float fval = val * p_item->factor + p_item->offset; val = (uint16_t)fval; @@ -470,10 +480,10 @@ LOCAL int read_item_value(stru_modbus_m_channel *p_ch, stru_modbus_m_item *p_ite } // type=6 (float32): 2 个连续寄存器 - if(p_item->type == 6 && read_cnt >= 2) + if (p_item->type == 6 && read_cnt >= 2) { float val = modbus_get_float(®_buf[0]); - if(p_item->factor != 1.0f || p_item->offset != 0.0f) + if (p_item->factor != 1.0f || p_item->offset != 0.0f) { val = val * p_item->factor + p_item->offset; } @@ -482,7 +492,7 @@ LOCAL int read_item_value(stru_modbus_m_channel *p_ch, stru_modbus_m_item *p_ite } // type=7 (uint32): 2 个连续寄存器 - if(p_item->type == 7 && read_cnt >= 2) + if (p_item->type == 7 && read_cnt >= 2) { uint32_t val = ((uint32_t)reg_buf[0] << 16) | reg_buf[1]; memcpy(p_item->p_val, &val, 4); @@ -490,10 +500,10 @@ LOCAL int read_item_value(stru_modbus_m_channel *p_ch, stru_modbus_m_item *p_ite } // type=13 (string) - if(p_item->type == 13) + if (p_item->type == 13) { char *p_str = (char *)p_item->p_val; - for(uint16_t i = 0; i < read_cnt && i < 64; i++) + for (uint16_t i = 0; i < read_cnt && i < 64; i++) { p_str[i * 2] = (reg_buf[i] >> 8) & 0xFF; p_str[i * 2 + 1] = reg_buf[i] & 0xFF; @@ -526,29 +536,29 @@ typedef struct LOCAL void build_poll_groups(std::vector &items, std::vector &groups) { - for(auto &item : items) + for (auto &item : items) { // 只处理读类型的 fc - if(item.fc != 0x01 && item.fc != 0x02 && item.fc != 0x03 && item.fc != 0x04) + if (item.fc != 0x01 && item.fc != 0x02 && item.fc != 0x03 && item.fc != 0x04) { continue; } // 查找已有组 bool found = false; - for(auto &g : groups) + for (auto &g : groups) { - if(g.channel == item.channel && g.slave == item.slave && g.fc == item.fc) + if (g.channel == item.channel && g.slave == item.slave && g.fc == item.fc) { g.items.push_back(&item); - if(item.addr < g.min_addr) g.min_addr = item.addr; - if(item.addr + item.count > g.max_addr) g.max_addr = item.addr + item.count; + if (item.addr < g.min_addr) g.min_addr = item.addr; + if (item.addr + item.count > g.max_addr) g.max_addr = item.addr + item.count; found = true; break; } } - if(!found) + if (!found) { stru_poll_group g; g.channel = item.channel; @@ -564,19 +574,19 @@ LOCAL void build_poll_groups(std::vector &items, std::vector LOCAL void poll_groups(std::vector &groups) { - for(auto &g : groups) + for (auto &g : groups) { // 查找通道 stru_modbus_m_channel *p_ch = NULL; - for(auto &ch : g_modbus_m_cfg.channels) + for (auto &ch : g_modbus_m_cfg.channels) { - if(ch.id == g.channel && ch.connected) + if (ch.id == g.channel && ch.connected) { p_ch = &ch; break; } } - if(NULL == p_ch) + if (NULL == p_ch) { continue; } @@ -584,13 +594,13 @@ LOCAL void poll_groups(std::vector &groups) uint16_t total_count = g.max_addr - g.min_addr; // FC 01/02: 读位 - if(g.fc == 0x01 || g.fc == 0x02) + if (g.fc == 0x01 || g.fc == 0x02) { uint8_t bits[256] = {0}; int ret = read_bits(p_ch, g.slave, g.fc, g.min_addr, total_count > 2000 ? 2000 : total_count, bits); - if(ret > 0) + if (ret > 0) { - for(auto *p_item : g.items) + for (auto *p_item : g.items) { uint16_t offset = p_item->addr - g.min_addr; uint8_t val = bits[offset] ? 1 : 0; @@ -601,44 +611,44 @@ LOCAL void poll_groups(std::vector &groups) } // FC 03/04: 读寄存器 - if(g.fc == 0x03 || g.fc == 0x04) + if (g.fc == 0x03 || g.fc == 0x04) { uint16_t regs[256] = {0}; int ret = read_registers(p_ch, g.slave, g.fc, g.min_addr, total_count > 125 ? 125 : total_count, regs); - if(ret > 0) + if (ret > 0) { - for(auto *p_item : g.items) + for (auto *p_item : g.items) { uint16_t offset = (p_item->addr - g.min_addr); - if(p_item->type == 2) + if (p_item->type == 2) { // FC03/04 线圈模式:从寄存器提取位 uint16_t reg_idx = offset; uint8_t val = (regs[reg_idx] >> p_item->bit) & 0x01; memcpy(p_item->p_val, &val, 1); } - else if(p_item->type == 4) + else if (p_item->type == 4) { uint16_t val = regs[offset]; - if(p_item->factor != 1.0f || p_item->offset != 0.0f) + if (p_item->factor != 1.0f || p_item->offset != 0.0f) { float fval = val * p_item->factor + p_item->offset; val = (uint16_t)fval; } memcpy(p_item->p_val, &val, 2); } - else if(p_item->type == 6 && offset + 1 < 256) + else if (p_item->type == 6 && offset + 1 < 256) { // float val = modbus_get_float(®s[offset]); float val = modbus_get_float_abcd(®s[offset]); - if(p_item->factor != 1.0f || p_item->offset != 0.0f) + if (p_item->factor != 1.0f || p_item->offset != 0.0f) { val = val * p_item->factor + p_item->offset; } memcpy(p_item->p_val, &val, 4); } - else if(p_item->type == 7 && offset + 1 < 256) + else if (p_item->type == 7 && offset + 1 < 256) { uint32_t val = ((uint32_t)regs[offset] << 16) | regs[offset + 1]; memcpy(p_item->p_val, &val, 4); @@ -662,19 +672,19 @@ LOCAL void process_write_queue() queue.swap(g_write_queue); } - for(auto &req : queue) + for (auto &req : queue) { // 查找通道 stru_modbus_m_channel *p_ch = NULL; - for(auto &ch : g_modbus_m_cfg.channels) + for (auto &ch : g_modbus_m_cfg.channels) { - if(ch.id == req.channel && ch.ctx != NULL) + if (ch.id == req.channel && ch.ctx != NULL) { p_ch = &ch; break; } } - if(NULL == p_ch) + if (NULL == p_ch) { LOG_E("write: channel %d not found", req.channel); continue; @@ -683,7 +693,7 @@ LOCAL void process_write_queue() modbus_set_slave(p_ch->ctx, req.slave); int ret = -1; - if(req.is_coil) + if (req.is_coil) { ret = modbus_write_bit(p_ch->ctx, req.addr, req.value ? 1 : 0); } @@ -692,7 +702,7 @@ LOCAL void process_write_queue() ret = modbus_write_register(p_ch->ctx, req.addr, req.value); } - if(ret < 0) + if (ret < 0) { LOG_E("write failed: ch=%d, slave=%d, fc=0x%02x, addr=%d", req.channel, req.slave, req.fc, req.addr); } @@ -710,14 +720,14 @@ LOCAL void enqueue_write(const stru_modbus_m_write_req &req) LOCAL void modbus_m_signal_co_change_callback(const char *saddr, dc_ctrl_step_t step, uint8_t data_type, uint8_t setting_zone, const void *p_data) { - if(step != DC_CTRL_DIRECT) + if (step != DC_CTRL_DIRECT) { return; } - for(auto &item : g_modbus_m_cfg.point.co_vec) + for (auto &item : g_modbus_m_cfg.point.co_vec) { - if(item.saddr == saddr) + if (item.saddr == saddr) { stru_modbus_m_write_req req; req.channel = item.channel; @@ -726,7 +736,7 @@ LOCAL void modbus_m_signal_co_change_callback(const char *saddr, dc_ctrl_step_t req.addr = item.addr; req.is_coil = true; - if(data_type == DATA_TYPE_B) + if (data_type == DATA_TYPE_B) { req.value = p_data ? (*(uint8_t *)p_data ? 1 : 0) : 0; } @@ -736,7 +746,7 @@ LOCAL void modbus_m_signal_co_change_callback(const char *saddr, dc_ctrl_step_t } enqueue_write(req); - MY_LOG_I("co write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value); + LOG_I("co write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value); break; } } @@ -744,14 +754,14 @@ LOCAL void modbus_m_signal_co_change_callback(const char *saddr, dc_ctrl_step_t LOCAL void modbus_m_signal_ao_change_callback(const char *saddr, dc_ctrl_step_t step, uint8_t data_type, uint8_t setting_zone, const void *p_data) { - if(step != DC_CTRL_DIRECT) + if (step != DC_CTRL_DIRECT) { return; } - for(auto &item : g_modbus_m_cfg.point.ao_vec) + for (auto &item : g_modbus_m_cfg.point.ao_vec) { - if(item.saddr == saddr) + if (item.saddr == saddr) { stru_modbus_m_write_req req; req.channel = item.channel; @@ -760,11 +770,11 @@ LOCAL void modbus_m_signal_ao_change_callback(const char *saddr, dc_ctrl_step_t req.addr = item.addr; req.is_coil = false; - if(data_type == DATA_TYPE_U16) + if (data_type == DATA_TYPE_U16) { req.value = p_data ? *(uint16_t *)p_data : 0; } - else if(data_type == DATA_TYPE_F32) + else if (data_type == DATA_TYPE_F32) { float f = p_data ? *(float *)p_data : 0; req.value = (uint16_t)f; @@ -775,7 +785,7 @@ LOCAL void modbus_m_signal_ao_change_callback(const char *saddr, dc_ctrl_step_t } enqueue_write(req); - MY_LOG_I("ao write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value); + LOG_I("ao write: ch=%d, slave=%d, addr=%d, val=%d", req.channel, req.slave, req.addr, req.value); break; } } @@ -786,39 +796,39 @@ LOCAL void modbus_m_signal_ao_change_callback(const char *saddr, dc_ctrl_step_t LOCAL int signal_register_items(const std::vector &items, const std::string &signal_type) { - for(auto &item : items) + for (auto &item : items) { uint8_t local_type = modbus_type_to_local_type(item.type); - if(signal_type == "st") + if (signal_type == "st") { - if(0 != dc_signal_out(item.saddr.c_str(), item.desc.c_str(), local_type, item.p_val, MODULE_MODBUS_M)) + if (0 != dc_signal_out(item.saddr.c_str(), item.desc.c_str(), local_type, item.p_val, MODULE_MODBUS_M)) { - MY_LOG_E("dc_signal_out failed: %s", item.saddr.c_str()); + LOG_E("dc_signal_out failed: %s", item.saddr.c_str()); return -1; } } - else if(signal_type == "mx") + else if (signal_type == "mx") { - if(0 != dc_signal_out(item.saddr.c_str(), item.desc.c_str(), local_type, item.p_val, MODULE_MODBUS_M)) + if (0 != dc_signal_out(item.saddr.c_str(), item.desc.c_str(), local_type, item.p_val, MODULE_MODBUS_M)) { - MY_LOG_E("dc_signal_out failed: %s", item.saddr.c_str()); + LOG_E("dc_signal_out failed: %s", item.saddr.c_str()); return -1; } } - else if(signal_type == "co") + else if (signal_type == "co") { - if(0 != dc_signal_yk(item.saddr.c_str(), item.desc.c_str(), local_type, DC_CTRL_DIRECT, item.p_val, modbus_m_signal_co_change_callback, MODULE_MODBUS_M)) + if (0 != dc_signal_yk(item.saddr.c_str(), item.desc.c_str(), local_type, DC_CTRL_DIRECT, item.p_val, modbus_m_signal_co_change_callback, MODULE_MODBUS_M)) { - MY_LOG_E("dc_signal_yk failed: %s", item.saddr.c_str()); + LOG_E("dc_signal_yk failed: %s", item.saddr.c_str()); return -1; } } - else if(signal_type == "ao") + else if (signal_type == "ao") { - if(0 != dc_signal_ao(item.saddr.c_str(), item.desc.c_str(), local_type, DC_CTRL_DIRECT, item.p_val, modbus_m_signal_ao_change_callback, MODULE_MODBUS_M)) + if (0 != dc_signal_ao(item.saddr.c_str(), item.desc.c_str(), local_type, DC_CTRL_DIRECT, item.p_val, modbus_m_signal_ao_change_callback, MODULE_MODBUS_M)) { - MY_LOG_E("dc_signal_ao failed: %s", item.saddr.c_str()); + LOG_E("dc_signal_ao failed: %s", item.saddr.c_str()); return -1; } } @@ -829,24 +839,24 @@ LOCAL int signal_register_items(const std::vector &items, co LOCAL int signals_init() { - if(0 != signal_register_items(g_modbus_m_cfg.point.st_vec, "st")) + if (0 != signal_register_items(g_modbus_m_cfg.point.st_vec, "st")) { return -1; } - if(0 != signal_register_items(g_modbus_m_cfg.point.mx_vec, "mx")) + if (0 != signal_register_items(g_modbus_m_cfg.point.mx_vec, "mx")) { return -1; } - if(0 != signal_register_items(g_modbus_m_cfg.point.co_vec, "co")) + if (0 != signal_register_items(g_modbus_m_cfg.point.co_vec, "co")) { return -1; } - if(0 != signal_register_items(g_modbus_m_cfg.point.ao_vec, "ao")) + if (0 != signal_register_items(g_modbus_m_cfg.point.ao_vec, "ao")) { return -1; } - MY_LOG_I("signals registered: st=%zu, mx=%zu, co=%zu, ao=%zu", + LOG_I("signals registered: st=%zu, mx=%zu, co=%zu, ao=%zu", g_modbus_m_cfg.point.st_vec.size(), g_modbus_m_cfg.point.mx_vec.size(), g_modbus_m_cfg.point.co_vec.size(), @@ -860,15 +870,15 @@ LOCAL int signals_init() int modbus_m_init() { - if(0 != signals_init()) + if (0 != signals_init()) { - MY_LOG_E("signals_init failed"); + LOG_E("signals_init failed"); return -1; } - if(0 != channels_init()) + if (0 != channels_init()) { - MY_LOG_E("channels_init failed"); + LOG_E("channels_init failed"); // 部分通道失败也可继续 } @@ -882,10 +892,10 @@ void modbus_m_show_info() { printf("===== Modbus Master Info =====\n"); printf("Channels: %zu\n", g_modbus_m_cfg.channels.size()); - for(auto &ch : g_modbus_m_cfg.channels) + for (auto &ch : g_modbus_m_cfg.channels) { printf(" ch[%d]: mode=%s, connected=%d", ch.id, ch.mode.c_str(), ch.connected); - if(ch.mode == "tcp") + if (ch.mode == "tcp") { printf(", %s:%d", ch.host.c_str(), ch.port); } @@ -910,13 +920,13 @@ LOCAL void cmd_modbus_m(int argc, char *argv[]) const char *str = "\ modbus_m info ----------> 查看所有通道和点表信息\n"; - if(argc < 2) + if (argc < 2) { printf("%s", str); return; } - if(0 == strcmp(argv[1], "info")) + if (0 == strcmp(argv[1], "info")) { modbus_m_show_info(); } @@ -940,21 +950,21 @@ int app_modbus_m_init1(void *arg) dc_signal_out("modbus_m.run_cnt", "modbus_m.run_cnt", DATA_TYPE_U32, &p_app->run_cnt, MODULE_MODBUS_M); std::string base_path = get_base_path(); - if(base_path.empty()) + if (base_path.empty()) { - MY_LOG_E("get_base_path failed"); + LOG_E("get_base_path failed"); return -1; } g_modbus_m_base_path = base_path + "config/MODBUS/"; std::string cfg_path = g_modbus_m_base_path + "modbus_m.xml"; - if(0 != modbus_m_cfg_parse(cfg_path)) + if (0 != modbus_m_cfg_parse(cfg_path)) { - MY_LOG_E("modbus_m_cfg_parse failed: %s", cfg_path.c_str()); + LOG_E("modbus_m_cfg_parse failed: %s", cfg_path.c_str()); return -1; } - if(0 != modbus_m_init()) + if (0 != modbus_m_init()) { LOG_E("modbus_m_init failed"); return -1; @@ -970,7 +980,7 @@ int app_modbus_m_init2(void *arg) void *app_modbus_m(void *arg) { - if(NULL == arg) + if (NULL == arg) { LOG_E("app_modbus_m arg null"); return NULL; @@ -985,9 +995,9 @@ void *app_modbus_m(void *arg) // 预构建轮询分组 build_poll_groups(g_modbus_m_cfg.point.st_vec, st_groups); build_poll_groups(g_modbus_m_cfg.point.mx_vec, mx_groups); - MY_LOG_I("poll groups: st=%zu, mx=%zu", st_groups.size(), mx_groups.size()); + LOG_I("poll groups: st=%zu, mx=%zu", st_groups.size(), mx_groups.size()); - while(1) + while (1) { task_event_recv(p_app->p_event, 0x01 | 0x02 | 0x04,