/* XDemo20: Reads/Writes/Clears NVRAM. Compiler: BC++ 3.1 Compile mode: large Project: user.c v7000.c vModbus.c [after vcom3002.lib] ..\Lib\7188EL.Lib ..\Lib\TCPIP32.Lib ..\Lib\VcomNNNN.Lib, with NNNN being the lib file's version. 19ri, 19RI -> Read one Integer from NVRAM 19wi, 19WI -> Write one Integer to NVRAM 19rf, 19RF -> Read one Float from NVRAM 19wf, 19WF -> Write one Float to NVRAM 19rs, 19RS -> Read String from NVRAM 19ws, 19WS -> Write String to NVRAM 19c, 19C -> clear NVSRAM NVRAM's characteristic is short response time, limitless erase, battery backup for 10 years. Hardware: 7188E Refer 7188e\TCP\Doc\[Big5|Eng|Gb2312]\Vxcomm.htm 7188e\TCP\Xserver\Xserver.htm 7188e\TCP\Xserver\Function.htm to get more information. Because the kernal of Xserver used InstallCom() function. Please use printCom1() instead of Print() if you want to send formatted output from COM1. [20/Nov/2001] by Kevin [14,Sep,2004] modified by Annita [10/Aug/2005] by Liam [26,July,2011] by Nicholas */ #include #include #include #include "..\lib\7188e.h" #include "..\lib\tcpip32.h" #include "..\lib\vxcomm.h" void NVRAM_MultiRead(int iAddress, int iCount, char* cData) { /* iAddress: Begining address of NVRAM iCount: How many bytes of the data? cData: pointer to a char buffer to store data. */ int i; for(i=iAddress;i<(iAddress+iCount);i++) { *cData=ReadNVRAM(i); //Read data from NVRAM(Address) cData++; } } void NVRAM_MultiWrite(int iAddress, int iCount, char* cData) { /* iAddress: Begining address of NVRAM iCount: How many bytes of the data? cData: pointer to a char buffer to store data. */ int i; for(i=iAddress;i<(iAddress+iCount);i++) { WriteNVRAM(i,*cData); //Write data to NVRAM(Address,Data) cData++; } } 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(); } 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. */ char sData[32]; // NVRAM: 31 Bytes int i,iLen; int iData=0; float fData=0; if (Cmd[0]=='c' || Cmd[0]=='C') { for(i=0;i<=10;i++) WriteNVRAM(i,0); strcpy(Response,"Clear NVRAM ok."); } else if (Cmd[0]=='r' || Cmd[0]=='R') { switch(Cmd[1]) { case 'I': case 'i': //read one integer from NVRAM address 0. //one integer occupies 2 bytes. NVRAM_MultiRead(0,2,(char*)&iData); sprintf(Response,"Read NVRAM Integer:%d",iData); break; case 'F': case 'f': //read one float value from NVRAM address 2. //one float value occupies 4 bytes. NVRAM_MultiRead(2,4,(char*)&fData); sprintf(Response,"Read NVRAM Float:%f",fData); break; case 'S': case 's': //read string from NVRAM address 6~30. //add a terminate char('\0') to the end of string. NVRAM_MultiRead(6,25,&sData); sData[25]='\0'; sprintf(Response,"Read NVRAM String:%s",&sData); break; } } else if (Cmd[0]=='w' || Cmd[0]=='W') { switch(Cmd[1]) { case 'I': case 'i': sscanf(Cmd+2,"%d",&iData); //get an integer from 3rd byte of Cmd. NVRAM_MultiWrite(0,2,(char*)&iData); sprintf(Response,"Write %d to NVRAM ok!", iData); break; case 'F': case 'f': sscanf(Cmd+2,"%f",&fData); //get a float from 3rd byte of Cmd. NVRAM_MultiWrite(2,4,(char*)&fData); sprintf(Response,"Write %f to NVRAM ok!",fData); break; case 'S': case 's': sscanf(Cmd+2,"%s",&sData); //get a string from 3rd byte of Cmd. NVRAM_MultiWrite(6,strlen(sData),&sData); sprintf(Response,"Write [%s] to NVRAM ok.",sData); break; } } 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 }