/* * @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 static uint8_t buf[1024]; int32_t loopback_tcpc_s7(uint8_t sn,uint8_t *destip, uint16_t destport) { int32_t ret; // return value for SOCK_ERRORs uint16_t size = 0, sentsize = 0; // Destination (TCP Server) IP info (will be connected) // >> loopback_tcpc() function parameter // >> Ex) // uint8_t destip[4] = {192, 168, 0, 214}; // uint16_t destport = 5000; // Port number for TCP client (will be increased) static uint16_t any_port = 50000; // Socket Status Transitions // Check the W5500 Socket n status register (Sn_SR, The 'Sn_SR' controlled by Sn_CR command or Packet send/recv status) switch (getSn_SR(sn)) { case SOCK_ESTABLISHED: if (getSn_IR(sn) & Sn_IR_CON) { // Socket n interrupt register mask; TCP CON interrupt = connection with peer is successful #ifdef _LOOPBACK_DEBUG_ 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); // this interrupt should be write the bit cleared to '1' } ////////////////////////////////////////////////////////////////////////////////////////////// // Data Transaction Parts; Handle the [data receive and send] process ////////////////////////////////////////////////////////////////////////////////////////////// if ((size = getSn_RX_RSR(sn)) > 0) { // Sn_RX_RSR: Socket n Received Size Register, Receiving data length if (size > 1024) { size = 1024; // DATA_BUF_SIZE means user defined buffer size (array) } ret = recv(sn, buf, size); // Data Receive process (H/W Rx socket buffer -> User's buffer) if (ret <= 0) { return ret; // If the received data length <= 0, receive failed and process end } size = (uint16_t) ret; sentsize = 0; // Data sentsize control while (size != sentsize) { ret = send(sn, buf + sentsize, size - sentsize); // Data send process (User's buffer -> Destination through H/W Tx socket buffer) if (ret < 0) { // Send Error occurred (sent data length < 0) close(sn); // socket close return ret; } sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero. } } ////////////////////////////////////////////////////////////////////////////////////////////// break; case SOCK_CLOSE_WAIT: #ifdef _LOOPBACK_DEBUG_ //printf("%d:CloseWait\r\n",sn); #endif if ((ret = disconnect(sn)) != SOCK_OK) { return ret; } #ifdef _LOOPBACK_DEBUG_ printf("%d:Socket Closed\r\n", sn); #endif break; case SOCK_INIT: #ifdef _LOOPBACK_DEBUG_ 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; // Try to TCP connect to the TCP server (destination) } break; case SOCK_CLOSED: close(sn); if ((ret = socket(sn, Sn_MR_TCP, any_port++, 0x00)) != sn) { if (any_port == 0xffff) { any_port = 50000; } return ret; // TCP socket open with 'any_port' port number } #ifdef _LOOPBACK_DEBUG_ //printf("%d:TCP client loopback start\r\n",sn); //printf("%d:Socket opened\r\n",sn); #endif break; default: break; } return 1; }