/* DEMO18.C StopWatch 0--> for Led ON/OFF StopWatch 1--> for change 7000 device StopWatch 2--> for measure 7000's response time COM1:(RS-232) <--> (RS-232) 7520 (RS-485) <--> 7052 * 4 (addr:0x20-0x23) COM2:(RS-485) <--> Host controller(another 7188 or PC) COM3:(RS-232) <--> (RS-232) 7520 (RS-485) <--> 7067 * 4 (addr:0x30-0x33) COM4:(RS-232) <--> (RS-232) 7520 (RS-485) <--> 7042 * 4 (addr:0x40-0x43) */ #include #include #include #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 #define Time7021 500 #define StopWatchLed 0 #define StopWatchCOM1 1 #define StopWatchCOM2 2 #define StopWatchCOM3 3 #define StopWatchCOM4 4 #define StopWatchTest 7 unsigned long TimeOutPeriod=50; /* 0.05sec */ int SendCmdTo7000_1(unsigned char *cCmd) { while (*cCmd) { ToCom1(*cCmd++); } ToCom1(0x0d); /* send out 0x0D */ return NoError; } int SendCmdTo7000_2(unsigned char *cCmd) { Set485DirToTransmit(2); /* change 485 direction to Transmit */ while (*cCmd) { ToCom2(*cCmd++); } ToCom2(0x0d); /* send out 0x0D */ WaitTransmitOver(2); /* wait this character transmit over */ Set485DirToReceive(2); /* change 485 direction to Receive */ return NoError; } int SendCmdTo7000_3(unsigned char *cCmd) { while (*cCmd) { ToCom3(*cCmd++); } ToCom3(0x0d); /* send out 0x0D */ return NoError; } int SendCmdTo7000_4(unsigned char *cCmd) { while (*cCmd) { ToCom4(*cCmd++); } ToCom4(0x0d); /* send out 0x0D */ return NoError; } int CheckSumOk(unsigned char *cmd,int len) { unsigned char sum=0; int i; for(i=0;i>4]==*cmd && hex_to_ascii[sum&0x0f]==*(cmd+1)){ *cmd=0; return 1; } return 0; /*checksum error */ } unsigned long LedTime; int LedMode=1; void DoLed(void) { StopWatchReadValue(StopWatchLed,&LedTime); if(LedTime>=500){ LedMode=!LedMode; if(LedMode) LedOn(); else LedOff(); StopWatchStart(StopWatchLed); } } /* 7042 */ int addr4=0; unsigned long TimeCom4; int WaitCom4=0; char CmdCom4[8]="@400000"; unsigned ErrorCom4=0; unsigned char ResponseCom4[80]; unsigned int Data7042[4]; unsigned long CountCom4=0; int BufCom4Idx=0; void DoCom4(void) {int data; int ret; if(!WaitCom4){ SendCommand4: CmdCom4[2]='0'+addr4; CmdCom4[3]=hex_to_ascii[Data7042[addr4]>>12]; CmdCom4[4]=hex_to_ascii[(Data7042[addr4]>>8)&0x0F]; CmdCom4[5]=hex_to_ascii[(Data7042[addr4]>>4)&0x0F]; CmdCom4[6]=hex_to_ascii[Data7042[addr4]&0x0F]; ret=SendCmdTo7000_4(CmdCom4); if(ret) { ErrorCom4++; } else { WaitCom4=1; BufCom4Idx=0; StopWatchStart(StopWatchCOM4); } } else { while(IsCom4()){ data=ReadCom4(); if(data=='\r'){ ResponseCom4[BufCom4Idx]=0; WaitCom4=0; break; } else { ResponseCom4[BufCom4Idx]=data; } BufCom4Idx++; } if(!WaitCom4){ CountCom4++; addr4++; addr4&=3; /* addr2=0,1,2,3,0,1,2,3... */ goto SendCommand4; } else { StopWatchReadValue(StopWatchCOM4,&TimeCom4); if(TimeCom4>TimeOutPeriod){ WaitCom4=0; addr4++; addr4&=3; /* addr2=0,1,2,3,0,1,2,3... */ goto SendCommand4; } } } } /* 7052 */ int addr1=0; unsigned long TimeCom1; int WaitCom1=0; char CmdCom1[5]="@20"; unsigned ErrorCom1=0; unsigned char ResponseCom1[80]; unsigned char Data7052[4]; unsigned long CountCom1=0; int BufCom1Idx=0; void DoCom1(void) {int data; int ret; if(!WaitCom1){ SendCommand1: CmdCom1[2]='0'+addr1; ret=SendCmdTo7000_1(CmdCom1); if(ret) { ErrorCom1++; } else { WaitCom1=1; BufCom1Idx=0; StopWatchStart(StopWatchCOM1); } } else { while(IsCom1()){ data=ReadCom1(); if(data=='\r'){ ResponseCom1[BufCom1Idx]=0; WaitCom1=0; break; } else { ResponseCom1[BufCom1Idx]=data; } BufCom1Idx++; } if(!WaitCom1){ CountCom1++; Data7052[addr1]=ascii_to_hex(ResponseCom1[4])+ (ascii_to_hex(ResponseCom1[4])<<4); addr1++; addr1&=3; /* addr2=0,1,2,3,0,1,2,3... */ goto SendCommand1; } else { StopWatchReadValue(StopWatchCOM1,&TimeCom1); if(TimeCom1>TimeOutPeriod){ WaitCom1=0; addr1++; addr1&=3; /* addr2=0,1,2,3,0,1,2,3... */ goto SendCommand1; } } } } /* 7067 Relay output*/ int addr3=0; unsigned long TimeCom3; int WaitCom3=0; char CmdCom3[5]="@300F"; unsigned ErrorCom3=0; unsigned char ResponseCom3[80]; unsigned char Data7067[4]={0,0,0,0}; unsigned long CountCom3=0; int BufCom3Idx=0; void DoCom3(void) {int data; int ret; if(!WaitCom3){ SendCommand3: CmdCom3[2]='0'+addr3; CmdCom3[3]=hex_to_ascii[Data7067[addr3]>>4]; CmdCom3[4]=hex_to_ascii[Data7067[addr3]&0x0F]; ret=SendCmdTo7000_3(CmdCom3); if(ret) { ErrorCom3++; } else { WaitCom3=1; BufCom3Idx=0; StopWatchStart(StopWatchCOM3); } } else { while(IsCom3()){ data=ReadCom3(); if(data=='\r'){ ResponseCom3[BufCom3Idx]=0; WaitCom3=0; break; } else { ResponseCom3[BufCom3Idx]=data; } BufCom3Idx++; } if(!WaitCom3){ CountCom3++; addr3++; addr3&=3; /* addr2=0,1,2,3,0,1,2,3... */ goto SendCommand3; } else { StopWatchReadValue(StopWatchCOM3,&TimeCom3); if(TimeCom3>TimeOutPeriod){ WaitCom3=0; /* printf("\n7060 timeout");*/ addr3++; addr3&=3; /* addr2=0,1,2,3,0,1,2,3... */ goto SendCommand3; } } } } int addr2=0; unsigned long TimeCom2; int WaitCom2=0; char CmdCom2[30]; unsigned ErrorCom2=0; unsigned char ResponseCom2[80]; unsigned long CountCom2=0; int BufCom2Idx=0; int quit=0; unsigned char Addr7188[2]={'0','1'}; void EchoCom2(void) { if(CmdCom2[0]=='@' && CmdCom2[1]==Addr7188[0] && CmdCom2[2]==Addr7188[1]){ if(BufCom2Idx==3){ /* response the value of 7052 */ sprintf(ResponseCom2,">%02X%02X%02X%02X",Data7052[0],Data7052[1],Data7052[2],Data7052[3]); SendCmdTo7000_2(ResponseCom2); } else if(BufCom2Idx==27){ /* get the value of 7042 & 7067 */ Data7067[0]=(ascii_to_hex(CmdCom2[3])<<4)+ascii_to_hex(CmdCom2[4]); Data7067[1]=(ascii_to_hex(CmdCom2[5])<<4)+ascii_to_hex(CmdCom2[6]); Data7067[2]=(ascii_to_hex(CmdCom2[7])<<4)+ascii_to_hex(CmdCom2[8]); Data7067[3]=(ascii_to_hex(CmdCom2[9])<<4)+ascii_to_hex(CmdCom2[10]); Data7042[0]=(ascii_to_hex(CmdCom2[11])<<12)+(ascii_to_hex(CmdCom2[12])<<8) +(ascii_to_hex(CmdCom2[13])<<4)+ascii_to_hex(CmdCom2[14]); Data7042[1]=(ascii_to_hex(CmdCom2[15])<<12)+(ascii_to_hex(CmdCom2[16])<<8) +(ascii_to_hex(CmdCom2[17])<<4)+ascii_to_hex(CmdCom2[18]); Data7042[2]=(ascii_to_hex(CmdCom2[19])<<12)+(ascii_to_hex(CmdCom2[20])<<8) +(ascii_to_hex(CmdCom2[21])<<4)+ascii_to_hex(CmdCom2[22]); Data7042[3]=(ascii_to_hex(CmdCom2[23])<<12)+(ascii_to_hex(CmdCom2[24])<<8) +(ascii_to_hex(CmdCom2[25])<<4)+ascii_to_hex(CmdCom2[26]); SendCmdTo7000_2(">"); } else if(BufCom2Idx==4){ quit=1; } } } void DoCom2(void) { int key; while(IsCom2()){ key=ReadCom2(); if(key=='\r'){ CmdCom2[BufCom2Idx]=0; EchoCom2(); BufCom2Idx=0; break; } else { CmdCom2[BufCom2Idx]=key; } BufCom2Idx++; } } void main(void) { unsigned long TestTime; unsigned long TestCount=0; int com=1,count; InitLib(); /* driver initial */ InstallCom1(115200L,8,0,1); /* COM1, RS-485, */ outp(0x202,0x07); InstallCom2(115200L,8,0,1); /* COM2, RS-485, half-duplex mode, INT */ outp(0x102,0x07); InstallCom3(115200L,8,0); /* COM3 */ InstallCom4(115200L,8,0); /* com4 connect to PC1 */ Init5DigitLed(); /* initial & blank the 5-digit LED */ LedOn(); /* red-LED on */ TimerOpen(); EnableWDT(); /* StopWatchStart(StopWatchLed); */ StopWatchStart(StopWatchTest); while(!quit) { TestCount++; RefreshWDT(); /* DoLed(); */ DoCom1(); DoCom2(); DoCom3(); DoCom4(); StopWatchReadValue(StopWatchTest,&TestTime); if(TestTime>=1000){ StopWatchStart(StopWatchTest); switch(com){ case 1: count=CountCom1; break; case 2: count=TestCount; break; case 3: count=CountCom3; break; case 4: count=CountCom4; break; } Show5DigitLed(5,count%10); count/=10; Show5DigitLed(4,count%10); count/=10; Show5DigitLed(3,count%10); Show5DigitLed(1,com); com++; if(com==5) com=1; TestCount=CountCom1=CountCom2=CountCom3=CountCom4=0; } } TimerClose(); DisableWDT(); RestoreCom(1); RestoreCom(2); RestoreCom(3); RestoreCom(4); }