/* XDemo57: The communication of the uPAC-7186EX acting as client and the other uPAC-7186EXs acting as server Compiler: BC++ 3.1 Compile mode: large Project: user.c v7000.c vModbus.c [after vcom3002.lib] ..\Lib\7186EL.Lib ..\Lib\tcp_dm32.lib ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version. This demo provides the client functions for uPAC-7186EX modules. You can use them to communicate with the other uPAC-7186EX without any PC helping. On this Demo,the uPAC-7186EX treated as client sends the command "10" to inquire the name of uPAC-7186EX acting as Server. Note: 1.The uPAC-7186EX has one restriction due to the collision of Ethernet. You must set the received time to 5~10 sec in the client program. Close the socket and then build a new socket to connect again while received timeout happen" This demo shows how to follow this rule. 2.Use the ET_TOUT to set the connection timeout within 800 ms to avoid the uPAC-7186EX reboot by WatchDog(refer to vsocket.c) The executing steps Step 1: Set the IP of first uPAC-7186EX as 192.168.255.1,and load the VcomNNNN.exe into it. Step 2: Set the IP of second uPAC-7186EX as 192.168.255.10,and load this demo, Xdemo57.exe to the uPAC-7186EX. Step 3: Connect the two uPAC-7186EX modules to the Ethernet as below,and power on them. PC(monitor)(Com1)<---RS232--->(Com1) uPAC-7186EX(client/server) <--->Hub<--->uPAC-7186EX(server) Step 4: During the both modules run,the user can monitor them using the program of PC through Com port. Step 5: You can see the string of sending and the string of receiving from the screen of PC. The client functions consist of the ReceiveSocketBinaryCmd(),KillSockets(),BuildSockets() and SendSocketBinaryCmd() in "Vsocket.C". Hardware: uPAC-7186EX modules * 2 uPAC-7186EX(client) uPAC-7186EX(server) Hardware: uPAC-7186EX [Dec 18, 2008] by Liam */ #include #include #include #include "..\lib\7186e.h" #include "..\lib\tcpip32.h" #include "..\lib\vxcomm.h" #include "Vsocket.h" int iSocketflag,iSend; //Uses to detect whether the socket is active; char InBuf[1023], OutBuf[1023]; //InBuf:the buffer of receing ,OutBuf:the buffer of sending int iOutskt; //The socket number of connecting unsigned int iPort; //The port number of the server unsigned char iIpStr[17]; //The IP of the server int Recesucceed=0,iTotalReceive; int total; extern jmp_buf jumper; unsigned long lTimeTick; unsigned long stime; void UserCount(void) { /* User's timer trigger function. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ if(iSocketflag==1 && iOutskt) //Set iSend=1 mean sending data to server per 4000ms set by AddUserTimerFunction,when the socket was bulit. iSend=1; else iSend=0; } void UserInit(void) { /* Initialize user's program. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ int hour, minu, sec; int year, month, day; InitLib(); /* All of the program user.c needn't function Installcom(). Becuase they have install When the Xserver initialize. If you want to change the baud rate.data format, please using the function SetBaudrate().SetDataFormat(). */ SetBaudrate(1, 115200L); SetDataFormat(1, 8, 0, 1); GetDate(&year, &month, &day); GetTime(&hour, &minu, &sec); // if(IsResetByPowerOff()) //The function was renamed to IsResetByPowerOn() from 7188e.h. if(IsResetByPowerOn()) printCom1("IsResetByPowerOn,%d/%d, %d:%d:%d\r\n",month,day,hour,minu,sec); else if(IsResetByWatchDogTimer()) printCom1("ResetByWatchDogTimer,%d/%d, %d:%d:%d\r\n",month,day,hour,minu,sec); else printCom1("jump to ffff:0000,%d/%d, %d:%d:%d\r\n",month,day,hour,minu,sec); iSocketflag=0; iSend=0; lTimeTick=0; sprintf(iIpStr,"10.0.8.23"); //change the ip to fit your your server.//192.168.255.1 iPort=10000;//change the port number to fit your your server. printCom1("IpStr=%10s,iPort=%5d\r\n",iIpStr,iPort); sprintf(OutBuf,"10\r"); //send the command of Minios7 to 7188E acting as server AddUserTimerFunction(UserCount,4000); } void UserLoopFun(void) { /* VxComm.exe will call this function every scan time Please refer to XDemo11 for Real-time I/O control */ int iRet,i; extern int errno; char InTemp[80]; if(!iSocketflag && (GetTimeTicks()-lTimeTick)>3000){ lTimeTick=GetTimeTicks(); iOutskt=BuildSocket(iIpStr,iPort,200); //buildsockets printCom1("Socket number:%d!!\r\n\r\n",iOutskt); if (iOutskt==-1){ printCom1("connect error!!\r\n"); iSocketflag=0; longjmp(jumper,1); //release the buffers relate to Ethernet } else iSocketflag=1; } if(iSend>0){ if(Recesucceed==0) { iRet=SendSocketBinaryCmd(iOutskt,OutBuf,strlen(OutBuf)); if (iRet<0)// write error { printCom1("echo Send error,code(%d)\r\n",iRet); KillSocket(iOutskt); iSocketflag=0; goto End; } else { printCom1("Total time:%d\r",total++); stime=GetTimeTicks(); } iTotalReceive=0; strnset(InBuf,0x0,1023); } iRet=ReceiveSocketBinaryCmd(iOutskt,InTemp,sizeof(InTemp),700); if (iRet==-1)// read error { if(errno==ETIMEDOUT) //Detect whether the error of recv is timeout. { if(GetTimeTicks()-stime<10000) //10 sec { Recesucceed=1; printCom1("echo Receive timeout \r\n"); } else { Recesucceed=0; printCom1("Receive timeout over 10 sec\r\n",iRet); KillSocket(iOutskt); iSend=0; iSocketflag=0; total=0; } } else { Recesucceed=0; printCom1("echo Receive error,code(%d)\r\n",iRet); KillSocket(iOutskt); iSend=0; iSocketflag=0; total=0; } } else { Recesucceed=0; iTotalReceive+=iRet; strcat(InBuf,InTemp); if(InBuf[iTotalReceive-1]=='\r') //Change the condition for your request { InBuf[iRet]=0; // add zero to end Recesucceed=0; } // printCom1("Connect->send->receive->close takes %d ms\r\n",*TimeTicks-lTimeTicks1); //debug } End: iSend=0; } } int UserCmd(unsigned char *Cmd,unsigned char *Response) { /* Xserver executes this function when received a package form TCP port 10000 and the first two bytes are "19". Funtion of Xserver, Please refer to XDemo04 for detail description. */ return 1; /* return error */ } int VcomUserBinaryCmd(TCPREADDATA *p) { /* VXCOMM.EXE 2.6.12(04,Sep,2001) or later will support this function. Xserver executes this function when received a package form TCP port 10000 and the first two bytes are "23". Please refer to XDemo04 for detail description. Please refer to XDemo23 for example code. */ return 1; /* any value will be accept */ } int VcomCmdUser(TCPREADDATA *p) { /* VCOM3005 (Feb,22,2002) or later will call this function for PortUser. When packets received by TCP PORT PortUser(user defined) of 7188E/8000E, Xserver will call this function. Please refer to XDemo04 for detail description. */ VcomSendSocket(p->Socket,p->ReadUartChar,p->Length); return 1; /* any value will be accept */ } void PortUserStart(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E via the user's defined port(PortUser), the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; //do nothing } void Port9999Start(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E TCP port 9999, the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; //do nothing } void Port502Start(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E TCP port 502, the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; //do nothing }