// XDemo28: Reads information from text files(basic) // Compiler: BC++ 3.1 // Compile mode: large // Project: user.c // v7000.c // vModbus.c [after XS8_3002] // ..\LIB\8000E.Lib // ..\LIB\TCPIPL.Lib // ..\LIB\XS8_NNNN.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 application, 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.dat) 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 demo28 // ================================================= // 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........... // // Hardware: 8000E // // Note: to run this demo, you must load one extra text file // (I wrote config28.ini for this demo) into 8000E. // // Refer 8000\843x883x\TCP\Doc\[Big5|Eng|Gb2312]\Vxcomm.htm // 8000\843x883x\TCP\Xserver\Xserver.htm // 8000\843x883x\TCP\Xserver\Function.htm // to get more information. // // [2/Feb/2002] by Kevin #include #include #include "..\lib\module.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) { } void UserEnd(void) { Print("Call UserEnd().\r\n"); StopUserTimerFun(); } void UserInit(void) { /* In this function, user CAN: 1. initialize user's program. 2. set time interval for calling UserCount(). 3. set initial value of I/O or variables for UserLoopFun(). 4. set initial value of I/O or variables for another functions. 5. change the default TCP PORT 10000/9999/502 to others value. [after XS8_3004.lib] Syntax: Port10000=newport_10000; for calling UserCmd (user.c) Port9999=newport_9999; for calling VcomCmd7000 (v7000.c) Port502=newport_502; for calling VcomCmdModbus (vModbus.c) [after XS8_3002.lib] PortUser=newport_User; for calling VcomCmdUser (user.c) [after XS8_3005.lib] Default port value: Port10000=10000; Port9999=9999; Port502=502; PortUser=0; If the port value is 0, Xserver will not listen that port. That means the port will be disable. Please refer to XDemo09 & XDemo11 for example code */ InitLib(); // To use [MiniOS7 for 8000 new], you need call this function // at begin of c program. // XS8_nnnn.Lib has not add this function to it's kernel. // So, you need call that in UserInit. // To know what difference between original MiniOS7 and new MiniOS7, // please refer CD:\Napdos\MiniOS7\8000new\8000-new_eng.txt // 8000-new_big5.txt // 8000-new_gb2312.txt // [26,Oct,2002] by Kevin config=GetFileInfoByName("config28.ini"); //Opens one file by name. SetBaudrate(1, 115200L); 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) { } int UserCmd(unsigned char *Cmd,unsigned char *Response) { // user's command interpreter // Maximum length of Cmd is 1458 bytes. // Maximum length of Response is 1024 bytes. 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(09/04/2001) or later will support this function. TCP PORT 10000, command 23 will call this function. user can get the following message: p->ReadUartChar : the buffer store the command data(include "23") Maximum length of p->ReadUartChar is 32767 bytes. p->Length : the command data length(include the two byte "23") p->Socket : the socket number that receives the command, that is, when the user function wants to return a message to the client, just use the socket to send data. usage: VcomSendSocket(p->Socket,pdata,datalength); */ VcomSendSocket(p->Socket,"User-defined command(23)",24); // return 24 bytes. 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. user can get the following message: p->ReadUartChar : the buffer store the command data. Maximum length of p->ReadUartChar is 32767 bytes. p->Length : the command data length. p->Socket : the socket number that receive the command, that is when the user function wants return message to the client, just use the socket to send data. usage: VcomSendSocket(p->Socket,pdata,datalength); */ /* here just send back the command to the client. */ VcomSendSocket(p->Socket,p->ReadUartChar,p->Length); return 1; /* any value will be accept */ } 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; } 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. You can use function VcomSendSocket to send a message to the client when a connection is established. For example: VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes. skt: socket number assigned to the TCP/IP client. */ 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. You can use function VcomSendSocket to send a message to the client when a connection is established. For example: VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes. skt: socket number assigned to the TCP/IP client. */ 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. You can use function VcomSendSocket to send a message to the client when a connection is established. For example: VcomSendSocket(skt,"Connection is established.",26); //return 26 bytes. skt: socket number assigned to the TCP/IP client. */ skt=skt; //do nothing }