/* * finsudpmaster.c * 欧姆龙FINS UDP通信主站 * 轮询方式: myRtumSTIdx 0~19轮转, 奇数索引读, 偶数索引写 */ #include "finsudpmaster.h" #include "socket.h" #include "wizchip_conf.h" #include "rtuMaster.h" #include "main.h" #include uint8_t myRtumSTIdx = 0; // 轮询索引 (0~19对应myRtumST[20]) uint8_t omrfins_buf[1024]; // FINS收发缓冲区 // 读响应固定头,只有匹配这个头才会处理 uint8_t readrx[14] = { 0xc0, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x40 }; // 处理FINS读响应 // 返回1: 处理成功,0: 不是有效响应 _Bool handleReadRecv(uint16_t size, uint16_t local_node) { // 设置本地节点号 readrx[4] = (uint8_t)local_node; // 对比前14字节,确认是读响应 for (int i = 0; i < 14; i++) { if (readrx[i] != omrfins_buf[i]) { return 0; } } // 找到上一次发送对应的索引 (因为发送后索引+1,所以响应对应前一个) uint16_t prevIdx; if (myRtumSTIdx == 0) { prevIdx = 19; } else { prevIdx = myRtumSTIdx - 1; } // 只有读响应会走到这里,读使用奇数索引,验证一下 if ((prevIdx % 2 == 1) != (memcmp(readrx, readrx, 7) == 0)) { return 0; // 不匹配,丢弃 } uint16_t thisSize = get_register(myRtumST[prevIdx].size); // 大小检查 + 边界保护 (最大20个寄存器) if (thisSize > 20 || thisSize * 2 + 14 != size) { return 0; } // FINS使用大端序: 高字节在前,低字节在后 for (int i = 0; i < thisSize; i++) { uint8_t byte_h = omrfins_buf[i * 2 + 14]; uint8_t byte_l = omrfins_buf[i * 2 + 15]; uint16_t value = ((uint16_t)byte_h << 8) | byte_l; // 存储位置: 1400 + (索引*20) + 偏移 set_register(1400 + (20 * prevIdx) + i, value); } return 1; } // 预定义的读/写请求模板 uint8_t send_read[18] = { 0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x01, 0x01, 0x82, 0x00, 0x64, 0x00, 0x00, 0x0a }; uint8_t write_send[58] = { 0x80, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x01, 0x02, 0x82, 0x00, 0x64, 0x00, 0x00, 0x02 }; // 构建FINS请求帧,返回发送长度 uint16_t handleSend(uint16_t local_node) { uint16_t sendsize = 0; uint16_t slave_id = get_register(myRtumST[myRtumSTIdx].slave_id); uint16_t plc_address = get_register(myRtumST[myRtumSTIdx].plc_address); uint16_t size = get_register(myRtumST[myRtumSTIdx].size); // 参数合法性检查 if (slave_id == 0 || slave_id >= 127 || size == 0 || size > 20) { sendsize = 0; } else { if (myRtumSTIdx % 2 == 1) { // 奇数索引 -> 读操作 send_read[7] = (uint8_t)local_node; send_read[13] = (plc_address >> 8) & 0xFF; send_read[14] = plc_address & 0xFF; send_read[17] = (uint8_t)size; sendsize = 18; // 拷贝到发送缓冲区,带边界检查 for (int i = 0; i < sendsize && i < sizeof(omrfins_buf); i++) { omrfins_buf[i] = send_read[i]; } } else { // 偶数索引 -> 写操作 sendsize = (size * 2) + 18; // 检查缓冲区边界,防止越界 if (sendsize > sizeof(write_send) || sendsize > sizeof(omrfins_buf)) { return 0; } write_send[7] = (uint8_t)local_node; write_send[13] = (plc_address >> 8) & 0xFF; write_send[14] = plc_address & 0xFF; write_send[17] = (uint8_t)size; // 填充数据,FINS大端序: 高字节在前 for (int i = 0; i < size; i++) { uint16_t value = get_register(1400 + (20 * myRtumSTIdx) + i); write_send[i * 2 + 18] = (value >> 8) & 0xFF; write_send[i * 2 + 19] = value & 0xFF; } // 拷贝到发送缓冲区 for (int i = 0; i < sendsize; i++) { omrfins_buf[i] = write_send[i]; } } } // 索引轮转 myRtumSTIdx = (myRtumSTIdx + 1) % 20; return sendsize; } uint16_t fins_timeout = 0; // FINS超时计数,由主循环调用 void update_timeout_fins(void) { if (fins_timeout < 0xffff) { fins_timeout++; } } // FINS UDP客户端主函数 int32_t loopback_udpc_omrfins(uint8_t sn, uint8_t *destip, uint16_t destport, uint16_t local_node) { int32_t ret; uint16_t size = 0, sentsize = 0; static uint16_t any_port = 50000; switch (getSn_SR(sn)) { case SOCK_UDP: // 接收响应 if ((size = getSn_RX_RSR(sn)) > 0) { // 边界保护,不超过缓冲区大小 if (size > sizeof(omrfins_buf)) { size = sizeof(omrfins_buf); } ret = recvfrom(sn, omrfins_buf, size, destip, (uint16_t*) &destport); if (ret <= 0) { return ret; } else { comm_count++; handleReadRecv(ret, local_node); fins_timeout = 490; // 重置超时 } } // 超时到了,发送下一个请求 size = 0; if (fins_timeout > 500) { size = handleSend(local_node); if (size == 0) { fins_timeout = 1000; } } // 发送请求 sentsize = 0; while (size != 0 && sentsize != size) { ret = sendto(sn, omrfins_buf + sentsize, size - sentsize, destip, destport); if (ret < 0) { return ret; } sentsize += ret; } break; case SOCK_CLOSED: // 端口绕回处理 if (any_port == 0xffff) { any_port = 50000; } if ((ret = socket(sn, Sn_MR_UDP, any_port++, 0x00)) != sn) { return ret; } break; default: break; } return 1; }