/* XDemo33: Backup system for Demo Board II. Compiler: BC++ 3.1 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. When one client connects to the 7188E/8000E, the client gets the control of the system. (7188E\TCP\Xserver\Client\VB5\DemoBoardII\*.* is for this demo) When no client connects to the 7188E/8000E, or the client crashes, the 7188E/8000E runs the control rules that users wrote in Xserver. That makes the whole system runs continually. Control rules in the Xserver: Step1: Reads AI0 of I-87017 Step2: Outputs AI0+1 to I-87024 0 -> 1 -> ... -> 9 -> 10 -> 9 -> ... -> 1 -> 0 -> 1 -> repeat ... Step3: Shows I-87024 ch0 value to Ch0~ch10 of I-87057. I-87024 Ch0= 0V ==> I-87057(#06) ch0 on I-87024 Ch0= 1V ==> I-87057(#06) ch1 0n ... Step4: Switchs outputs of I-87057(#01) (Lamps) if ch0 of I-87024 0-->10V ==> Ch0~Ch3 0n --> ch4~ch7 on --> ch8~ch11 on --> ch0~ch3 on --> repeat ... if ch0 of I-87024 10-->0V ==> Ch0~Ch3 0n --> ch8~ch11 on --> ch4~ch7 on --> ch0~ch3 on --> repeat ... Step5: Reads RTD0 of I-87013 Step6: Shows RTD0 value to I-87057(#06) RTD0: 10~19.9 ==> ch11 of I-87057(#06) on 20~29.9 ==> ch12 of I-87057(#06) on ... 50~59.9 ==> ch15 of I-87057(#06) on Hardware: 7188E (COM2, Baudrate=115200, checksum disable) + I-87017(#00, AI*8) ch0~ch7 <==> ch0 of I-87024 + I-87057(#01, DO*16) ch0~ch11 <==> Lamp0~Lamp11 + I-87013(#02, RTD*4) ch0 <==> RTD + I-87024(#03, AO*4) ch0 <==> ch0~ch7 of I-87017 + I-87053(#05, DI*16) ch0~ch15 <==> ch0~ch15 of I-87057 + I-87057(#06, DO*16) ch0~ch15 <==> ch0~ch15 of I-87053 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. [20/April/2002] by Kevin [06,Oct,2004] Commented by Annita [10/Aug/2005] by Liam [26,July,2011] by Nicholas */ #include #include #include #include #include "..\lib\7188e.h" #include "..\lib\tcpip32.h" #include "..\lib\vxcomm.h" extern unsigned long ulSystemTimeout; /* system variable for disable/enable /STxxx command option. 0: disable /STxxx command option. xxxx: enable /STxxxx command option. Timeout for reset system is xxxx seconds. */ int iTimeIsUp; int iState; //for I-87057(Lamp) outputs. // 1: ch0~ch3 on ==> 0xF // 2: ch4~ch7 on ==> 0xF0 // 3: ch8~ch11 on ==> 0xF00 int iIncrease=1; //1: I-87024 output fValue 0 --> 10 // I-87057(Lamp) output state1 --> state2 --> state3 --> state1 --> ... //0: for I-87024 output fValue 10 --> 0 // I-87057(Lamp) output state3 --> state2 --> state1 --> state3 --> ... char sReceive[80],sSend[20]; float fValue=0; void UserCount(void) { /* User's timer trigger function. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ iTimeIsUp=1; } void UserInit(void) { /* Initialize user's program. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ /* 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(). */ //InstallCom(1,115200L,8,0,1); //For display information to PC monitor. //InstallCom(2,115200L,8,0,1); //For communicate with 87K modules connected on COM2(RS-485) SetBaudrate1(115200L); SetBaudrate2(115200L); AddUserTimerFunction(UserCount,1000); //Executes control rules per-second when losing control /* If the 7188E/8000E is reset by power on, enable the system timeout option (/ST). */ //if(IsResetByPowerOff()) //The function was renamed to IsResetByPowerOn() from 7188e.h. if(IsResetByPowerOn()) ulSystemTimeout=5; // 5 seconds /* Else if the 7188E/8000E is reset by watchdog, (due to /ST is up and no client connect to the 7188E/8000E) disable the system timeout option (/ST) to lets the Xserver run the backup system control rules. */ else ulSystemTimeout=0; /* System timeout option (/ST) is an important function. If client program crashes or connection is broken, 7188E/8000E can reset by watchdog when system timeout is up. */ } void UserLoopFun(void) { /* VxComm.exe will call this function every scan time Please refer to XDemo11 for Real-time I/O control */ if(iTimeIsUp) { iTimeIsUp=0; /* ScoketConnect=0 means no clients connect to the 7188E/8000E, we can use this variable to know if the system is lost control by master. Copy the master control rules to below sub. When master crashes, 7188E/8000E can automaticly switch the system control from master to 7188E/8000E. Then the system can still work fine. */ if(SocketConnected==0) { ulSystemTimeout=0; //Disable /ST option //I-87017 #00 //Reads channel 0 SendCmdTo7000(2,"#000",0); ReceiveResponseFrom7000(2,sReceive,500,0); sscanf(sReceive+1,"%f",&fValue); printCom1("87017 ch0= %f \n\r",fValue); fValue=ceil(fValue); //Increases fValue, if(iIncrease) { fValue++; if(fValue>10.5) { fValue=9; iIncrease=0; iState--; } else iState++; if(iState>3) iState=1; if(iState<1) iState=3; } else { fValue--; if(fValue<-0.5) { fValue=1; iIncrease=1; iState++; } else iState--; if(iState>3) iState=1; if(iState<1) iState=3; } Delay(10); //delay 10 ms //I-87024 #03 //Outputs channel 0 sprintf(sSend,"#030%+06.3f",fValue); printCom(1,"87024 ch0 ==> %s\n\r",sSend); SendCmdTo7000(2,sSend,0); ReceiveResponseFrom7000(2,sReceive,500,0); Delay(10); //delay 10 ms /* I-87057 #06 Shows I-87024 ch0 value to Ch0~ch10 of I-87057. I-87024 Ch0= 0V ==> I-87057 ch0 on I-87024 Ch0= 1V ==> I-87057 ch1 0n ... */ sprintf(sSend,"@06%04X",1<<(int)fValue); SendCmdTo7000(2,sSend,0); ReceiveResponseFrom7000(2,sReceive,500,0); if(sReceive[0]=='>') printCom(1,"87057 switch ok.\n\r"); else printCom(1,"87057 switch error.\n\r"); Delay(10); //delay 10 ms //I-87057 #1 //Switch lamps switch(iState) { case 1: sprintf(sSend,"%s","@01000F"); break; case 2: sprintf(sSend,"%s","@0100F0"); break; case 3: sprintf(sSend,"%s","@010F00"); break; } SendCmdTo7000(2,sSend,0); ReceiveResponseFrom7000(2,sReceive,500,0); if(sReceive[0]=='>') printCom(1,"87057(Lamp) switch ok.\n\r"); else printCom(1,"87057(Lamp) switch error.\n\r"); Delay(10); //delay 10 ms //I-87013 #02 //Reads RTD ch0 SendCmdTo7000(2,"#020",0); ReceiveResponseFrom7000(2,sReceive,500,0); sscanf(sReceive+1,"%f",&fValue); printCom(1,"87013 ch0 = %+6.2f\n\r",fValue); //Shows RTD information to I-87057 ch11~ch15 if(fValue>=10 && fValue<20) { //Turns on ch11 SendCmdTo7000(2,"#06B301",0); ReceiveResponseFrom7000(2,sReceive,500,0); } if(fValue>=20 && fValue<30) { //Truns on ch12 SendCmdTo7000(2,"#06B401",0); ReceiveResponseFrom7000(2,sReceive,500,0); } if(fValue>=30 && fValue<40) { //Turns on ch13 SendCmdTo7000(2,"#06B501",0); ReceiveResponseFrom7000(2,sReceive,500,0); } if(fValue>=40 && fValue<50) { //Turns on ch14 SendCmdTo7000(2,"#06B501",0); ReceiveResponseFrom7000(2,sReceive,500,0); } if(fValue>=50 && fValue<60) { //Turns on ch15 SendCmdTo7000(2,"#06B701",0); ReceiveResponseFrom7000(2,sReceive,500,0); } printCom(1,"\n\r"); } //Some Client programs connect to the 7188E/8000E. else ulSystemTimeout=5; //Enable /ST option and sets timeout = 5 seconds. } } 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 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". 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 }