From 4e80a72d242a1ee6a5b5a905bc5accefcbccd4a3 Mon Sep 17 00:00:00 2001 From: claw Date: Thu, 2 Apr 2026 23:15:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Es7tcpmaster=EF=BC=8C?= =?UTF-8?q?=E8=A5=BF=E9=97=A8=E5=AD=90S7Comm=20TCP=E4=B8=BB=E7=AB=99?= =?UTF-8?q?=EF=BC=8C=E9=9C=80=E8=A6=81=E4=B8=A4=E6=AC=A1=E5=8D=8F=E5=95=86?= =?UTF-8?q?=E5=90=8E=E8=BF=9B=E5=85=A5=E8=BD=AE=E8=AF=A2=EF=BC=8C=E6=9E=B6?= =?UTF-8?q?=E6=9E=84=E5=AF=B9=E9=BD=90FINS/SLMP=EF=BC=8C=E5=A5=87=E6=95=B0?= =?UTF-8?q?=E8=AF=BB=E5=81=B6=E6=95=B0=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comm/protocols/s7tcpmaster.c | 438 +++++++++++++++++++++++++++++++++++ comm/protocols/s7tcpmaster.h | 65 ++++++ 2 files changed, 503 insertions(+) create mode 100644 comm/protocols/s7tcpmaster.c create mode 100644 comm/protocols/s7tcpmaster.h diff --git a/comm/protocols/s7tcpmaster.c b/comm/protocols/s7tcpmaster.c new file mode 100644 index 0000000..2cc538d --- /dev/null +++ b/comm/protocols/s7tcpmaster.c @@ -0,0 +1,438 @@ +/* + * @file s7tcpmaster.c + * @brief 西门子S7Comm TCP客户端主站实现 + * + * 轮询机制: 和SLMP/FINS相同,使用myRtumST[20] + * - s7_idx: 0~19 轮转索引,对应 myRtumST[20] + * - **奇数索引**: 读操作,从PLC读取数据存储到 1000 + (idx*20) + i + * - **偶数索引**: 写操作,从 1000 + (idx*20) + i 读取数据写入PLC + * - 需要先建立TCP连接,然后完成COTP连接协商和S7Comm参数协商 + * - 协商完成后进入正常轮询读写 + */ + +#include "s7tcpmaster.h" +#include "socket.h" +#include "wizchip_conf.h" +#include "rtuMaster.h" +#include "main.h" +#include + +// ============================================================================ +// 结构体定义 +// ============================================================================ + +// 24位大端存储 +typedef union { + uint8_t bytes[3]; + struct { + uint8_t byte0; // 最高字节 + uint8_t byte1; // 中间字节 + uint8_t byte2; // 最低字节 + }; +} s7_uint24_t; + +// S7帧读写参数结构体 +typedef union { + uint8_t frame[31]; + struct { + uint8_t padding1[23]; + uint8_t read_size[2]; + uint8_t db_id[2]; + uint8_t padding2[1]; + s7_uint24_t start_address; + }; +} s7ReadFrame; + +typedef union { + uint8_t frame[1024]; + struct { + uint8_t padding1[2]; + uint8_t frame_size[2]; + uint8_t padding2[11]; + uint8_t dataframe_size[2]; + uint8_t padding3[6]; + uint8_t data_size[2]; + uint8_t db_id[2]; + uint8_t padding4[1]; + s7_uint24_t start_address; + uint8_t padding5[2]; + uint8_t databyte_size_t8[2]; + uint8_t data[800]; + }; +} s7WriteFrame; + +// ============================================================================ +// 全局变量 +// ============================================================================ + +uint8_t s7_txbuf[1024]; // 发送缓冲区 +uint8_t s7_rxbuf[1024]; // 接收缓冲区 +uint16_t s7_idx = 0; // 轮询索引 (0~19,和FINS/SLMP一致) +uint16_t s7_curr_idx; // 当前处理的索引 +uint16_t s7_addr; // 当前读写地址 +uint16_t s7_size; // 当前读写点数 +uint16_t s7Timegap = 0; // 超时计数 +uint8_t s7_rx_done = 1; +uint8_t s7_comm_step = 0; // 协商步骤: 0=发送connect, 1=发送comm-set, 2=读写轮询 +uint8_t s7_read_write_flag = 0; // 0=读,1=写 + +// 预定义的协商报文 +uint8_t s7_comm1[] = { 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x00, 0xC0, 0x01, 0x0A, 0xC1, 0x02, 0x01, 0x00, 0xC2, 0x02, 0x03, 0x02 }; +uint8_t s7_comm2[] = { 0x03, 0x00, 0x00, 0x19, 0x02, 0xF0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0xE0 }; +uint8_t s7_read[] = { 0x03, 0x00, 0x00, 0x1f, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0A, 0x10, 0x02, 0x00, 0x02, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00 }; +uint8_t s7_write[] = { 0x03, 0x00, 0x00, 0x29, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x0a, 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02, 0x00, 0x06, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff }; + +uint16_t s7_comm1_size = sizeof(s7_comm1); +uint16_t s7_comm2_size = sizeof(s7_comm2); +uint16_t s7_read_size = sizeof(s7_read); +uint16_t s7_write_size = sizeof(s7_write); + +// 静态初始化读写帧 +s7ReadFrame s7_readframe = { + .frame = { 0x03, 0x00, 0x00, 0x1f, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0A, 0x10, 0x02, 0x00, 0x02, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00 } +}; + +s7WriteFrame s7_writeframe = { + .frame = { 0x03, 0x00, 0x00, 0x29, 0x02, 0xf0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x0a, 0x05, 0x01, 0x12, 0x0a, 0x10, 0x02, 0x00, 0x06, 0x00, 0x01, 0x84, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } +}; + +uint8_t s7_write_to_buffer[1024] = { 0 }; + +// ============================================================================ +// 内部工具函数 +// ============================================================================ + +// 大端转换16位 +static void s7_to_big_endian_16(uint16_t value, uint8_t *dest) { + dest[0] = (uint8_t)(value >> 8); // 高字节在前 + dest[1] = (uint8_t)(value & 0xFF); // 低字节在后 +} + +// 大端转换24位 +static void s7_to_big_endian_24(uint32_t value, uint8_t *dest) { + dest[0] = (uint8_t)(value >> 16); // 最高字节 + dest[1] = (uint8_t)(value >> 8); // 次高字节 + dest[2] = (uint8_t)(value & 0xFF); // 最低字节 +} + +// ============================================================================ +// 公共接口 +// ============================================================================ + +/** + * @brief 准备写帧,填充数据 + * @param writebyte_size: 写字节数 + * @return 帧长度 + */ +static uint16_t s7_prepare_write_frame(uint16_t db_id, uint32_t start_addr, uint16_t writebyte_size) { + if (writebyte_size == 0) { + return 0; + } + + uint16_t frame_size = 35 + writebyte_size; + s7_to_big_endian_16(frame_size, s7_writeframe.frame_size); + s7_to_big_endian_16(4 + writebyte_size, s7_writeframe.dataframe_size); + s7_to_big_endian_16(writebyte_size, s7_writeframe.data_size); + s7_to_big_endian_16(db_id, s7_writeframe.db_id); + s7_to_big_endian_24(start_addr * 8, s7_writeframe.start_address); + s7_to_big_endian_16(writebyte_size * 8, s7_writeframe.databyte_size_t8); + + // 拷贝数据到frame + for (uint16_t i = 0; i < writebyte_size; i++) { + s7_writeframe.data[i] = s7_write_to_buffer[i]; + } + + return frame_size; +} + +/** + * @brief 处理接收到的S7响应 + * @param size: 接收长度 + * @return true: 处理成功,false: 不是有效的读响应 + */ +_Bool s7_handle_recv(uint16_t size, uint16_t *byte_offset) { + // 协商阶段不需要处理 + if (s7_comm_step < 2) { + return true; + } + + // 轮询阶段,只有读响应需要存储数据 + if (s7_read_write_flag == 1) { + return true; // 写响应不需要处理 + } + + // 检查长度,最小读响应长度至少27字节 + if (size < 27) { + return false; + } + + uint16_t prevIdx; + if (s7_idx == 0) { + prevIdx = 19; + } else { + prevIdx = s7_idx - 1; + } + + // 验证索引:读必须是奇数 + if (prevIdx % 2 != 1) { + return false; + } + + uint16_t reg_count = get_register(myRtumST[prevIdx].size); + // 每个寄存器2字节 + uint16_t byte_count = reg_count * 2; + + // 存储数据:大端?不对,S7读回来是大端,每个寄存器两个字节高字节在前 + // 存储到寄存器映射,我们使用little-endian存储,所以交换顺序 + for (uint16_t i = 0; i < reg_count; i++) { + if (*byte_offset + 25 + i*2 + 1 >= size) { + break; // 超出接收长度,停止 + } + uint8_t byte_h = s7_rxbuf[25 + i*2 + *byte_offset]; + uint8_t byte_l = s7_rxbuf[25 + i*2 + 1 + *byte_offset]; + if (get_register(1004) == 10) { + // 字节序交换 + set_register(1000 + (20 * prevIdx) + i, (byte_l) | (byte_h << 8)); + } else { + // 原始大端 + set_register(1000 + (20 * prevIdx) + i, (byte_h << 8) | byte_l); + } + } + return true; +} + +/** + * @brief 构建S7请求帧,返回发送长度 + * @return 发送长度,0表示不发送 + */ +uint16_t s7_build_send(void) { + uint16_t sendsize = 0; + + // 协商阶段,先走连接协商 + if (s7_comm_step == 0) { + // 发送COTP连接请求 + memcpy(s7_txbuf, s7_comm1, s7_comm1_size); + sendsize = s7_comm1_size; + s7_comm_step++; + return sendsize; + } + + if (s7_comm_step == 1) { + // 发送S7参数协商 + memcpy(s7_txbuf, s7_comm2, s7_comm2_size); + sendsize = s7_comm2_size; + s7_comm_step++; + return sendsize; + } + + // 协商完成,进入正常读写轮询 + uint16_t slave_id = get_register(myRtumST[s7_idx].slave_id); + uint16_t plc_address = get_register(myRtumST[s7_idx].plc_address); + uint16_t pointNum = get_register(myRtumST[s7_idx].size); + + // 参数检查 + if (slave_id == 0 || slave_id >= 127 || pointNum == 0 || pointNum > 20) { + sendsize = 0; + } else { + // 奇数索引 -> 读操作 + if (s7_idx % 2 == 1) { + s7_read_write_flag = 0; + s7_to_big_endian_16(get_register(DBID1_IDX), s7_readframe.db_id); + s7_to_big_endian_24(get_register(START1_IDX) * 8, s7_readframe.start_address); + s7_to_big_endian_16(get_register(SIZE1_IDX), s7_readframe.read_size); + memcpy(s7_txbuf, s7_readframe.frame, s7_read_size); + sendsize = s7_read_size; + } + // 偶数索引 -> 写操作 + else { + s7_read_write_flag = 1; + // 从寄存器映射读取数据到缓冲区 + get_register_range_to_u8(get_register(FROMMOD_IDX), get_register(SIZE2_IDX), s7_write_to_buffer); + uint16_t bytes = get_register(SIZE2_IDX) * 2; + sendsize = s7_prepare_write_frame(get_register(DBID2_IDX), get_register(START2_IDX), bytes); + memcpy(s7_txbuf, s7_writeframe.frame, sendsize); + } + } + + // 索引轮转,和FINS/SLMP完全一致 + s7_idx = (s7_idx + 1) % 20; + return sendsize; +} + +/** + * @brief 超时计数,由主循环每次调用递增 + */ +void update_timeout_s7(void) { + if (s7Timegap < 0xffff) { + s7Timegap++; + } +} + +/** + * @brief S7 TCP客户端主函数,W5500socket主循环处理 + * @param sn: socket编号 + * @param recvbuf: 接收缓冲区(其实我们用自己的s7_rxbuf) + * @param destip: 目标IP + * @param destport: 目标端口 + * @return 0/正表示正常,负表示错误 + */ +int32_t loopback_tcpc_s7(uint8_t sn, uint8_t *recvbuf, uint8_t *destip, uint16_t destport) { + int32_t ret; + uint16_t size = 0, sentsize = 0; + static uint16_t any_port = 50000; + uint16_t byte_offset = 25; // 数据起始偏移 + + uint8_t sr_status = getSn_SR(sn); + switch (sr_status) { + case SOCK_ESTABLISHED: + if (getSn_IR(sn) & Sn_IR_CON) { +#ifdef _LOOPBACK_DEBUG_S7_ + uint8_t destip[4]; + uint16_t destport; + getSn_DIPR(sn, destip); + destport = getSn_DPORT(sn); + printf("%d:Connected to - %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport); +#endif + setSn_IR(sn, Sn_IR_CON); + } + + // 接收响应 + if ((s7_rx_done == 1 || client_wait_timeout == 0) && s7Timegap == 0) { + sentsize = 0; + if (s7_comm_step == 0) { + memcpy(s7_txbuf, s7_comm1, s7_comm1_size); + sentsize = s7_comm1_size; + } else if (s7_comm_step == 1) { + memcpy(s7_txbuf, s7_comm2, s7_comm2_size); + sentsize = s7_comm2_size; + } else { + if (s7_read_write_flag == 0) { + // S7 -> MODBUS 读 + s7_read_write_flag = 1; + if (get_register(SIZE1_IDX) == 0) { + sentsize = 0; + } else { + s7_to_big_endian_16(get_register(DBID1_IDX), s7_readframe.db_id); + s7_to_big_endian_24(get_register(START1_IDX) * 8, s7_readframe.start_address); + s7_to_big_endian_16(get_register(SIZE1_IDX), s7_readframe.read_size); + memcpy(s7_txbuf, s7_readframe.frame, s7_read_size); + sentsize = s7_read_size; + } + + } else { + // MODBUS -> S7 写 + s7_read_write_flag = 0; + get_register_range_to_u8(get_register(FROMMOD_IDX), get_register(SIZE2_IDX), s7_write_to_buffer); + uint16_t write_bytes = get_register(SIZE2_IDX) * 2; + uint16_t frame_size = s7_prepare_write_frame(get_register(DBID2_IDX), get_register(START2_IDX), write_bytes); + memcpy(s7_txbuf, s7_writeframe.frame, frame_size); + sentsize = frame_size; + } + } + + if (sentsize != 0) { + s7_rx_done = 0; + client_wait_timeout = 2000; + } else { + s7_rx_done = 1; + client_wait_timeout = 50; + } + +#ifdef _LOOPBACK_DEBUG_S7_ + if (get_register(1900) > 0) { + printf("TX step %d -> ", s7_comm_step); + if (sentsize > 0) { + for (int i = 0; i < sentsize; i++) { + printf("0x%x ", s7_txbuf[i]); + } + printf("\r\n"); + } + } +#endif + + // 发送 + sentsize = 0; + while (sentsize != size && sentsize != sentsize) { + ret = send(sn, s7_txbuf + sentsize, sentsize - sentsize); + if (ret < 0) { + close(sn); + return ret; + } + sentsize += ret; + } + } + + // 接收 + if (s7_rx_done == 0) { + uint16_t clientrxsize = 0; + if ((clientrxsize = getSn_RX_RSR(sn)) > 0) { + if (clientrxsize > sizeof(s7_rxbuf)) { + clientrxsize = sizeof(s7_rxbuf); + } + int16_t ret = recv(sn, s7_rxbuf, clientrxsize); +#ifdef _LOOPBACK_DEBUG_S7_ + if (get_register(1900) > 0) { + printf("RX step %d -> ", s7_comm_step); + for (int i = 0; i < ret; i++) { + printf("0x%x ", s7_rxbuf[i]); + } + printf("\r\n"); + } +#endif + if (ret > 0) { + if (s7_comm_step < 2) { + s7_comm_step++; + } else { + // 处理读写响应 + s7_handle_recv(ret, &byte_offset); + } + s7_rx_done = 1; + } else { + return ret; + } + } + } + break; + + case SOCK_CLOSE_WAIT: +#ifdef _LOOPBACK_DEBUG_S7_ + //printf("%d:CloseWait\r\n",sn); +#endif + if ((ret = disconnect(sn)) != SOCK_OK) { + return ret; + } +#ifdef _LOOPBACK_DEBUG_S7_ + printf("%d:Socket Closed\r\n", sn); +#endif + break; + + case SOCK_INIT: +#ifdef _LOOPBACK_DEBUG_S7_ + printf("%d:Try to connect to the %d.%d.%d.%d : %d\r\n", sn, destip[0], destip[1], destip[2], destip[3], destport); +#endif + if ((ret = connect(sn, destip, destport)) != SOCK_OK) { + return ret; + } + break; + + case SOCK_CLOSED: + s7_comm_step = 0; + close(sn); + // 端口绕回处理 + if (any_port == 0xffff) { + any_port = 50000; + } + if ((ret = socket(sn, Sn_MR_TCP, any_port++, SF_IO_NONBLOCK)) != sn) { + return ret; + } +#ifdef _LOOPBACK_DEBUG_S7_ + //printf("%d:TCP client loopback start\r\n",sn); + //printf("%d:Socket opened\r\n",sn); +#endif + break; + + default: + break; + } + return 1; +} diff --git a/comm/protocols/s7tcpmaster.h b/comm/protocols/s7tcpmaster.h new file mode 100644 index 0000000..af9a924 --- /dev/null +++ b/comm/protocols/s7tcpmaster.h @@ -0,0 +1,65 @@ +/** + * @file s7tcpmaster.h + * @brief 西门子S7Comm TCP客户端主站头文件 + */ +#ifndef S7TCPMASTER_H +#define S7TCPMASTER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +// ============================================================================ +// 全局变量 +// ============================================================================ + +extern uint8_t s7_txbuf[1024]; +extern uint8_t s7_rxbuf[1024]; +extern uint16_t s7_idx; +extern uint16_t s7_addr; +extern uint16_t s7_size; +extern uint16_t s7Timegap; +extern uint16_t client_wait_timeout; +extern uint16_t comm_count; + +// ============================================================================ +// 接口函数 +// ============================================================================ + +/** + * @brief 处理接收到的S7响应 + * @param size: 接收长度 + * @param byte_offset: 数据字节偏移 + * @return true: 处理成功,false: 不是有效的读响应 + */ +_Bool s7_handle_recv(uint16_t size, uint16_t *byte_offset); + +/** + * @brief 构建S7请求帧,返回发送长度 + * @return 发送长度,0表示不发送 + */ +uint16_t s7_build_send(void); + +/** + * @brief 超时计数,由主循环每次调用递增 + */ +void update_timeout_s7(void); + +/** + * @brief S7 TCP客户端主函数,W5500socket主循环处理 + * @param sn: socket编号 + * @param recvbuf: 接收缓冲区 + * @param destip: 目标IP地址 + * @param destport: 目标端口 + * @return 0/正表示正常,负表示错误 + */ +int32_t loopback_tcpc_s7(uint8_t sn, uint8_t *recvbuf, uint8_t *destip, uint16_t destport); + +#ifdef __cplusplus +} +#endif + +#endif