/* Receive.c : Receive COM port Compiler: BC++ 3.1, Turbo C ++ 1.01(3.01) (free from http://community.borland.com/museum) MSC 6.0, MSVC 1.52. Compile mode: large Project: Receive.c ..\..\Lib\(8000E.Lib,7188XAL.Lib,7188XBL.Lib,7188XCL.Lib or 7188EL.Lib) Detail description: Slv_COM.c and Receive.c are a little diffent. Slv_COM.c is non-blocked mode, Receive.c is blocked mode. Hardware: 7188/8000 [May,13,2005] by Kevin */ #include #include #include "..\..\lib\8000a.h" /* you must change this lib to suit the hardware that you want to use. */ int iIndex=0; int returnflag=0; int Receive_Data(int iPort,unsigned char* cInBuf,unsigned long lTimeout); // Uses COM port to receive data with 0x0D [Enter]. void main(void) { int iLength,iValue; //int iCommandCOMPort; float fValue; char cData[10]; char buf[10]; char test[10]; unsigned long lTimeout1; int i,j; InitLib(); //iCommandCOMPort=1; //InstallCom(iCommandCOMPort,115200,8,0,1); InstallCom(3,115200,8,0,1); Print("Input a timeout value:"); //sscanf(cData,"%d",&iValue); //sscanf(buf,"%s"); LineInput(test,10); sscanf(test,"%lu",&lTimeout1); Print("Set timeout as %lu ms\n\r",lTimeout1); for (;;) { j=0; i=0; //Print("send a str to COM 3:"); //sscanf(cData,"%d",&iValue); //sscanf(buf,"%s"); //LineInput(test,10); //scanf("%s\r",&buf); for (;;) { if (Kbhit()) { i=Getch(); buf[j++]=i; //Print ("buf= %s , i= %c j= %d",buf,i,j); //Print ("Y"); if ((i=='\r') ) { buf[j]=0; break; //returnflag=1; } } } ClearCom3(); ToComBufn(3,buf,j-1); //Print("send str '%s' ok,and wait to get response until timeout ",buf); //ToComStr(3,"Input a value333(integer):"); iLength=Receive_Data(3,cData,lTimeout1); /* timeout=10 seconds. */ if(iIndex==0) { Print("\n\r timeout = %lu ms ,COM3 get no response \n\r",lTimeout1); //printCom(3,"\n\rValue=%d\n\r",iValue); } else { Print("\n\r timeout = %lu ms ,COM3 get response, str= %s \n\r",lTimeout1,cData); //Print("Keyin timeout!\n\r"); } if (returnflag==1) return; } /* Print("\n\rInput a value(float):"); iLength=Receive_Data(iCommandCOMPort,cData,10000); if(iLength>0) { sscanf(cData,"%f",&fValue); Print("\n\rValue=%f\n\r",fValue); } else { Print("Keyin timeout!\n\r"); } */ //Delay(10); //Wait for all data is transmited to COM port. //RestoreCom(iCommandCOMPort); } /*=====================================================================*/ /*==== Following functions are used to receive data from COM port. ===*/ /*==== You can copy the functions to your own program. ===*/ /*=====================================================================*/ /* The following two functions are implemented by [block] method. Block method: After calling the function, the CPU will be blocked in the function till it gets the 0x0D [Enter] or timeout. */ int Receive_Data(int iPort,unsigned char* cInBuf,unsigned long lTimeout) { int i,j; /* Uses COM port to receive data with a terminative char. iPort: COM port number to receive data. 0:COM0, 1:COM1, 2:COM2 ..... *cInBuf: Input buffer to receive data. cTerminator: what is the last byte ? lTimeout: timeout to receive data. (Unit: ms) The timeout is measured from last received byte to the terminator. return: >0 :length of received data 0 :doen't receive any data -1 :timeout */ unsigned char cChar; unsigned long lStartTime; iIndex=0; lStartTime=GetTimeTicks(); //Print ("\n\r Press 'Q' or 'q' to exit program "); for(;;) { if (Kbhit()) { i=Getch(); //Print ("Y"); if ((i=='q') || (i=='Q') ) { returnflag=1; } } while(IsCom(iPort)) //check COM port { cChar=ReadCom(iPort); cInBuf[iIndex++]=cChar; //Print (" get str from COM3 =%s",cInBuf); lStartTime=GetTimeTicks(); /* refresh data timeout */ } //Print("\r timetick = %lu ",(GetTimeTicks()-lStartTime)); if((GetTimeTicks()-lStartTime)>=lTimeout) { if (iIndex !=0) { if (cInBuf[iIndex-1]=='\r') { Print ("\n\r Get response form COM3 string length= %d \n\r",iIndex-1); //cInBuf[iIndex]=0; for (j=0;j0 :length of received data 0 :doen't receive any data -1 :timeout */ unsigned char cChar; int iIndex=0; unsigned long lStartTime; lStartTime=GetTimeTicks(); iIndex=0; for(;;) { while(IsCom(iPort)) //check COM port { cInBuf[iIndex++]=ReadCom(iPort); if(iIndex>=iLength) { cInBuf[iIndex]=0; return iIndex; /* return data length */ } lStartTime=GetTimeTicks(); /* refresh data timeout */ } if((GetTimeTicks()-lStartTime)>=lTimeout) return -1; /* receive data timeout */ RefreshWDT(); } }