/* DEMO4: send/receive command to Xserver File name: user.c Compile mode: large Last time update: 02/19/2002 By: Tim */ #include #include #include "module1.h" static long lUserCnt=0; void UserCount(void) { /* // user's timer trigger function // // In this function cannot use any function that will use the hardware signal "clock", // Such as: // 1. ClockHigh(),ClockLow(), ClockHighLow(), // 2. Any EEPROM functions. // 3. Any 5DigitLed functions. // 4. Any NVRAM function. // 5. Any RTC function.(GetTime(),SetTime(),GetDate(),SetDate()) // // refer to demo9 for example code */ lUserCnt++; } /* */ extern COMPORT MyCom; extern int Com87K; extern long Baud87K; extern int DataBit87K; extern int Parity87K; extern int Stop87K; void Com0_LoopFun(void); int MyReadComn_0(unsigned char *buf,int n); void UserEnd(void) { XS_RemoveComPort(&MyCom); RestoreCom_0(); Print("Call UserEnd().\r\n"); StopUserTimerFun(); // DisableMonitorCom4(); } void UserInit(void) { /* In this function CAN DO: 1. user's initial function 2. timer initialized for UserCount() 3. I/O or variables initialized for UserLoopFun() 4. I/O or variables initialized for User's functions in this file 5. change the default TCP PORT 10000/9999/502 to others value by: Port10000=newport_10000; Port9999=newport_9999; Port502=newport_502; Please refer to demo9 & demo11 for example code [02/19/2002] add PortUser 6. PortUser=newport_User; (by default PortUser=0.) *** If Port9999/Port502/PortUser=0, The main program will not listen these port. *** */ UserProgramName="Vcom3 DEMO7"; UserProgramVersion="V1.0[" __DATE__ "]"; InstallUserTimerFunction_ms(1,UserCount); /* 底下是使用 COM0 有關的設定(有關程式碼在 COM_0.c) */ InstallCom_DMA_0(Baud87K,DataBit87K,Parity87K,Stop87K); MyCom.ReadComn=ReadComn_0; //COM0_DMA XS_AddComPort(&MyCom); XS_AddSystemLoopFun(Com0_LoopFun); } void UserLoopFun(void) { /* // VxComm.exe will call this function in every scan time // refer to demo11 for scan-time evaluation */ static int LastStatus=9999; int status; status=ReadInitPin(); if(status != LastStatus){ LastStatus=status; if(LastStatus){ EnableCom(2); EnableCom(3); printCom_1("Enable COM2/3 OK\r\n"); //printCom_1("[init=%d]\r\n",LastStatus); } else { DisableCom(2); DisableCom(3); printCom_1("Disable COM2/3 OK\r\n"); } } } int UserCmd(unsigned char *Cmd,unsigned char *Response) { /* // user's command interpreter // refer to all demo */ // _dPrint("%s",Cmd); if (Cmd[0]){ /* Not Null command */ if(Cmd[0]=='0') { DisableCom(2); DisableCom(3); strcpy(Response,"Disable COM2/3 OK"); } else if(Cmd[0]=='1') { EnableCom(2); EnableCom(3); strcpy(Response,"Enable COM2/3 OK"); } else { long tmpl; _asm cli tmpl=lUserCnt; _asm sti sprintf(Response,"Current UserCnt=%lu\r\n",tmpl); } return 1; /* return OK */ } return 0; /* return ERROR */ } /* typedef struct t_TcpReadData{ int Comport; int Socket; int Length; char* ReadUartChar; }TCPREADDATA; */ int VcomSendSocket(int skt,char *data,int cnt); 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") p->Length : the command data length(include the two byte "23") p->Socket : the socket number that receive the command, that is when the user function want return message to the client, just use the socket to send data. use: VcomSendSocket(p->Socket,pdata,datalength); */ /* just echo back*/ VcomSendSocket(p->Socket,p->ReadUartChar,p->Length); } int VcomCmdUser(TCPREADDATA *p) { /* VCOM3005 or later will call this function for PortUser. when socket from TCP PORT PortUser will call this function. user can get the following message: p->ReadUartChar : the buffer store the command data. p->Length : the command data length. p->Socket : the socket number that receive the command, that is when the user function want return message to the client, just use the socket to send data. use: VcomSendSocket(p->Socket,pdata,datalength); */ /* here just send back the command come from PortUser. */ VcomSendSocket(p->Socket,p->ReadUartChar,p->Length); } void Port9999Start(int skt) { TcpPrint(skt,1,"Welcome connect to Port %u\r\n",Port9999); } void Port502Start(int skt) { TcpPrint(skt,1,"Welcome connect to Port %u\r\n",Port502); } void PortUserStart(int skt) { TcpPrint(skt,1,"Welcome connect to Port %u\r\n",PortUser); }