#include #include #include #include #include #include "..\lib\7188.h" /* Demo50: Use 7188 COM2 link to 7060(4 D/I,4 D/O(Relay)),for Digital I/O. address 1 : 7060, connect to COM2(RS485) of 7188 9600,N,8,1 */ extern char hex_to_ascii[16]; int ascii_to_hex(char c); int SendCmdTo7000(int iPort, unsigned char *cCmd, int iChksum); int read_7000(int port, unsigned char *cmd); int ReceiveResponseFrom7000(int iPort, unsigned char *cCmd, long lTimeout, int iChksum); /* ------------------------------------------------------------------- */ unsigned char *TestCmd[]={ "$01M", /* "~010", "~011", "~011", "~012", "~014P", "~014S", "~015P", "~015S", "@01A", "@01", "$01L0", "$01F", "$016", "$015", "$014", "$012", */ "@011", "@01", "@012", "@01", "@013", "@01", "@014", "@01", "@015", "@01", "@016", "@01", "@017", "@01", "@018", "@01", "@019", "@01", "@01A", "@01", "@01B", "@01", "@01C", "@01", "@01D", "@01", "@01E", "@01", "@01F", "@01", "@010", "@01", }; int CmdNo=sizeof(TestCmd)/sizeof(unsigned char *); void main(int argc,char *argv[]) { int i,k,testcount=0,count=0; unsigned char c,cmd[80]; int ledmode=1; int quit=0; int CommandMode=1; clock_t start,end,start2; long baudrate=9600L; if(argc>1){ baudrate=atol(argv[1]); if(baudrate <300) baudrate=9600L; } InitLib(); /* driver initial */ InstallCom(2,baudrate,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(); Show5DigitLed(2,7); Show5DigitLed(3,1); Show5DigitLed(4,8); Show5DigitLed(5,8); read_7000(2,"$01M"); /* read module name of address 1 */ /* 7060 will echo !017060 */ printf("\ninput 'c':command mode,'a':auto test mode,'q':quit"); printf("\n7188_demo50>"); ungetch('a'); for (;!quit;) { end=clock(); if((end-start)/CLK_TCK >= 0.5){ ledmode=!ledmode; if (ledmode) LedOn(); else LedOff(); start=clock(); } if(CommandMode){ if (kbhit()) { /* is user press PC's keyboard from COM4 */ c=getch(); /* if this key is Q or Q --> exit this program */ if ((c=='q') || (c=='Q')) quit=1; else { if(c=='\r'){ /* 0x0d */ if(count){ cmd[count]=0; } /* else repeat last command */ read_7000(2,cmd); count=0; printf("\n7188_demo50>"); } else if(c=='\b'){ /* backspace */ if(count){ count--; putchar(c); } } else if(c=='a'){ CommandMode=0; /* change to automode */ count=0; start2=clock(); } else { putchar(c); cmd[count++]=c; } } } } else { end=clock(); if((end-start2)/CLK_TCK >= 0.3){ read_7000(2,TestCmd[count]); Show5DigitLed(1,testcount); testcount++; testcount%=17; start2=clock(); count++; if(count>=CmdNo) { count=0; } if(kbhit()){ switch(getch()){ case 'q': quit=1; break; case 'c': CommandMode=1; count=0; printf("\n7188_demo50>"); break; } } } } } RestoreCom(2); return; } /* ------------------------------------------------------------------- */ /* send cmd & receive result to 7000 module */ int read_7000(int port, unsigned char *cmd) { unsigned char str[20]; int h,l,val,len; SendCmdTo7000(port, cmd, 0); printf("\nSend=%s, Receive=",cmd); val=ReceiveResponseFrom7000(port, str, 60000L, 0); if (val==NoError) { printf("%s",str); if(!strcmp(cmd,"@01")){ /* show the I/O value */ Show5DigitLed(2,ascii_to_hex(str[1])); Show5DigitLed(3,ascii_to_hex(str[2])); Show5DigitLed(4,ascii_to_hex(str[3])); Show5DigitLed(5,ascii_to_hex(str[4])); } return(0); } printf("(Error3)"); return(-1); }