diff --git a/comm/loopback.c b/comm/loopback.c index 512c1bf..088660e 100644 --- a/comm/loopback.c +++ b/comm/loopback.c @@ -269,13 +269,7 @@ void to_big_endian_24(uint32_t value, uint8_t *dest) { } // ============== SLMP (三菱SLMP协议) 完整整合 ============== - -// SLMP状态机枚举 -typedef enum { - SLMP_STATE_WRITE_RX_DONE, // 写响应已接收,准备发送下一次读 - SLMP_STATE_READ_RX_DONE, // 读响应已接收,准备发送下一次写 - SLMP_STATE_READY_SEND // 准备发送 -} SLMPSTATUS; +// 完全按照FINS相同架构编写 // 24位小端存储结构体 (SLMP使用) typedef union { @@ -305,9 +299,14 @@ typedef union { uint8_t frameU8[21]; // 帧头最大长度21字节 } SLMPFrame; -// SLMP全局状态 -SLMPSTATUS slmp_master_status = SLMP_STATE_WRITE_RX_DONE; -uint8_t slmp_read_resp_header[7] = { 0xD0, 0x00, 0x00, 0xFF, 0xFF, 0x03, 0x00 }; +// SLMP全局变量 - 和FINS相同风格 +uint8_t slmp_txbuf[1024]; // 发送缓冲区 +uint8_t slmp_rxbuf[1024]; // 接收缓冲区 +uint16_t slmp_read_resp_header[7] = { 0xD0, 0x00, 0x00, 0xFF, 0xFF, 0x03, 0x00 }; +uint16_t slmp_idx = 0; // 轮询索引 (0~9对应readST[10], 奇数读 偶数写,和FINS一致) +uint16_t slmp_curr_idx; // 当前处理的索引 +uint16_t slmp_addr; // 当前读写地址 +uint16_t slmp_size; // 当前读写点数 static SLMPFrame slmp_frame = { .frameStruct.header = 0x0050, .frameStruct.target = 0xff00, @@ -318,17 +317,6 @@ static SLMPFrame slmp_frame = { .frameStruct.dm = 0xA8 }; -uint8_t slmp_txbuf[1024]; // 发送缓冲区 -uint8_t slmp_rxbuf[1024]; // 接收缓冲区 -uint16_t slmp_read_addr; // 当前读起始地址 -uint16_t slmp_read_size; // 当前读点数 -uint16_t slmp_write_addr; // 当前写起始地址 -uint16_t slmp_write_size; // 当前写点数 -uint16_t slmp_read_idx = 0; // 读轮询索引 (0~9对应readST[10]) -uint16_t slmp_write_idx = 0;// 写轮询索引 (0~9对应writeST[10]) -uint16_t slmp_curr_read_idx;// 当前正在处理的读索引 -uint16_t slmp_curr_write_idx;// 当前正在处理的写索引 - // 32位转换为SLMP 24位小端 static slmp_uint24_t u32_to_slmp24(uint32_t val) { slmp_uint24_t r; @@ -338,182 +326,165 @@ static slmp_uint24_t u32_to_slmp24(uint32_t val) { return r; } -// 检查是否是合法的读响应 -static _Bool slmp_is_read_response(uint16_t recv_size) { - // 读响应长度 = 11字节头 + 每个寄存器2字节 - if (recv_size != (slmp_read_size * 2 + 11)) { +// 处理SLMP接收到的响应 +// 返回1: 处理成功,0: 不是有效响应 +_Bool slmp_handle_recv(uint16_t size) { + // 只有读响应需要处理数据,写响应不需要存储 + // 比对读响应头确认 + if (memcmp(slmp_rxbuf, slmp_read_resp_header, 7) != 0) { + return 0; // 不是读响应,直接忽略 + } + + // 找到上一次发送对应的索引 (发送后索引+1,响应对应前一个) + uint16_t prevIdx; + if (slmp_idx == 0) { + prevIdx = 9; + } else { + prevIdx = slmp_idx - 1; + } + + // 读使用奇数索引,验证一下 + if (prevIdx % 2 != 1) { + return 0; // 不匹配,丢弃 + } + + uint16_t thisSize = get_register(readST[prevIdx].size); + // 大小检查 + 边界保护 (最大20个寄存器) + if (thisSize > 20 || thisSize * 2 + 11 != size) { return 0; } - // 比对响应头确认是读响应 - return (memcmp(slmp_rxbuf, slmp_read_resp_header, 7) == 0); -} -// 解析读响应,存储数据到寄存器映射 -static void slmp_map_read_response(void) { - // SLMP数据使用小端序: 低字节在前,高字节在后 - for (uint8_t i = 0; i < slmp_read_size; i++) { - uint16_t value = slmp_rxbuf[i * 2 + 11] | (slmp_rxbuf[i * 2 + 12] << 8); + // SLMP使用小端序: 低字节在前,高字节在后 + for (int i = 0; i < thisSize; i++) { + uint8_t byte_l = slmp_rxbuf[i * 2 + 11]; + uint8_t byte_h = slmp_rxbuf[i * 2 + 12]; + uint16_t value = ((uint16_t)byte_l) | (((uint16_t)byte_h) << 8); // 存储位置: 1200 + (索引*20) + 偏移 - set_register(1200 + (slmp_curr_read_idx * 20) + i, value); + set_register(1200 + (20 * prevIdx) + i, value); } -} - -// 更新下一个要读的通道,检查参数合法性 -static _Bool slmp_update_read(void) { - slmp_curr_read_idx = slmp_read_idx; - uint16_t slave_id = get_register(readST[slmp_read_idx].slave_id); - uint16_t plc_addr = get_register(readST[slmp_read_idx].plc_address); - uint16_t size = get_register(readST[slmp_read_idx].size); - - // 索引轮转 - slmp_read_idx = (slmp_read_idx + 1) % 10; - - // 参数范围检查 - if (slave_id < 0x01 || slave_id > 0xF7 || size < 0x01 || size > 20) { - return 0; - } - if ((plc_addr + size) >= 8000) { - return 0; // 地址越界 - } - - slmp_read_addr = plc_addr; - slmp_read_size = size; return 1; } -// 更新下一个要写的通道,检查参数合法性 -static _Bool slmp_update_write(void) { - slmp_curr_write_idx = slmp_write_idx; - uint16_t slave_id = get_register(writeST[slmp_write_idx].slave_id); - uint16_t plc_addr = get_register(writeST[slmp_write_idx].plc_address); - uint16_t size = get_register(writeST[slmp_write_idx].size); +// 构建SLMP请求,返回发送长度 +uint16_t slmp_build_send(void) { + uint16_t sendsize = 0; + uint16_t slave_id; + uint16_t plc_address; + uint16_t size; - // 索引轮转 - slmp_write_idx = (slmp_write_idx + 1) % 10; + // 奇数索引 -> 读操作 (和FINS一致) + if (slmp_idx % 2 == 1) { + slave_id = get_register(readST[slmp_idx].slave_id); + plc_address = get_register(readST[slmp_idx].plc_address); + size = get_register(readST[slmp_idx].size); - // 参数范围检查 - if (slave_id < 0x01 || slave_id > 0xF7 || size < 0x01 || size > 20) { - return 0; - } - if ((plc_addr + size) >= 8000) { - return 0; // 地址越界 + if (slave_id < 0x01 || slave_id > 0xF7 || size == 0 || size > 20) { + sendsize = 0; + } else if ((plc_address + size) >= 8000) { + sendsize = 0; + } else { + // 构建读请求帧 + slmp_frame.frameStruct.cmd = 0x0401; // 读命令 + slmp_frame.frameStruct.requestSize = 0x000C; // 请求长度固定12字节 + slmp_frame.frameStruct.start = u32_to_slmp24(plc_address); + slmp_frame.frameStruct.size = size; + sendsize = 21; // 帧头长度固定21字节,无额外数据 + // 拷贝到发送缓冲区,带边界检查 + for (int i = 0; i < sendsize && i < sizeof(slmp_txbuf); i++) { + slmp_txbuf[i] = slmp_frame.frameU8[i]; + } + } + + } else { + // 偶数索引 -> 写操作 (和FINS一致) + slave_id = get_register(writeST[slmp_idx].slave_id); + plc_address = get_register(writeST[slmp_idx].plc_address); + size = get_register(writeST[slmp_idx].size); + + if (slave_id < 0x01 || slave_id > 0xF7 || size == 0 || size > 20) { + sendsize = 0; + } else if ((plc_address + size) >= 8000) { + sendsize = 0; + } else { + sendsize = 21 + (size * 2); // 帧头 + 数据 + // 检查缓冲区边界,防止越界 + if (sendsize > sizeof(slmp_txbuf)) { + return 0; + } + + // 构建写请求帧 + slmp_frame.frameStruct.cmd = 0x1401; // 写命令 + slmp_frame.frameStruct.requestSize = 0x000C + (size * 2); + slmp_frame.frameStruct.start = u32_to_slmp24(plc_address); + slmp_frame.frameStruct.size = size; + + // 填充数据,SLMP小端序: 低字节在前 + for (int i = 0; i < size; i++) { + uint16_t value = get_register(1000 + (20 * slmp_idx) + i); + slmp_txbuf[i * 2 + 21] = value & 0xFF; // 低字节在前 + slmp_txbuf[i * 2 + 22] = (value >> 8) & 0xFF; // 高字节在后 + } + + // 拷贝帧头到发送缓冲区 + for (int i = 0; i < 21 && i < sizeof(slmp_txbuf); i++) { + slmp_txbuf[i] = slmp_frame.frameU8[i]; + } + } } - slmp_write_addr = plc_addr; - slmp_write_size = size; - return 1; + // 索引轮转 (0~9对应10个通道,和FINS轮询方式一致) + slmp_idx = (slmp_idx + 1) % 10; + return sendsize; } -// 构建SLMP读请求 -static uint16_t slmp_build_read_request(void) { - slmp_frame.frameStruct.cmd = 0x0401; // 读命令 - slmp_frame.frameStruct.requestSize = 0x000C; // 请求长度固定12字节 - slmp_frame.frameStruct.start = u32_to_slmp24(slmp_read_addr); // 起始地址 - slmp_frame.frameStruct.size = slmp_read_size; // 读点数 - return 21; // 帧头长度固定21字节,无额外数据 -} - -// 构建SLMP写请求 -static uint16_t slmp_build_write_request(void) { - uint16_t send_size = 21 + (slmp_write_size * 2); // 帧头 + 数据 - // 检查发送缓冲区边界 - if (send_size > sizeof(slmp_txbuf)) { - return 0; - } - - slmp_frame.frameStruct.cmd = 0x1401; // 写命令 - slmp_frame.frameStruct.requestSize = 0x000C + (slmp_write_size * 2); - slmp_frame.frameStruct.start = u32_to_slmp24(slmp_write_addr); - slmp_frame.frameStruct.size = slmp_write_size; - - // 拷贝数据到发送缓冲区,SLMP小端序: 低字节在前 - for (uint8_t i = 0; i < slmp_write_size; i++) { - uint16_t val = get_register(1000 + (slmp_curr_write_idx * 20) + i); - slmp_txbuf[i * 2 + 21] = val & 0xFF; // 低字节在前 - slmp_txbuf[i * 2 + 22] = (val >> 8) & 0xFF; // 高字节在后 - } - return send_size; -} - -// SLMP超时计数,由主循环调用 +// SLMP超时计数,由主循环调用 (和FINS一致) void update_timeout_slmp(void) { if (slmpTimegap < 0xffff) { slmpTimegap++; } } -// SLMP TCP客户端主函数 +// SLMP TCP客户端主函数 (和FINS相同结构) int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport) { - int32_t ret = 0; + int32_t ret; + uint16_t size = 0, sentsize = 0; static uint16_t any_port = 50000; - uint8_t sr_status = getSn_SR(sn); - switch (sr_status) { + switch (getSn_SR(sn)) { case SOCK_ESTABLISHED: if (getSn_IR(sn) & Sn_IR_CON) { setSn_IR(sn, Sn_IR_CON); } // 接收响应 - if (slmp_master_status == SLMP_STATE_READY_SEND) { - uint16_t clientrxsize = getSn_RX_RSR(sn); - if (clientrxsize > 0) { - // 边界保护 - if (clientrxsize > sizeof(slmp_rxbuf)) { - clientrxsize = sizeof(slmp_rxbuf); - } - ret = recv(sn, slmp_rxbuf, clientrxsize); - if (ret > 0) { - comm_count++; - if (slmp_is_read_response(ret)) { - // 是读响应,解析存储 - slmp_map_read_response(); - slmp_master_status = SLMP_STATE_READ_RX_DONE; - } else { - // 写响应,不需要解析数据 - slmp_master_status = SLMP_STATE_WRITE_RX_DONE; - } - } + if ((size = getSn_RX_RSR(sn)) > 0) { + // 边界保护,不超过缓冲区大小 + if (size > sizeof(slmp_rxbuf)) { + size = sizeof(slmp_rxbuf); + } + ret = recv(sn, slmp_rxbuf, size); + if (ret <= 0) { + return ret; + } else { + comm_count++; + slmp_handle_recv(ret); + slmpTimegap = 900; // 重置超时 } } - // 间隔到了,准备下一次发送 - uint16_t sendbufsize = 0; - if (slmpTimegap > 1000 || slmp_master_status != SLMP_STATE_READY_SEND) { - slmpTimegap = 0; - switch (slmp_master_status) { - case SLMP_STATE_WRITE_RX_DONE: - // 写完了,准备读 - if (slmp_update_read()) { - slmp_master_status = SLMP_STATE_READY_SEND; - if (slmp_read_size <= 20) { - sendbufsize = slmp_build_read_request(); - memcpy(slmp_txbuf, slmp_frame.frameU8, 21); - } - } - break; - - case SLMP_STATE_READ_RX_DONE: - // 读完了,准备写 - if (slmp_update_write()) { - slmp_master_status = SLMP_STATE_READY_SEND; - if (slmp_write_size <= 20) { - sendbufsize = slmp_build_write_request(); - memcpy(slmp_txbuf, slmp_frame.frameU8, 21); - } - } - break; - - default: - slmp_master_status = SLMP_STATE_WRITE_RX_DONE; - break; + // 超时到了,发送下一个请求 + size = 0; + if (slmpTimegap > 1000) { + size = slmp_build_send(); + if (size == 0) { + slmpTimegap = 1000; } } - // 发送报文 - uint16_t sentsize = 0; - while (sendbufsize != 0 && sentsize != sendbufsize) { - ret = send(sn, slmp_txbuf + sentsize, sendbufsize - sentsize); + // 发送请求 + sentsize = 0; + while (size != 0 && sentsize != size) { + ret = send(sn, slmp_txbuf + sentsize, size - sentsize); if (ret < 0) { close(sn); return ret; @@ -536,7 +507,7 @@ int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport) { case SOCK_CLOSED: close(sn); - // 端口绕回处理 + // 端口绕回处理 (和FINS一致) if (any_port == 0xffff) { any_port = 50000; } @@ -548,9 +519,8 @@ int32_t loopback_tcpc_slmp(uint8_t sn, uint8_t *destip, uint16_t destport) { default: break; } - return 1; -} +} ///////////////// OMR FINS (欧姆龙FINS UDP通信) start diff --git a/comm/loopback.h b/comm/loopback.h index a3ed1d8..35d3432 100644 --- a/comm/loopback.h +++ b/comm/loopback.h @@ -34,20 +34,12 @@ typedef struct { uint16_t size; } RtuRxST; -// SLMP状态枚举 (对外声明) -typedef enum { - SLMP_STATE_WRITE_RX_DONE, - SLMP_STATE_READ_RX_DONE, - SLMP_STATE_READY_SEND -} SLMPSTATUS; - -extern SLMPSTATUS slmp_master_status; +// SLMP全局变量 (和FINS相同风格) extern uint8_t slmp_txbuf[1024]; extern uint8_t slmp_rxbuf[1024]; -extern uint16_t slmp_read_addr; -extern uint16_t slmp_read_size; -extern uint16_t slmp_write_addr; -extern uint16_t slmp_write_size; +extern uint16_t slmp_idx; +extern uint16_t slmp_addr; +extern uint16_t slmp_size; /* Modbus TCP 服务 */ int32_t loopback_tcps_modbus(uint8_t sn, uint16_t port);