/* DEMO37.C ---- demo. program for demoBOX4 StopWatch 0--> for Led ON/OFF StopWatch 1--> for change 7000 device StopWatch 2--> for measure 7000's response time Addr #1:7013D Addr #2:7021 Addr #3:7012D Addr #4:7044D use: demo37 [/t] [/bxxxx] use /t to show debug information use /bxxx to set COM2 baudrate, default is 115200. (the baudrate must be set the the same as on 7000 modules) */ #include #include #include "..\lib\7188.h" #define NoError 0 #define Error 1 #define NoCard 2 #define TimeoutFor7000 800L unsigned char Data7013[10]; unsigned char Data7012d[10]; char szCmd1[80]; int ascii_to_hex(char c); int Set7044(unsigned data); /* ------------------------------------------------------------------- */ int fDebug=0; int fSet7044=0; int fSet7021=0; int Data7044=0; int Data7012=0; int Mode485=0; int LastMode485; int Wait485=0; unsigned char Buf485[120]; int Buf485Idx; int f7021On=1; int f7044On=1; int f7012On=1; int f7013On=0; //unsigned char Step[8]={'1','3','2','6','4','C','8','9'}; unsigned char Step[8]={'3','6','C','9','3','6','C','9'}; unsigned char Led[4]={'1','2','4','8'}; int LedIdx=0; int StepIdx=0; int StepDir=1; unsigned StepCount=0; int DataStep=0; void MonitorCom2For485(void) { if(Wait485){ int data; while(IsCom(2)){ data=ReadCom(2); if(data=='\r'){ Buf485[Buf485Idx]=0; Wait485=0; return; } else Buf485[Buf485Idx++]=data; } } } static unsigned Serror7013=0,Rerror7013=0; static unsigned Serror7017=0,Rerror7017=0; static unsigned Serror7044=0,Rerror7044=0; static unsigned Serror7021=0,Rerror7021=0; static unsigned Serror7012=0,Rerror7012=0; int Wait7044=0,Wait7044Out=0; int Wait7013=0; int Wait7017=0; int Wait7021=0; int Wait7012Out=0; int Wait7012In=0; int mode7012=0; unsigned long TimeOutPeriod=500; /* 0.5sec */ void Run7000(void) { unsigned long WaitTime; int NoCmd=0; int ret; StopWatchReadValue(4,&WaitTime); switch(Mode485){ case 0: /* for 7044 */ if(!Wait7044){ ret=SendCmdTo7000(2,"@04",0); if(fDebug) printf("\n@04 --> 7044"); if(ret) { Mode485=1; Serror7044++; printf("[S60Err]=%u ret=%d",Serror7044,ret); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { Wait7044=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value; Wait7044=0; /* process 7044 data */ /* Inverse the input data, for switch on [I] will get 1 , on [O] will get 0 */ value=0xF^ascii_to_hex(Buf485[4]); if(fDebug) printf("\n7044-->%s",Buf485); if(value != Data7044){ /* when DI1 0-->1 , change step direction */ if(!(Data7044&1) && (value&1)) { if(Data7044 & 2) StepDir=!StepDir; else StepCount=0; } Data7044=value; } Mode485=1; NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { if(WaitTime>TimeOutPeriod){ Wait7044=0; Mode485=1; if(fDebug) printf("[Mode485=%d]",Mode485); printf("\n7044 timeout\n"); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } } } break; case 1: /* for 7013 */ if(!Wait7013){ ret=SendCmdTo7000(2,"#01",0); if(fDebug) printf("\n#01 --> 7013"); if(ret) { Mode485=3; Serror7013++; if(fDebug) printf("[S13Err]=%u ret=%d",Serror7013,ret); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { Wait7013=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value; // f7013On=0; Wait7013=0; Mode485=3; /* save 7013D data */ Data7013[0]=Buf485[2]; Data7013[1]=Buf485[3]; Data7013[2]=Buf485[4]; Data7013[3]=Buf485[6]; Data7013[4]=Buf485[7]; if(fDebug) printf("\n7013-->%s",Buf485); NoCmd=1; if(fDebug) printf("[Mode485=%d]",Mode485); fSet7044=1; } else { if(WaitTime>TimeOutPeriod){ Wait7013=0; Mode485=3; printf("\n7013 timeout\n"); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } } } break; case 2: /* for 7021 */ if(!Wait7021){ static int cnt7021=0; static int mode7021=1; char cmd[12]="#0200.000"; char buf[10]; if(!f7021On){ Mode485=LastMode485; // Mode485=3; if(fDebug) printf("[Mode485=%d]",Mode485); break; } if(Data7044 & 4){ /* DI3 for 7021 sweep ON/OFF */ if(mode7021){ cnt7021++; if(cnt7021>=20) mode7021=0; } else { cnt7021--; if(cnt7021<=0) mode7021=1; } } cmd[3]='0'+cnt7021/10; cmd[4]='0'+cnt7021%10; ret=SendCmdTo7000(2,cmd,0); if(fDebug) printf("\n%s -->> 7021",cmd); if(ret) { Mode485=LastMode485; // Mode485=3; Serror7021++; printf("[S21Err]=%u ret=%d",Serror7021,ret); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { Wait7021=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value,i,j; char *ptr; Wait7021=0; Mode485=LastMode485; // Mode485=3; if(fDebug) printf("\n7021-->%s",Buf485); NoCmd=1; if(fDebug) printf("[Mode485=%d]",Mode485); fSet7044=1; } else { if(WaitTime>TimeOutPeriod){ Wait7021=0; Mode485=LastMode485; // Mode485=3; printf("\n7021 timeout\n"); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } } } break; case 3: /* read 7012D input */ if(!Wait7012Out){ char cmd[8]="#03"; if(!f7012On){ Mode485=4; if(fDebug) printf("[Mode485=%d]",Mode485); break; } ret=SendCmdTo7000(2,cmd,0); if(fDebug) printf("\n#03 --> 7012D"); if(ret) { Mode485=4; mode7012=0; Serror7012++; printf("[S80Err]=%u ret=%d",Serror7012,ret); } else { Wait7012Out=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ Wait7012Out=0; if(fDebug) printf("\n7012-->%s",Buf485); Data7012d[0]=Buf485[2]; Data7012d[1]=Buf485[3]; Data7012d[2]=Buf485[5]; Data7012d[3]=Buf485[6]; Data7012d[4]=Buf485[7]; if(mode7012==0){ Mode485=4; if(fDebug) printf("[Mode485=%d]",Mode485); } } else { if(WaitTime>TimeOutPeriod){ Wait7012Out=0; Mode485=4; mode7012=0; printf("\n7012 Out timeout\n"); if(fDebug) printf("[Mode485=%d]",Mode485); } } } break; case 4: if(!Wait7012In){ ret=SendCmdTo7000(2,"@03DI",0); if(fDebug) printf("\n@03DI --> 7012"); if(ret) { Mode485=0; Serror7044++; printf("[S60Err]=%u ret=%d",Serror7044,ret); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { Wait7012In=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value; Wait7012In=0; value=ascii_to_hex(Buf485[7]); if(fDebug) printf("\n7012-->%s",Buf485); if(value) DataStep=3; else { if(DataStep) { DataStep--; if(!DataStep){ if(StepDir) { StepCount++; if(StepCount>999) StepCount=0; } else { if(StepCount) StepCount--; else StepCount=999; } } } } Mode485=0; NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { if(WaitTime>TimeOutPeriod){ Wait7012In=0; Mode485=0; if(fDebug) printf("[Mode485=%d]",Mode485); printf("\n7012 timeout\n"); NoCmd=1; fSet7044=1; if(fDebug) printf("[Mode485=%d]",Mode485); } } } break; case 5: /* set 7044 output */ if(!Wait7044Out){ char cmd[6]="@0400"; if(!f7044On){ Mode485=LastMode485; NoCmd=1; if(fDebug) printf("[Mode485=%d f7044On=%d]",Mode485,f7044On); break; } cmd[3]=Step[StepIdx]; cmd[4]=Led[LedIdx]; ret=SendCmdTo7000(2,cmd,0); if(fDebug) printf("\n%s --> 7044D",cmd); if(Data7044&2){ /* DI2 for Step motor ON/OFF */ if(StepDir) StepIdx++; else StepIdx--; StepIdx&=7; } if(ret) { Mode485=LastMode485; Serror7044++; printf("[S44Err]=%u ret=%d",Serror7044,ret); NoCmd=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { /* Show5DigitLed(1,Data7044); */ Wait7044Out=Wait485=1; Buf485Idx=0; StopWatchStart(4); // Data7044<<=1; // if(Data7044>=0x10) Data7044=1; } } else { if(!Wait485){ Wait7044Out=0; Mode485=LastMode485; if(fDebug) printf("\n7044D-->%s",Buf485); NoCmd=1; if(fDebug) printf("[Mode485=%d]",Mode485); } else { if(WaitTime>TimeOutPeriod){ Wait7044Out=0; Mode485=LastMode485; printf("\n7044 Out timeout\n"); NoCmd=1; if(fDebug) printf("[Mode485=%d]",Mode485); } } } break; } if(NoCmd){ if(fSet7044){ fSet7044=0; Wait7044Out=0; LastMode485=Mode485; Mode485=5; if(fDebug) printf("[Mode485=%d]",Mode485); } else if(fSet7021){ fSet7021=0; LastMode485=Mode485; Mode485=2; /* 7021 */ if(fDebug) printf("[Mode485=%d]",Mode485); } } } void main(int argc,char *argv[]) { int quit=0; unsigned tmpdata; int now_i1,now_i2,now_i3,now_i4; int old_i1,old_i2,old_i3,old_i4; int i,k,LedMode=1,ShowMode=0; char c,rep; unsigned long LedTime,Time7021; unsigned long baud=115200L,b; for(i=1;i=1000){ fSet7021=1; StopWatchStart(2); } StopWatchReadValue(0,&LedTime); if(LedTime>=1000){ LedMode=!LedMode; f7013On=1; if(LedMode){ LedOn(); } else LedOff(); if(Data7044 & 8) { LedIdx++; LedIdx&=3; } StopWatchStart(0); /* show status of 7000's */ switch(ShowMode){ case 0: /* show 7013D */ Show5DigitLed(1,16 /*Data7013[0]-'0'*/); Show5DigitLed(2,Data7013[1]-'0'); Show5DigitLedWithDot(3,Data7013[2]-'0'); Show5DigitLed(4,Data7013[3]-'0'); Show5DigitLed(5,Data7013[4]-'0'); Data7013[5]=0; ShowMode=1; if(fDebug) { printf("[Data7013=%s]",Data7013); } break; case 1: /* show 7012D */ Show5DigitLed(1,Data7012d[0]-'0'); Show5DigitLedWithDot(2,Data7012d[1]-'0'); Show5DigitLed(3,Data7012d[2]-'0'); Show5DigitLed(4,Data7012d[3]-'0'); Show5DigitLed(5,Data7012d[4]-'0'); Data7012d[5]=0; ShowMode=2; if(fDebug) { printf("[Data7012d=%s]",Data7012d); } break; case 2: /* Show step moter count */ tmpdata=StepCount; Show5DigitLed(1,ShowMode); Show5DigitLed(2,17); Show5DigitLed(5,tmpdata%10); tmpdata/=10; Show5DigitLed(4,tmpdata%10); tmpdata/=10; Show5DigitLed(3,tmpdata%10); ShowMode=0; break; } } Run7000(); if (kbhit4()) { /* is user press PC's keyboard from COM4 */ c=getch4(); /* if this key is Q or Q --> exit this program */ switch(c){ case 'q': case 'Q': quit=1; break; } } } TimerClose(); DisableWDT(); RestoreCom(2); printf("\n7013 Error s=%u r=%u",Serror7013,Rerror7013); printf("\n7012 Error s=%u r=%u",Serror7012,Rerror7012); printf("\n7044 Error s=%u r=%u",Serror7044,Rerror7044); printf("\n7021 Error s=%u r=%u",Serror7021,Rerror7021); }