/* XDemo04: Send/Receive command to Xserver Compiler: BC++ 3.1 ,TC++ 3.0, TC++ 1.01, TC 2.0 Compile mode: large Project: user.c v7000.c vModbus.c [after vcom3002.lib] ..\Lib\7188EL.Lib ..\Lib\TCPIP32.Lib ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version. 19~!@#$ -> Any non-null command will be accepted. This demo is the original user.c User can modify their own Xserver from this file. Hardware: 7188E Refer 7188e\TCP\Doc\[Big5|Eng|Gb2312]\Vxcomm.htm 7188e\TCP\Xserver\Xserver.htm 7188e\TCP\Xserver\Function.htm to get more information. Because the kernal of Xserver used InstallCom() function. Please use printCom1() instead of Print() if you want to send formatted output from COM1. [30/Nov/2001] by TCK [16,Sep,2004] Commented by Annita [27/June/2004] by Liam [09/Aug/2004] by Liam [26,July,2011] by Nicholas */ #include #include #include #include "..\lib\7188e.h" #include "..\lib\tcpip32.h" #include "..\lib\vxcomm.h" void UserCount(void) { /* user's timer trigger function. Call AddUserTimerFunc can trigger this function periodly. Example: void UserInit(void) { AddUserTimerFunc(UserCount, 1000); // 1000 ms } 7188E/8000E executes UserCount every second. In this function, users should avoid to use any function that could conflict hardware signals. Such as: 1. ClockHigh(),ClockLow(), ClockHighLow(), 2. EEPROM functions. 3. 5DigitLed functions. 4. NVRAM function. 5. RTC function.(GetTime(),SetTime(),GetDate(),SetDate()) 6. float point calculation. Refer to XDemo09 for example code . [16,Sep,2004] Commented by Annita */ } void UserInit(void) { /* In this function, user CAN: 1. initialize user's program. 2. set time interval for calling UserCount(). 3. set initial value of I/O or variables for UserLoopFun(). 4. set initial value of I/O or variables for another functions. 5. change the default TCP PORT 10000/9999/502 to others value. [after vcom3004.lib] Syntax: Port10000=newport_10000; for calling UserCmd (user.c) Port9999=newport_9999; for calling VcomCmd7000 (v7000.c) Port502=newport_502; for calling VcomCmdModbus (vModbus.c) [after vcom3002.lib] PortUser=newport_User; for calling VcomCmdUser (user.c) [after vcom3005.lib] Default port value: Port10000=10000; Port9999=9999; Port502=502; PortUser=0; If the port value is 0, Xserver will not listen that port. That means the port will be disable. Refer to XDemo09 for example code */ InitLib(); } void UserLoopFun(void) { /* VxComm.exe will call this function every scan time Refer to XDemo11 for Real-time I/O control */ } 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". XServer trims "19" and passes the other data (without "19") to Cmd. You can put a message to Response. Xserver will reply it to the Client when finish this function. Maximum length of Cmd is 1458 bytes. Maximum length of Response is 1024 bytes. Refer to all demo to use the procedure. [16,Sep,2004] Commented by Annita */ int i; sscanf(Cmd,"%d",&i); if (Cmd[0]) /* Not Null command */ { strcpy(Response,Cmd); /* echo user's command back */ return 1; /* return OK */ } return 0; /* 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". Received data stored in the TCPREADDATA structure. The TCPREADDATA is declared as below: Type define t_TCPReadData{ int Comport; int Socket; int Length; char* ReadUartChar; }TCPREADDATA; user can get the following message: p->Socket : The socket number is an Index number for TCP/IP connection. It records which the client connected to 7188E series using TCP/IP. p->ReadUartChar : the buffer stored the command data(include "23") Maximum length of p->ReadUartChar is 32767 bytes. so, the data needs triming "23". p->Length : the command data length(include the two byte "23") When we want return the data to the Client, We can use the function as following: usage: VcomSendSocket(p->Socket,pdata,datalength); Refer to XDemo23 for example code If you need sendback the data in other function,such as UserLoopFun, please refer to XDemo34. [16,Sep,2004] Commented by Annita */ return 1; /* any value will be accept */ } int VcomCmdUser(TCPREADDATA *p) { /* VCOM3005 (22,Feb,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. user can get the following message: p->ReadUartChar : the buffer store the command data. Maximum length of p->ReadUartChar is 32767 bytes. p->Length : the command data length. p->Socket : the socket number that receive the command, that is when the user function wants return message to the client, just use the socket to send data. usage: VcomSendSocket(p->Socket,pdata,datalength); */ /* here just send back the command to the client.*/ 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. You can use function VcomSendSocket to send a message to the client when a connection is established. For example: VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes. skt: socket number assigned to the TCP/IP client. */ 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. You can use function VcomSendSocket to send a message to the client when a connection is established. For example: VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes. skt: socket number assigned to the TCP/IP client. */ 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. You can use function VcomSendSocket to send a message to the client when a connection is established. For example: VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes. skt: socket number assigned to the TCP/IP client. */ skt=skt; /*do nothing*/ }