/* TEST18.C StopWatch 0--> for Led ON/OFF StopWatch 1--> for change 7000 device StopWatch 2--> for measure 7000's response time StopWatch 3--> for EasyView (Read) Timeout COM2: RS-485 <--> slave controller COM4: RS-232 <--> PC (for test & debug) */ #include #include #include #include #include #include "..\lib\7188.h" #define NoError 0 #define Error 1 #define NoCard 2 #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 fSendCmd=0; int fSendQuit=0; int SendCmdTo7000_1(unsigned char *cCmd) { while (*cCmd) { ToCom1(*cCmd++); } ToCom1(0x0d); /* send out 0x0D */ return NoError; } int SendCmdTo7000_2(unsigned char *cCmd) { ToCom4Str(cCmd); ToCom4('\r'); ToCom4('\n'); 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); } } int quit=0; /* 7052 */ int addr2=0; unsigned long TimeCom2; int WaitCom2=0; char CmdCom2[5]="@01"; char CmdCom2_1[30]="@01000000000000000000000000"; unsigned ErrorCom2=0; unsigned char ResponseCom2[80]; unsigned Data7042[4]={1,2,4,8}; unsigned char Data7067[4]={1,2,4,8}; unsigned long CountCom2=0; int BufCom2Idx=0; void DoCom2(void) {int data; int ret; if(!fSendCmd){ if(fSendQuit){ SendCmdTo7000_2("@01Q"); /* Send 4 byte cmd will let slave stop */ DelayMs(10); quit=1; } else return; } else if(!WaitCom2){ if(fSendCmd==2){ CmdCom2_1[3]=hex_to_ascii[Data7067[0]>>4]; CmdCom2_1[4]=hex_to_ascii[Data7067[0]&0x0F]; CmdCom2_1[5]=hex_to_ascii[Data7067[1]>>4]; CmdCom2_1[6]=hex_to_ascii[Data7067[1]&0x0F]; CmdCom2_1[7]=hex_to_ascii[Data7067[2]>>4]; CmdCom2_1[8]=hex_to_ascii[Data7067[2]&0x0F]; CmdCom2_1[9]=hex_to_ascii[Data7067[3]>>4]; CmdCom2_1[10]=hex_to_ascii[Data7067[3]&0x0F]; CmdCom2_1[11]=hex_to_ascii[Data7042[0]>>12]; CmdCom2_1[12]=hex_to_ascii[(Data7042[0]>>8)&0x0F]; CmdCom2_1[13]=hex_to_ascii[(Data7042[0]>>4)&0x0F]; CmdCom2_1[14]=hex_to_ascii[Data7042[0]&0x0F]; CmdCom2_1[15]=hex_to_ascii[Data7042[1]>>12]; CmdCom2_1[16]=hex_to_ascii[(Data7042[1]>>8)&0x0F]; CmdCom2_1[17]=hex_to_ascii[(Data7042[1]>>4)&0x0F]; CmdCom2_1[18]=hex_to_ascii[Data7042[1]&0x0F]; CmdCom2_1[19]=hex_to_ascii[Data7042[2]>>12]; CmdCom2_1[20]=hex_to_ascii[(Data7042[2]>>8)&0x0F]; CmdCom2_1[21]=hex_to_ascii[(Data7042[2]>>4)&0x0F]; CmdCom2_1[22]=hex_to_ascii[Data7042[2]&0x0F]; CmdCom2_1[23]=hex_to_ascii[Data7042[3]>>12]; CmdCom2_1[24]=hex_to_ascii[(Data7042[3]>>8)&0x0F]; CmdCom2_1[25]=hex_to_ascii[(Data7042[3]>>4)&0x0F]; CmdCom2_1[26]=hex_to_ascii[Data7042[3]&0x0F]; ret=SendCmdTo7000_2(CmdCom2_1); } else { ret=SendCmdTo7000_2(CmdCom2); } if(ret) { ErrorCom2++; } else { WaitCom2=1; BufCom2Idx=0; StopWatchStart(StopWatchCOM2); } } else { while(IsCom2()){ data=ReadCom2(); ToCom4(data); if(data=='\r'){ ResponseCom2[BufCom2Idx]=0; WaitCom2=0; ToCom4('\n'); break; } else { ResponseCom2[BufCom2Idx]=data; } BufCom2Idx++; } if(!WaitCom2){ CountCom2++; fSendCmd--; } else { StopWatchReadValue(StopWatchCOM2,&TimeCom2); if(TimeCom2>TimeOutPeriod){ WaitCom2=0; fSendCmd--; } } } } int addr1=0; unsigned long TimeCom1; int WaitCom1=0; char CmdCom1[30]; unsigned ErrorCom1=0; unsigned char ResponseCom1[80]; unsigned long CountCom1=0; int BufCom1Idx=0; void DoCom4(void) { int key; while(IsCom4()){ key=ReadCom4(); if(key=='q'){ fSendQuit=1; /* quit and stop slave controller */ } else if(key=='0'){ Data7067[0]=Data7067[1]=Data7067[2]=Data7067[3]=0; } else if(key=='1'){ Data7067[0]=Data7067[1]=Data7067[2]=Data7067[3]=0x7F; } else if(key==27) { /* ESC */ quit=1; /* quit but not stop controller */ } } } void main(void) { unsigned long TestTime; InitLib(); /* driver initial */ InstallCom2(115200L,8,0,1); /* COM2, RS-485, half-duplex mode, INT */ outp(0x102,0x07); InstallCom4(57600L,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) { RefreshWDT(); DoLed(); DoCom2(); DoCom4(); StopWatchReadValue(StopWatchTest,&TestTime); if(TestTime>=100){ StopWatchStart(StopWatchTest); if(!fSendCmd){ fSendCmd=2; } } } TimerClose(); DisableWDT(); RestoreCom(2); RestoreCom(4); }