/* XDemo32: Backup system for Demo Board I. 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. (CD:\Napdos\Others\DemoBoard\DBoardI\*.* 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 DI of I-7044 to checks if any button is pressed. Step2: If presses button2(Analog output & input) ==> Reads In of I-7012 to fValue ==> output fValue+1 to I-87024 0 -> 1 -> ... -> 20 -> 19 -> 18 -> ... -> 1 -> 0 -> 1 -> 2 -> repeat ... Step3: If presses button0(Lamp action) DO4~DO7 of I-7044 ==> Ch4 off -> ch5 off -> ... -> ch7 off -> ch4 off -> repeat ... Step4: If presses button1(Step motor action) ==> DO0~DO3 acts to driver step motor ==> If presses button3 : CW ==> CCW CCW ==> CW Step5: If none button be pressed ==> DO4~DO7 of I-7044: All on -> All off -> All on --> repeat ... Hardware: 7188E (COM2, Baudrate=115200, checksum disable) + I-7012(#01, AI*1, 0~20mA) In <==> IOut of I-7021 + I-7021(#02, AO*1, 0~20mA) IOut <==> Current meter <==> In of I-7012 + I-7044(#03, DI*4 + DO*8) DI0~DI3 <==> Button0~Button3 Button0 ==> Lamps Button1 ==> Step motor Button2 ==> Analog output & input Button3 ==> Step motor direction inverse. DO0~DO3 <==> A+/A-/B+/B- of Step motor DO4~DO7 <==> Lamp0~Lamp3 + I-7013(#04, RTD*1) <==> RTD 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 iStateLamp; //for I-7044 outputs to Lamps. // 0: ch4 on ==> 0x10 // 1: ch5 on ==> 0x20 // 2: ch6 on ==> 0x40 // 3: ch7 on ==> 0x80 int iStateStep; //for I-7044 outputs to step motor. // 0: ch0 on ==> 0x1 // 1: ch1 on ==> 0x2 // 2: ch2 on ==> 0x4 // 3: ch3 on ==> 0x8 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 --> ... int iStepDir=0; //0(CW): A+ A- --> A- B+ --> B+ B- --> B- A+ --> A+ A- --> repeat //1(CCW): A+ B- --> B- B+ --> B+ A- --> A- A+ --> A+ B- --> repeat //A+:ch4 of DO of I-7044 //A-:ch5 0f DO of I-7044 //B+:ch6 of DO of I-7044 //B-:ch7 of DO of I-7044 char sReceive[80],sSend[20],sTemp[10]; int iStep[4]={3,6,12,9}; //For step motor float fValue=0; unsigned iuValue; int iRet; int iLampAction=0,iStepAction=1,iAnalogAction=0,iNoButton=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(9600L); AddUserTimerFunction(UserCount,1000); //Executes control rules per-second when losing control 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-7013 #04 SendCmdTo7000(2,"#04",0); ReceiveResponseFrom7000(2,sReceive,500,0); if(!strcmp(sReceive,">-0000")) printCom1("\n\rI-7013 under range\n\r"); else if(!strcmp(sReceive,">+9999")) printCom1("\n\rI-7013 over range\n\r"); else { sscanf(sReceive+1,"%f",&fValue); printCom1("aaaa\n\r"); printCom1("\n\rI-7013 RTD= %f \n\r",fValue); } //I-7044 #03 SendCmdTo7000(2,"$036",0); ReceiveResponseFrom7000(2,sReceive,500,0); sscanf(sReceive+4,"%01X",&iuValue); printCom1("I-7044 DI = %02X(Hex)\n\r",iuValue); //Reads the buttons iuValue=~iuValue; iLampAction=iuValue&1; //Button 0 iStepAction=iuValue&2; //Button 1 iAnalogAction=iuValue&4; //Button 2 if(iuValue&8) //Button 3 iStepDir=1; else iStepDir=0; if((iuValue&0xF)==0) iNoButton=1; else iNoButton=0; if(iAnalogAction) { printCom1("Analog Input & output\n\r"); //Reads In of I-7012(#01) SendCmdTo7000(2,"#01",0); ReceiveResponseFrom7000(2,sReceive,500,0); sscanf(sReceive+1,"%f",&fValue); printCom1(" I-7012 In= %f \n\r",fValue); //Increases fValue if(iIncrease) { fValue++; if(fValue>20) { fValue=19; iIncrease=0; } } else { fValue--; if(fValue<0) { fValue=1; iIncrease=1; } } //I-7021(#02) outputs sprintf(sSend,"#02%06.3f",fValue); printCom(1," I-7021 IOut ==> %s\n\r",sSend); SendCmdTo7000(2,sSend,0); ReceiveResponseFrom7000(2,sReceive,500,0); } Delay(10); RefreshWDT(); iuValue=0; if(iLampAction) { printCom1("Lamp action\n\r"); iStateLamp++; if(iStateLamp>3) iStateLamp=0; iuValue=0xF0^(0x10<3) iStateStep=0; if(iStateStep<0) iStateStep=3; iuValue+=iStep[iStateStep]; } if(iNoButton) { SendCmdTo7000(2,"$036",0); ReceiveResponseFrom7000(2,sReceive,500,0); printCom1("No button pressed ==> %s\n\r",sReceive); sscanf(sReceive+1,"%01X",&iuValue); if(iuValue==0) iuValue=0xF0; else iuValue=0; RefreshWDT(); } sprintf(sTemp,"@03%02X",iuValue); SendCmdTo7000(2,sTemp,0); ReceiveResponseFrom7000(2,sReceive,500,0); } //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 }