/* demo61.exe must run on 7188 PC1 ----- ISA-7520R(or 7520R) -----+-------+-----..... (demo62)RS232(9600) RS485(9600) | | | | [7188-1] com2 ---------------------+ | (run demo61.exe) RS485 (9600) | | [7188-2] com2 -----------------------------+ (run demo61.exe) RS485 (9600) (1) 7188-1/7188-2 receive commmands from PC1,and echo commond for it's address (2) PC1 send command to 7188 if 7188-1's local address is 01, (use demo60.exe to set 7188 address) 7188-2's local address is 02 (a)$01M(CR) --> 7188-1 echo : !017188(CR) $02M(CR) --> 7188-2 echo : !027188(CR) (b)$010(CR) --> 7188-1 echo : !01+string store in it+(CR) $020(CR) --> 7188-2 echo : !02+string store in it+(CR) (default string is "7188_demo61"); (c)$011string(CR) --> 7188-1 echo : !01(CR) and store new string $021string(CR) --> 7188-2 echo : !02(CR) and store new string (d)$01x(CR),x:other cmd --> 7188-1 echo ?01(CR) --> invalid command $02x(CR),x:other cmd --> 7188-2 echo ?02(CR) --> invalid command */ #include #include #include #include #include"..\lib\7188.h" int ReadCmdFromCom2(char *cmd); int ProcessCmd(char *cmd); int SendCmdToCom2(char *cmd); #define WrongCmd 1 #define GetName 2 #define ReadStr 3 #define SendStr 4 unsigned int LocalAddr; char Str[80]="!017188_demo61"; char cmd[80]; char ModualName[]="!017188"; char InvalidCommand[]="?01"; char OkCommand[]="!01"; char HexToAscii[]={ '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; void main(void) { int quit=0; InitLib(); LocalAddr=ReadEEP(7,255); ModualName[1]=Str[1]=InvalidCommand[1]=OkCommand[1]=HexToAscii[LocalAddr>>4]; ModualName[2]=Str[2]=InvalidCommand[2]=OkCommand[2]=HexToAscii[LocalAddr&0x0f]; Show5DigitLed(1,LocalAddr>>4); Show5DigitLed(2,LocalAddr&0x0f); Show5DigitLed(3,17); Show5DigitLed(4,17); Show5DigitLed(5,17); InstallCom(2,9600,8,0,1); while(!quit){ if(kbhit()){ if(getch()=='q') quit=1; } if(IsCom(2)){ Show5DigitLed(3,16); if(NoError==ReadCmdFromCom2(cmd)) { Show5DigitLed(3,17); switch(ProcessCmd(cmd)){ case WrongCmd: Show5DigitLed(5,14); break; case GetName: Show5DigitLed(5,15); break; case ReadStr: Show5DigitLed(5,0); break; case SendStr: Show5DigitLed(5,1); break; } } } } RestoreCom(2); } #define TIMEOUT 6000 int ReadCmdFromCom2(char *cmd) { int count=0; int i=0,quit=0; while(!quit){ count=0; while(!IsCom(2)){ count++; if(count>=TIMEOUT) return TimeOut; } cmd[i]=ReadCom(2); if(cmd[i]=='\r') { /* 0x0d */ cmd[i]=0; quit=1; } else i++; } return NoError; } int SendCmdToCom2(char *cmd) { static int count=0; Set485DirToTransmit(2); while(*cmd){ ToCom(2,*cmd++); count++; Show5DigitLed(4,count&0x0f); } ToCom(2,'\r'); WaitTransmitOver(2); Set485DirToReceive(2); return NoError; } int AsciiToHex(char data) { if(data>='0' && data<='9') return data-'0'; if(data>='A' && data <='F') return data-'A'+10; if(data>='a' && data <='f') return data-'a'+10; return 0; } int ProcessCmd(char *cmd) { int addr; addr=(AsciiToHex(cmd[1])<<8)+AsciiToHex(cmd[2]); if(addr!=LocalAddr) return NoError; switch(cmd[0]){ case '$': switch(cmd[3]){ case 'M': /* read modeal name */ SendCmdToCom2(ModualName); return GetName; case '0': SendCmdToCom2(Str); return ReadStr; case '1': SendCmdToCom2(OkCommand); strcpy(Str+3,cmd+4); return SendStr; default: SendCmdToCom2(InvalidCommand); return WrongCmd; } default: /* wrong command */ SendCmdToCom2(InvalidCommand); return WrongCmd; } }