/* XDemo28: Reads information from text files(basic) Compiler: BC++ 3.1 Compile mode: large Project: user.c v7000.c vModbus.c [after vcom3002.lib] ..\Lib\7186EL.Lib ..\Lib\tcp_dm32.lib ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version. 191 -> Reads data from the 1st * in the text file(config.dat). 192 -> Reads data from the 2st * in the text file(config.dat). 193 -> Reads data from the 3th * in the text file(config.dat). In many applications, users need one text file to record some information. And the program can read it. You can use FSeek to get specific information in text files. The format of the text file (config.ini) must follow the rules: 1. The item information you want to read must be positioned after '*'. 2. The comment cann't contain any '*'. 3. KeyName and '=' must be seperated by least one space character. 4. If the setting of KeyName is string type, you must add t least one space character to the end of file(EOF). For example: ================================================= config file sample for Xdemo28 ================================================= Star Item1 Item2 Item3 Item4 Comment ================================================= * 1st 19200 on 1.25 ;comment1 * 2nd 9600 off -1.3 ;comment2 * 3th 2400 off 19.34 ;comment3 = end of settings = ================================================= comment........... Hareware: uPAC-7186EX Note: to run this demo, you must load one extra text file (I wrote config28.ini for this demo) into 7188E. [Dec 18, 2008] by Liam */ #include #include #include "..\lib\7186e.h" #include "..\lib\tcpip32.h" #include "..\lib\vxcomm.h" static FILE_DATA far *config; unsigned long FSeek(FILE_DATA far *file_pointer,char cMark,unsigned long lStart,int iTh); /* search the cMark from lStart until reach the iTh(th) cMark, and return the position next to the cMark. */ void UserCount(void) { /* User's timer trigger function. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ } void UserInit(void) { /* Initialize user's program. Please refer to XDemo04 for detail description. Please refer to XDemo09 for example code. */ InitLib(); SetBaudrate(1, 115200L); config=GetFileInfoByName("config28.ini"); //Opens one file by name. printCom1("File size=%lu bytes\n\r",config->size); printCom1(" day=%02d\n\r",config->day); printCom1(" month=%02d\n\r",config->month); printCom1(" year=%02d\n\r",config->year+1980); printCom1(" hour=%02d\n\r",config->hour); printCom1(" minute=%02d\n\r",config->minute); printCom1(" second=%02d\n\r",config->sec*2); } void UserLoopFun(void) { /* VxComm.exe will call this function every scan time Please refer to XDemo11 for Real-time I/O control */ } int UserCmd(unsigned char *Cmd,unsigned char *Response) { /* Xserver executes this function when received a package form TCP port 10000 and the first two bytes are "19". Funtion of Xserver, Please refer to XDemo04 for detail description. */ unsigned long lPointer=0; int iTh; //iThe: to find information from ?th * in the text file. char sItem1[20]; char sItem2[20]; char sItem3[20]; char sItem4[20]; if (Cmd[0]) /* Not Null command */ { sscanf(Cmd,"%d",&iTh); lPointer=FSeek(config,'*',0,iTh); //Gets the position next //to the iTh * in the text file. //0: from the start position of the file // to find the *. /* Return value of FSeek >0: find the iTh '*' =0: cannot find the iTh '*' */ if(lPointer) // Finds the iTh '*'. { // Reads 4 Items (string type) in config.dat from the iTh * sscanf(config->addr+lPointer,"%s %s %s %s", &sItem1,&sItem2,&sItem3,&sItem4); sprintf(Response,"Item1=%s Item2=%s Item3=%s Item4=%s", sItem1,sItem2,sItem3,sItem4); } else strcpy(Response,"Cannot find!"); return 1; /* return OK */ } strcpy(Response,"Syntax Error!"); return 1; /* return OK */ } int VcomUserBinaryCmd(TCPREADDATA *p) { /* VXCOMM.EXE 2.6.12(04,Sep,2001) or later will support this function. Xserver executes this function when received a package form TCP port 10000 and the first two bytes are "23". Please refer to XDemo04 for detail description. Please refer to XDemo23 for example code. */ return 1; /* any value will be accept */ } int VcomCmdUser(TCPREADDATA *p) { /* VCOM3005 (Feb,22,2002) or later will call this function for PortUser. When packets received by TCP PORT PortUser(user defined) of 7188E/8000E, Xserver will call this function. Please refer to XDemo04 for detail description. */ VcomSendSocket(p->Socket,p->ReadUartChar,p->Length); return 1; /* any value will be accept */ } void PortUserStart(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E via the user's defined port(PortUser), the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; //do nothing } void Port9999Start(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E TCP port 9999, the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; //do nothing } void Port502Start(int skt) { /* XS8_3200.Lib Version 3.2.00 (20,Apr,2004) or later version supports this function. When a TCP/IP client connects to the 7188E/8000E TCP port 502, the Xserver calls the function once. Please refer to XDemo04 for detail description. */ skt=skt; //do nothing } unsigned long FSeek(FILE_DATA far *file_pointer,char cMark,unsigned long lStart,int iTh) { /* This function seeks the mark letter from the start position to the EOF(end of file). cMark: find the position next to the cMark. lStart: the start position to seek the cMark. (0: start position of the file) iTh: seek the iTh(th) cMark. Return value: 0: cannot find the cMark >0: the offset position next to the cMark */ /* typedef struct { unsigned mark; unsigned char fname[12]; unsigned char year; unsigned char month; unsigned char day; unsigned char hour; unsigned char minute; unsigned char sec; unsigned long size; char far *addr; unsigned CRC; unsigned CRC32; } FILE_DATA; */ char c; unsigned long i; int iOrder; i=lStart; iOrder=1; while(i<(file_pointer->size-1)) { c=file_pointer->addr[i]; if(c==cMark) if(iOrder==iTh) return ++i; // return the position next to the cMark. else iOrder++; i++; } return 0; }