/* program: ECHO485.C use : echo485 [baudrate] (1) must run on 7188 (2) default baudrate is 9600 function description: (1) echo any data on the 7188's COM2(RS485) to COM4. (2) data come from COM4(end with CR(0x0d)) will be send to COM2(RS485)(data in will be buffered,until receive 0x0d, 7188 will send out the buffered data.(if checksum enable, will append checksum(2 bytes) after the data,before 0x0d.) (3) press 'q'(not 'Q') to quit. (4) press 's'(not 'S') to toggle checksum mode. so 'q' and 's' will not be send to 485. (5) default mode is checksum enable. (6) protocal is 1 start bit,8 data bit,none parity,1 stop bit. (7) *bBAUD to change baudrate [PC]<---------->[7188-0]<------------------+----->[7000's] RS232 (COM4) (COM2) RS485 | [7188-1]<------------------+ (COM2/COM1) RS485 NOTE: Use echo485 to (1)monitor data on RS485,(2)send command to 7000's [10/11/1999] Ver.1.03 change putchar() to putch4() [10/12/1999] Ver.1.10 Use new library(ver 1.15 or latter) function SetCom2AutoDir(); After call SetCom2AutoDir(), ToCom2() will set the direction of RS-485 bus to TRANSMIT mode, and when all data is send out, the COM2 ISR will change the direction of RS-485 to RECEIVE mode. [10/18/1999] Ver.1.11 Use new library function: SetBaud1() to change baudrate. */ #include #include #include #include"..\lib\7188.h" unsigned no; unsigned char buf[80]; unsigned char OutBuf[80]; int idx=0; int outidx=0; main(int argc,char *argv[]) { long baud=9600,newbaud; int quit=0; int data; int i; int checksum=0; int fSearch=0; int Addr; unsigned wait=0; if(!Is7188()){ puts("\necho485 must run on 7188!"); return; } if(argc>1){ baud=atol(argv[1]); if(baud<=0) baud=9600; } printf("\nEcho485 Ver. 1.10" "\nUse COM2(RS485) connect to 7000's" "\necho485 [baudrate] --> default:9600" "\n's' to toggle checksum ON(1)/OFF(0)" "\nCTRL_S to search 7000's modules *** NEW ***" "\n*bBAUD to change baudrate *** NEW ***" "\n'q' to quit to ROM-DOS" "\nCurrent mode:baudrate=%ld,checksum=%d\n",baud,checksum); InstallCom2(baud,8,0,1); Init5DigitLed(); Show5DigitLed(1,15); Show5DigitLed(2,12); Show5DigitLed(3,4); Show5DigitLed(4,8); Show5DigitLed(5,5); OutBuf[0]=0; SetCom2AutoDir(); /* set to AutoDir mode. */ while(!quit){ if(fSearch){ if(!wait){ sprintf(OutBuf,"$%02XM",Addr); outidx=4; wait=1; printf("\r%02X",Addr); goto SendCmd; } else { wait++; if(wait>=1000) { wait=0; Addr++; if(Addr>=256){ fSearch=0; printf("\n"); } } } } if(kbhit4()){ data=getch4(); if(data=='q') quit=1; else if(data==19){ /* CTRL_S */ fSearch=1; Addr=0; continue; } else if(data=='s'){ checksum=!checksum; printf("CheckSum="); putch4('0'+checksum); printf("\n"); } else if(data=='\b'){ if(outidx){ outidx--; putch4(data); } } else { putch4(data); if(data=='\r'){ unsigned char sum; putch4('\n'); if(outidx) OutBuf[outidx]=0; if(OutBuf[0]=='*'){ switch(OutBuf[1]){ case 'b': newbaud=atol(OutBuf+2); if(NoError==SetBaudrate2(newbaud)) { baud=newbaud; printf("Change baudrate to %ld\n",baud); } break; } outidx=0; } else { SendCmd: sum=0; /* *** Need not set direction to transmit. Set485DirToTransmit(2); */ for(i=0;i>4]); ToCom2(hex_to_ascii[sum&0xf]); } ToCom2('\r'); outidx=0; /* *** Need not wait transmit over and then set direction to receive WaitTransmitOver2(); Set485DirToReceive(2); */ } } else { OutBuf[outidx++]=data; } } } if(IsCom2()){ data=ReadCom2(); putch4(data); if(data=='\r'){ putch4('\n'); buf[idx]=0; if(checksum){ unsigned char sum=0; int i; for(i=0;i>4] || buf[i+1] != hex_to_ascii[sum&0x0f]) { printf("Check sum error,must be %02X",sum); } } idx=0; if(wait){ printf("wait=%u\n",wait); wait=0; Addr++; if(Addr>=256){ fSearch=0; printf("\n"); } } } else { buf[idx++]=data; if(idx>=79) idx=78; } } } RestoreCom2(); }