#include #include #include #include #include #include "..\lib\7188.h" /* Demo12: Use 7188 COM2 link to 7053(16 D/I) x 2 and 7043() x 2. address 01 : 7053-1, connect to COM2(RS485) of 7188 9600,N,8,1 address 02 : 7053-2, connect to COM2(RS485) of 7188 9600,N,8,1 address 03 : 7043-1, connect to COM2(RS485) of 7188 9600,N,8,1 address 04 : 7043-2, connect to COM2(RS485) of 7188 9600,N,8,1 7188 COM4 link to PC com1(or com2) 5digitled : digit 1 will show channel no digit 2-5 will show the input value */ extern char hex_to_ascii[16]; int ascii_to_hex(char c); int SendCmdTo7000(int iPort, unsigned char *cCmd, int iChksum); int read_7000(unsigned char *cmd); int ReceiveResponseFrom7000(int iPort, unsigned char *cCmd, long lTimeout, int iChksum); int GetModualName(int addr,char *name); /* ------------------------------------------------------------------- */ char OutCmd[10]="@01"; int Name[4]; void main() { int i,k; unsigned char c,cmd[80]; int ledmode=1; int quit=0; int channel=-1; clock_t start,end,start2; InitLib(); /* driver initial */ InstallCom(2,9600L,8,0,1); /* COM2, RS-485, half-duplex mode, INT */ /* --> need to control 485 direction */ /* --> initial 485 direction = Receive */ /* --> address 1 = 7060 */ /* COM4, RS-232, connect to PC's COM port for download */ Init5DigitLed(); /* initial & blank the 5-digit LED */ LedOn(); /* red-LED on */ start=clock(); for(i=1;i<=4;i++){ if(GetModualName(i,cmd)==NoError){ Name[i]=atoi(cmd); Show5DigitLed(1,i); Show5DigitLed(2,cmd[0]-'0'); Show5DigitLed(3,cmd[1]-'0'); Show5DigitLed(4,cmd[2]-'0'); Show5DigitLed(5,cmd[3]-'0'); } else { Name[i]=0; Show5DigitLed(1,i); Show5DigitLed(2,17); Show5DigitLed(3,17); Show5DigitLed(4,17); Show5DigitLed(5,17); } } for(i=1;i<=4;i++){ if(Name[i]==7053 || Name[i]==7041){ channel=i; Show5DigitLed(1,i); OutCmd[2]=i+'0'; break; } } printf("\n'1': show channel 1\n'2': show channel 2" "\n'3': show channel 3\n'4': show channel 4" "\n'q' : quit" "\npress any key to start."); if(Name[channel]==7053 || Name[channel]==7041){ while(!quit) { end=clock(); if((end-start)/CLK_TCK >= 0.5){ ledmode=!ledmode; if (ledmode) LedOn(); else LedOff(); start=clock(); } if (kbhit()) { /* is user press PC's keyboard from COM4 */ c=getch(); /* if this key is Q or Q --> exit this program */ switch(c){ case 'q': case 'Q': quit=1; break; case '1': case '2': case '3': case '4': if(Name[c-'0']==7053 || Name[c-'0']==7041){ channel=c-'0'; Show5DigitLed(1,channel); OutCmd[2]=c; } break; } } else { read_7000(OutCmd); } } } else printf("\nCannot find 7053 or 7041 on address 0-3"); RestoreCom(2); return; } /* ------------------------------------------------------------------- */ /* send cmd & receive result to 7000 module */ char *Binary[16]= { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111", }; int read_7000(unsigned char *cmd) { unsigned char str[20]; int val,data; static unsigned long count=0; count++; SendCmdTo7000(2, cmd, 0); printf("\n[%lu]Send=%s, Receive=",count,cmd); val=ReceiveResponseFrom7000(2, str, 60000L, 0); if (val==NoError) { printf("%s ",str); /* show the I/O value */ data=ascii_to_hex(str[1]); Show5DigitLed(2,data); printf("[%s",Binary[data]); data=ascii_to_hex(str[2]); Show5DigitLed(3,data); printf(" %s",Binary[data]); data=ascii_to_hex(str[3]); Show5DigitLed(4,data); printf(" %s",Binary[data]); data=ascii_to_hex(str[4]); Show5DigitLed(5,data); printf(" %s]",Binary[data]); } else printf("Errorno=%d(%s)",val,val==TimeOut?"TimeOut":""); return val; } int GetModualName(int addr,char *name) { char cmd[10]; int val,i; cmd[0]='$'; cmd[1]=hex_to_ascii[(addr>>4)&0xf]; cmd[2]=hex_to_ascii[addr&0xf]; cmd[3]='M'; cmd[4]=0; SendCmdTo7000(2, cmd, 0); val=ReceiveResponseFrom7000(2, cmd, 60000L, 0); if(val==NoError) { if(cmd[0]=='!'){ for(i=0;i<4;i++){ name[i]=cmd[3+i]; } name[i]=0; return NoError; } else return PortError; } return val; }