// XDemo47: D/I from 87K modules (8, or 16 DI channels) // 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. // 190 DI 16 -> Reads 16 DI channels from DI modules plugged in slot0 // 191 DI 8 -> Reads 8 DI channels from DI modules plugged in slot1 // 192 DIO 8 -> Reads 8 DI channels from DIO modules plugged in slot2 // D/I from 87K series DI or DIO modules on 8000 slots. // Hardware: 8000E + 87K DI or DIO modules // 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. // [16/Jan/2002] by Kevin #include #include #include "..\lib\module.h" 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 } 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 InstallCom0(115200,8,0,1); //SetBaudrate(0,115200L); } void UserLoopFun(void) { // VxComm.exe will call this function every scan time // refer to demo11 for scan-time evaluation } int UserCmd(unsigned char *Cmd,unsigned char *Response) { // user's command interpreter // refer to all demo int iSlot,iChannel,iValue,iRet; char sType[5]; unsigned char InBufCom0[20]; //unsigned char OutBufCom0[20]; if(Cmd[0]) // Not Null command { sscanf(Cmd,"%d %s %d",&iSlot,&sType,&iChannel); strupr(sType); //transfer sType to upper case //Build command string. //Please refer to 8000\87k\*.* for the command protocol. //sprintf(OutBufCom0,"$006"); ChangeToSlot(iSlot); //Change to specified slot iRet=SendCmdTo7000(0,"$006",0); //To COM 0, checksum disable if(iRet==NoError) { iRet=ReceiveResponseFrom7000(0,InBufCom0,1000,0); //From COM 0, timeout=1000 ms, checksum disable if(iRet==NoError) switch(iChannel) { case 8: // 8 DI channels if(!strcmp(sType,"DIO")) //DI from DIO modules //Response="!(Data0 Data1 Data2 Data3 Data4 Data5)" //Data0 and Data1 are for DO //Data2 and Data3 are for DI iValue=((ascii_to_hex(InBufCom0[3])<<4)&0xF0)+(ascii_to_hex(InBufCom0[4])&0xF); else if(!strcmp(sType,"DI")) //DI from DI modules //Response="!(Data0 Data1 Data2 Data3 Data4 Data5)" //Data0 and Data1 are for DI iValue=((ascii_to_hex(InBufCom0[1])<<4)&0xF0)+(ascii_to_hex(InBufCom0[2])&0xF); sprintf(Response,"Value= %02X(Hex)",iValue); break; case 16://16 DI channels if(!strcmp(sType,"DI") || !strcmp(sType,"DIO")) //Response="!(Data)" //Response="!(Data0 Data1 Data2 Data3 Data4 Data5)" //Data0 and Data1 are for Hi byte of DI //Data2 and Data3 are for Low byte of DI iValue=((ascii_to_hex(InBufCom0[1])<<12)&0xF000)+((ascii_to_hex(InBufCom0[2])<<8)&0xF00) +((ascii_to_hex(InBufCom0[3])<<4)&0xF0)+(ascii_to_hex(InBufCom0[4])&0xF); sprintf(Response,"Value= %04X(Hex)",iValue); break; } else strcpy(Response,"Input0 failure!"); } else sprintf(Response,"Input1 failure!"); return 1; // return OK } return 0; // return ERROR } 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 to return a message to the client, just use the socket to send data. use: 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 */ } 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 }