/* 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:7017 Addr #4:7060 Addr #5:7080D */ #include #include #include "..\lib\7188.h" #define NoError 0 #define Error 1 #define NoCard 2 #define DLE 0x10 #define STX 0x02 #define ACK 0x06 #define NAK 0x15 #define ETX 0x03 #define DATA_SIZE 30 #define TimeoutFor7000 800L unsigned Data7[512]; unsigned NewData[DATA_SIZE]; char szCmd1[80]; int ComPort=3; /* this value can be 1/3/4 --> COM 1/3/4 */ int read_touch7(int *TouchType, int *TouchAddr, int *TouchValue); int write_to_easyview_1(int addr, unsigned int value); int write_to_easyview_2(int addr, int onoff); int WriteToEasyview1(int addr, unsigned int value); int WriteToEasyview2(int addr, int onoff); int ascii_to_hex(char c); int Set7060(unsigned data); /* ------------------------------------------------------------------- */ int IsCom3OutBufEmpty(void); /* ------------------------------------------------------------------- */ int SendDataToCom(char *buf,int no) { int ret; for(;no;no--){ ret=ToCom(ComPort,*buf++); } return ret; } int fSet7060=0; int fSet7021=0; int Data7060=1; int Data7080=0; int Mode485=0; int LastMode485; int Wait485=0; unsigned char Buf485[120]; int Buf485Idx; int f7021On=1; int f7060On=1; int f7080On=1; int f7013On=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 Serror7060=0,Rerror7060=0; static unsigned Serror7021=0,Rerror7021=0; static unsigned Serror7080=0,Rerror7080=0; int Wait7060=0,Wait7060Out=0; int Wait7013=0; int Wait7017=0; int Wait7021=0; int Wait7080Out=0; int mode7080=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 7060 */ if(!Wait7060){ ret=SendCmdTo7000(2,"@04",0); if(ret) { Mode485=f7013On?1:2; Serror7060++; printf("[S60Err]=%u ret=%d",Serror7060,ret); NoCmd=1; } else { Wait7060=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value; Wait7060=0; Mode485=f7013On?1:2; /* process 7060 data */ value=0xF^ascii_to_hex(Buf485[4]); f7021On=value&4; f7060On=value&2; if(!f7080On && (value&1)){ /* off to on --> change dir */ Data7080^=1; } f7080On=value&1; value<<=4; value+=ascii_to_hex(Buf485[2]); NewData[1]=(Data7[1]&0xFF00)+value; /* printf("\n7060-->%s",Buf485); */ NoCmd=1; } else { if(WaitTime>TimeOutPeriod){ Wait7060=0; Mode485=f7013On?1:2; printf("\n7060 timeout"); NoCmd=1; } } } break; case 1: /* for 7013 */ if(!Wait7013){ ret=SendCmdTo7000(2,"#01",0); if(ret) { Mode485=2; Serror7013++; /* printf("[S13Err]=%u ret=%d",Serror7013,ret); */ NoCmd=1; } else { Wait7013=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value; f7013On=0; Wait7013=0; Mode485=2; /* process 7013 data */ value=Buf485[2]-'0'; value=value*10+Buf485[3]-'0'; value=value*10+Buf485[4]-'0'; value=value*10+Buf485[6]-'0'; value=value*10+Buf485[7]-'0'; if(Buf485[1]=='-') value=-value; NewData[22]=value; /* printf("\n7013-->%s",Buf485); */ NoCmd=1; } else { if(WaitTime>TimeOutPeriod){ Wait7013=0; Mode485=2; printf("\n7013 timeout"); NoCmd=1; } } } break; case 2: /* for 7017 */ if(!Wait7017){ ret=SendCmdTo7000(2,"$03A",0); if(ret) { Mode485=5; Serror7017++; printf("[S17Err]=%u ret=%d",Serror7017,ret); NoCmd=1; } else { Wait7017=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value,i,j; char *ptr; Wait7017=0; Mode485=5; /* process 7017 data */ ptr=Buf485+1; for(i=0;i<8;i++){ value=0; for(j=0;j<4;j++){ value<<=4; value|=ascii_to_hex(*ptr++); } NewData[2+i*2]=(int)((value*40000L)>>16); } /* printf("\n7017-->%s",Buf485); */ NoCmd=1; } else { if(WaitTime>TimeOutPeriod){ Wait7017=0; Mode485=5; printf("\n7017 timeout"); NoCmd=1; } } } break; case 3: /* for 7021 */ if(!Wait7021){ static int cnt7021=0; static int mode7021=1; char cmd[12]="#0200.000"; char buf[10]; if(!f7021On){ Mode485=LastMode485; break; } 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; /* printf("\n%s -->> 7021",cmd); */ ret=SendCmdTo7000(2,cmd,0); if(ret) { Mode485=LastMode485; Serror7021++; printf("[S21Err]=%u ret=%d",Serror7021,ret); NoCmd=1; } else { Wait7021=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ int value,i,j; char *ptr; Wait7021=0; Mode485=LastMode485; NoCmd=1; } else { if(WaitTime>TimeOutPeriod){ Wait7021=0; Mode485=LastMode485; printf("\n7021 timeout"); NoCmd=1; } } } break; case 4: /* set 7060 output */ if(!Wait7060Out){ char cmd[5]="@040"; if(!f7060On){ Mode485=LastMode485; NoCmd=1; break; } cmd[3]=hex_to_ascii[Data7060]; ret=SendCmdTo7000(2,cmd,0); if(ret) { Mode485=LastMode485; Serror7060++; printf("[S60Err]=%u ret=%d",Serror7060,ret); NoCmd=1; } else { /* Show5DigitLed(1,Data7060); */ Wait7060Out=Wait485=1; Buf485Idx=0; StopWatchStart(4); Data7060<<=1; if(Data7060>=0x10) Data7060=1; } } else { if(!Wait485){ Wait7060Out=0; Mode485=LastMode485; NoCmd=1; } else { if(WaitTime>TimeOutPeriod){ Wait7060Out=0; Mode485=LastMode485; printf("\n7060 Out timeout"); NoCmd=1; } } } break; case 5: /* set 7080 output,read 7080 input */ if(!Wait7080Out){ char cmd[8]="@05DO00"; if(!f7080On){ Mode485=0; break; } if(mode7080){ Data7080&=0x0001; mode7080=0; } else { Data7080|=0x0002; mode7080=1; } cmd[6]=hex_to_ascii[Data7080]; ret=SendCmdTo7000(2,cmd,0); if(ret) { Mode485=0; mode7080=0; Serror7080++; printf("[S80Err]=%u ret=%d",Serror7080,ret); } else { Wait7080Out=Wait485=1; Buf485Idx=0; StopWatchStart(4); } } else { if(!Wait485){ Wait7080Out=0; if(mode7080==0) Mode485=0; } else { if(WaitTime>TimeOutPeriod){ Wait7080Out=0; Mode485=0; mode7080=0; printf("\n7080 Out timeout"); } } } break; } if(NoCmd){ if(fSet7060){ fSet7060=0; LastMode485=Mode485; Mode485=4; } else if(fSet7021){ fSet7021=0; LastMode485=Mode485; Mode485=3; } } } void main(void) { int quit=0,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; InitLib(); /* driver initial */ /* InstallCom(2,9600L,8,0,1); */ InstallCom(2,115200L,8,0,1); Init5DigitLed(); /* initial & blank the 5-digit LED */ LedOn(); /* red-LED on */ /* while(!quit){ char cmd[8]="@05DO00"; char buf[12]; if(kbhit4()) { getch4(); quit=1;} cmd[6]=hex_to_ascii[Data7080]; SendCmdTo7000(2,cmd,0); ReceiveResponseFrom7000(2,buf,600,0); Data7080^=2; } quit=0; */ TimerOpen(); InstallUserTimer(MonitorCom2For485); EnableWDT(); StopWatchStart(0); StopWatchStart(1); StopWatchStart(2); /* for 7021 */ while(!quit) { RefreshWDT(); StopWatchReadValue(2,&Time7021); if(Time7021>=300){ fSet7021=1; StopWatchStart(2); } StopWatchReadValue(0,&LedTime); if(LedTime>=500){ LedMode=!LedMode; f7013On=1; if(LedMode){ LedOn(); fSet7060=1; } else LedOff(); StopWatchStart(0); /* show status of 7000's */ switch(ShowMode){ case 0: /* show 7013D */ tmpdata=NewData[22]; if(tmpdata<0) { tmpdata=-tmpdata; } Show5DigitLed(5,tmpdata%10); tmpdata/=10; Show5DigitLed(4,tmpdata%10); tmpdata/=10; Show5DigitLedWithDot(3,tmpdata%10); tmpdata/=10; Show5DigitLed(2,tmpdata%10); tmpdata/=10; if(NewData[22]&0x8000) /* NewData[22]<0 */ Show5DigitLed(1,17); else if(tmpdata) Show5DigitLed(1,tmpdata); else Show5DigitLed(1,16); ShowMode=1; break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: tmpdata=NewData[ShowMode*2]; Show5DigitLed(1,ShowMode-1); if(tmpdata<0){ tmpdata=-tmpdata; Show5DigitLed(2,17); } else Show5DigitLed(2,16); { long val=3052L*tmpdata/10000L; tmpdata=val; Show5DigitLed(5,tmpdata%10); tmpdata/=10; Show5DigitLed(4,tmpdata%10); tmpdata/=10; Show5DigitLedWithDot(3,tmpdata%10); } ShowMode++; if(ShowMode>8) 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); RestoreCom(ComPort); printf("\n7013 Error s=%u r=%u",Serror7013,Rerror7013); printf("\n7017 Error s=%u r=%u",Serror7017,Rerror7017); printf("\n7060 Error s=%u r=%u",Serror7060,Rerror7060); printf("\n7021 Error s=%u r=%u",Serror7021,Rerror7021); }