/* DEMO98.c: Using the timer function to communicate with the I-7000 series modules via RS-485. Compiler: BC++ 3.1, Turbo C++ 1.01 (3.01), MSVC 1.52 Compile mode: Large Project: DEMO98.c ..\..\Lib\upac5000.lib Hardware: uPAC-5000 Note Setup the COM2(RS-485) as following: 9600, N, 8, 1 [Dec 21, 2011] by Liam */ #include #include #include "..\..\lib\upac5000.h" unsigned char Com2Buf[80]={0}; int Com2Idx=0; int fCom2HasDataIn=0; unsigned char Com4InBuf[80]={0}; int Com4Idx=0; int CheckSum=0; unsigned long Timeout=500; // default timeout=0.5 sec int bWait485Echo=0; int fTimeOut=0; void MonitorCom2(void) { unsigned char data; while(IsCom(2)) { if((data=ReadCom(2))=='\r') { Com2Buf[Com2Idx]=0; fCom2HasDataIn=1; break; } else Com2Buf[Com2Idx++]=data; } if(bWait485Echo && !fCom2HasDataIn) { unsigned long t; StopWatchReadValue(0, &t); if(t>Timeout) { fTimeOut=1; bWait485Echo=0; } } } void ToCom485(int port, unsigned char *buf, int checksum) { unsigned char sum=0; if(!(*buf)) return; while(*buf) { ToCom(port, *buf); if(checksum) sum+=*buf; buf++; } if(checksum) { ToCom(port, hex_to_ascii[sum>>4]); ToCom(port, hex_to_ascii[sum&0x0F]); } ToCom(port,'\r'); } void CheckCom2Data(int checksum) { unsigned char sum=0; int i; for(i=0; i>4) || ascii_to_hex(Com2Buf[i+1])!=(sum&0x0F)) { Print("!!! Checksum error !!! (%02X)-->%c%c", sum, Com2Buf[i], Com2Buf[i+1]); } } else { Putch(Com2Buf[i++]); Putch(Com2Buf[i++]); } Putch('\r'); Putch('\n'); Com2Idx=0; } void main(int argc, char *argv[]) { int quit=0; unsigned char data; long baud=9600L; InitLib(); /* InitLib() must be called before other functions in the library may be used */ if(argc>1) { baud=atol(argv[1]); if(baud<1200L || baud>115200L) baud=9600L; } Print("COM2: %ld, N, 8, 1\r\n", baud); InstallCom(2, baud, 8, 0, 1); TimerOpen(); InstallUserTimer(MonitorCom2); while(!quit) { if(Kbhit()) { switch(data=Getch()) { case 'q': quit=1; break; case 's': CheckSum=!CheckSum; Print("\n\rCheckSum=%d\r\n", CheckSum); break; case '\r': if(Com4Idx) { Com4InBuf[Com4Idx]=0; Com4Idx=0; } Putch('\r'); Putch('\n'); ToCom485(2, Com4InBuf, CheckSum); bWait485Echo=1; StopWatchStart(0); break; case '\b': if(Com4Idx) { Com4Idx--; Putch('\b'); } break; default: Com4InBuf[Com4Idx++]=data; Putch(data); break; } } if(fTimeOut) { fTimeOut=0; bWait485Echo=0; Print(" !!! Timeout !!! \r\n"); } if(fCom2HasDataIn) { CheckCom2Data(CheckSum); fCom2HasDataIn=0; bWait485Echo=0; } } TimerClose(); RestoreCom(2); }