/* MBTS_LAN.c: Modbus TCP Slave via Ethernet Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) Compile mode: large Project: MBTS_LAN.C lib\G4500.lib lib\TCP_DM32.lib lib\MBTCP.lb Hardware: G-4513 [Feb 17, 2015] by William */ #include "lib\g4500.h" #include "lib\Tcpip32.h" #include "lib\MBTCP.h" #define SOCKET_NUM 32 // define the maximum number of sockets #define SOCKET_QUEUE_LENGTH SOCKET_NUM -1 // maximum connection queue length #define NUM_OF_SERVICE_PORT 1 unsigned short service_port[NUM_OF_SERVICE_PORT]= {502}; // define the service port for Server struct timeval seltime= {0, 20}; // 2000ns=2ms unsigned ActiveSkt[2]= {0, 0}; STOPWATCH swSocket[SOCKET_NUM], swTimer; ulong ulSocketTimeout=86400000UL; // Sets the socket communication timeout value. // If the timeout expires, the connection will be released // Time unit: ms fd_set rfds; // set of socket file descriptors for reading int X305IO_AnalogIn_2(int iChannel); typedef struct tagSocket_State { int active; // socket is active int init; // socket just initiated struct sockaddr_in sin; // client address unsigned port; // port number int be_master_socket; // is master socket } socket_state; socket_state socket_state_table[SOCKET_NUM]; // sockets state table struct ip host_ip; unsigned char RecvBuffer[1024] = {0}; unsigned char SendBuffer[1024] = {0}; int TCP_server_initial(void) { /* Return: 0: ok. -1: function "llip" error -2: function "Ninit" error -3: function "Portinit" error */ int i, iRet; GetSavedIp((struct ip*) &host_ip); // read the saved IP address and mask from EEPROM of 7188E iRet=lhip("7186EX", &host_ip); iRet=Ninit(); // initiate host environment if(iRet!=NoError) return -2; iRet=Portinit("*"); // initiate all network device if(iRet!=NoError) { Nterm(); return -3; } FD_ZERO(&rfds); for(i=0; i=16) ActiveSkt[1]|=1<<(err-16); else ActiveSkt[0]|=1<=16) ActiveSkt[1]&=~(1<<(current_socket-16)); else ActiveSkt[0]&=~(1<0) { Print("==== Recv data ====\r\n"); for(i=0; i Modbus Memory */ //fValue=X305IO_AnalogIn(0); fValue=X305IO_AnalogIn_HexToFloat(X305IO_AnalogIn_2(0)); Print("AI0:%6.4fma\r\n",fValue); Memory_AI[0]=(int)(fValue); //Memory_AI[0]=(int)(fValue*1638.35); /* (fValue/20.0*32767.0) */ // /* Analog input (Ch1) ==> Modbus Memory */ // fValue=X305IO_AnalogIn(1); // Memory_AI[1]=(int)(fValue*1638.35); // /* Analog input (Ch2) ==> Modbus Memory */ // fValue=X305IO_AnalogIn(2); // Memory_AI[2]=(int)(fValue*1638.35); // /* Analog input (Ch3) ==> Modbus Memory */ // fValue=X305IO_AnalogIn(3); // Memory_AI[3]=(int)(fValue*1638.35); iProcess=1; break; case 1: // /* Analog input (Ch4) ==> Modbus Memory */ // fValue=X305IO_AnalogIn(4); // Memory_AI[4]=(int)(fValue*1638.35); // /* Analog input (Ch5) ==> Modbus Memory */ // fValue=X305IO_AnalogIn(5); // Memory_AI[5]=(int)(fValue*1638.35); // /* Analog input (Ch6) ==> Modbus Memory */ // fValue=X305IO_AnalogIn(6); // Memory_AI[6]=(int)(fValue*1638.35); // /* Analog input (Ch7) ==> Modbus Memory */ // fValue=X305IO_AnalogIn(7); // Memory_AI[7]=(int)(fValue*1638.35); iProcess=2; break; case 2: /* Digital input ==> Modbus Memory */ iValue=X305IO_Read_All_DI(); for(i=0; i<3; i++) { if(iValue&0x01) Memory_DI[i]=1; else Memory_DI[i]=0; iValue>>=1; } iProcess=3; break; case 3: /* Modbus Memory ==> Digital output */ iValue=0; for(i=0; i<3; i++) { if(Memory_DO[i]!=0) iValue+=(1<>8, iLib&0xff); InstallUserTimerFunction_ms(100, refreshIOValue); // the timer used to get the I/O value of G-4513(Time interval: 100ms) ulSocketTimeout=10000; // socket timeout=10000 ms ModbusTCP_Init(); // init modbus TCP //Step 1: Initialize TCP Server err=TCP_server_initial(); if(err<0) { if(err==-1) Print("This program must be executed in uPAC-7186EX series.\r\n"); if(err==-2) Print("Ninit() failed! error %d\r\n", err); if(err==-3) Print("Portinit() failed! error %d\r\n", err); } else Print("Initialize TCP Server\r\n"); //Step 2: Create sockets and bound to names then listen for connections for(i=0; i=0) continue; // success if(err==-1) Print("cannot create a socket on port: %d", service_port[i]); if(err==-2) Print("cannot bind to port %d \n\r", service_port[i]); if(err==-3) Print("cannot listen on port %d \n\r", service_port[i]); } //Step 3: Service loop: 3-1: wait on sockets and accept // 3-2: perform service, receive and send service_loop_continue=1; T_StopWatchStart(&swTimer); GetIp(MyIP); Print("IP:%d.%d.%d.%d\r\n",MyIP[0],MyIP[1],MyIP[2],MyIP[3]); while(service_loop_continue) // service loop { YIELD(); //Step 3-1: Wait for activity on sockets and accept a connection err=wait_to_accept(); if(err==-1) { Print("selectsocket error\n\r"); break; } else if(err==-2) continue; //selectsocket() time out else if(err==-3) Print("accept error %d\n\r"); //Step 3-2: perform service, receive and send for(i=0; i