/* MTDemo07: Be a Modbus/RTU master to communicate with several Modbus/RTU slave devices. Compiler: BC++ 3.1 Compile mode: large Project: user.c v7000.c ..\LIB\7188EL.Lib ..\LIB\tcpip32.lib ..\LIB\VCOM_NNNN.Lib, with NNNN being the lib file's version. ..\LIB\MBTCPNNN.Lib, with NNN being the lib file's version. The ModbusRTU_Master() function can be used to controll Modbus/RTU slave devices through the serial port. Please refer to MBTCP.h for more information. Hardware: 7188E + Modbus/RTU slave device Refer 7188e\TCP\Doc\[Big5|Eng|Gb2312]\Vxcomm.htm 7188e\TCP\Xserver\Xserver.htm 7188e\TCP\Xserver\Function.htm to get more information. [Nov 13, 2009] first release by Liam [Sep,23,2011] by Nicholas */ #include #include "..\lib\7188e.h" #include "..\lib\Tcpip32.h" #include "..\lib\vxcomm.h" #include "..\lib\MBTCP.h" // Modbus/TCP Head file for i-7188EX int iScanCOMPort=1; // for Modbus Kernel int iScanProcess=0; int iSendFlag=0; int ScanUserProcess(int iProcess); //The I/O memory mapping unsigned char far iMemory_DI[100]; unsigned char far iMemory_DO[100]; int far iMemory_AI[100]; int far iMemory_AO[100]; 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()) } 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 vcom3004.lib] Syntax: Port10000=newport_10000; for calling UserCmd (user.c) Port9999=newport_9999; for calling VcomCmd7000 (v7000.c) PortUser=newport_User; for calling VcomCmdUser (user.c) [after vcom3005.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 demo9 & demo11 for example code */ //InitLib(); // InitModbus() already includes InitLib(); //======= Begin Modbus Kernel ======= int iRet; Set_COMEnableMode(1, _Programming); Set_COMEnableMode(2, _Programming); iRet=InitModbus(iMemory_DI, iMemory_DO, iMemory_AI, iMemory_AO); if(iRet!=0) { // Initial Modbus library error. } else { // Initial Modbus library ok. } //======= End Modbus Kernel ======= } unsigned long lStart_TimeTick; //To record when the 7188E/8000E do //Ethernet communication. void UserLoopFun(void) { //======= Begin of Modbus Kernel ======= if(mtModbusPort[iScanCOMPort].EnableMode==_ModbusRTU_Slave) CheckModbusRTURequest(iScanCOMPort); // Is any request from Modbus/RTU Master ? if(mtModbusPort[iScanCOMPort].EnableMode==_ModbusASCII_Slave) CheckModbusASCIIRequest(iScanCOMPort); // Is any request from Modbus/ASCII Master ? if(mtModbusPort[iScanCOMPort].EnableMode==_ModbusRTU_Gateway) { SendModbusRequest(iScanCOMPort); // Passes request to modbus slave device. CheckResponseTimeout(iScanCOMPort); // If response timeout, sets iModbusAction // to IDLE status. CheckModbusResponse(iScanCOMPort); // Is any response from modbus slave device? SendModbusResponse(iScanCOMPort); // Passes response to Modbus/RTU Master. } iScanCOMPort++; if(iScanCOMPort>iTotalCOMPort) iScanCOMPort=1; //======= End of Modbus Kernel ======= iScanProcess=ScanUserProcess(iScanProcess); } int ScanUserProcess(int iProcess) { int i,iRet,iNextProcess; iNextProcess=iProcess; if(mtModbusPort[2].EnableMode==_Programming && ((GetTimeTicks()-lStart_TimeTick)>200 || iSendFlag ) ) //Do communication every 200 ms. { lStart_TimeTick=GetTimeTicks(); switch(iProcess) { case 0: iRet=ModbusRTU_Master(2, 2, 2, 0, 0, 5, 450, 0); // COM port=2 // NetID=2 // Function Code = 2 -> Read DI // beginning index of register of 7188E is 0 // beginning index of register of Modub/RTU slave device is 0 // I/O channel = 5 // Timeout=450 ms // non-blocked communication mode if(iRet==0) { printCom1("[Modbus/RTU Slave #2]==> DI ok "); for(i=0; i<5; i++) printCom1("DI[%02X]=%d ", i, iMemory_DI[i]); printCom1("\r\n"); iSendFlag=0; iNextProcess=1; } else if(iRet>0 && iRet<256) { printCom1("error code return by Modbus/RTU device is %d\r\n", iRet); iSendFlag=0; iNextProcess=1; } else if(iRet<0) { printCom1("[Modbus/RTU Slave #2] Process[0]==> DI error code=%d\r\n", iRet); iSendFlag=0; iNextProcess=1; } else if(iRet==502 || iRet==2) { //printCom1("Wait for DI transaction response %d\n\r",iRet); iSendFlag=1; } else //for return value:500,501,503 { iSendFlag=0; iNextProcess=1; } break; case 1: iMemory_DO[0]=1; iMemory_DO[1]=0; iMemory_DO[2]=1; iMemory_DO[3]=0; iMemory_DO[4]=1; iRet=ModbusRTU_Master(2, 2, 15, 0, 0, 5, 450, 0); // COM port=2 // NetID=2 // Function Code = 15 -> Write DO // beginning index of register of 7188E is 0 // beginning index of register of Modub/RTU slave device is 0 // I/O channel = 5 // Timeout=450 ms // non-blocked communication mode if(iRet==0) { printCom1("[Modbus/RTU Slave #2]==> DO ok\r\n"); iSendFlag=0; iNextProcess=0; } else if(iRet>0 && iRet<256) { printCom1("error code return by Modbus/RTU device is %d\r\n", iRet); iSendFlag=0; iNextProcess=0; } else if(iRet<0) { printCom1("[Modbus/RTU Slave #2] Process[1]==> DO error code=%d\r\n", iRet); iSendFlag=0; iNextProcess=0; } else if(iRet==502 || iRet==2) { //printCom1("Wait for DO transaction response %d\n\r",iRet); iSendFlag=1; } else //for return value:500,501,503 { iSendFlag=0; iNextProcess=0; } break; default: iNextProcess=0; } } return iNextProcess; } int UserCmd(unsigned char *Cmd,unsigned char *Response) { sprintf(Response, "%s", Cmd); 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") 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); */ VcomSendSocket(p->Socket,"User-defined command(23)", 24); // return 24 bytes. 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 TCP port 10000, 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 } 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 Modbus_Request_Event(char* CommandData,int* CommandLength) { /* Modbus_Request_Event is supported since version 1.6.8 [2007,03,13]. char* CommandData: For Modbus/TCP, it includes 6 leading bytes. (needful) For Modbus/RTU, it includes 6 leading bytes. (needless) int* CommandLength: For Modbus/TCP, it includes 6 leading bytes. For Modbus/RTU, it includes 6 leading bytes. */ /* Example code */ //int i; //printCom1("FC:%2d StartAddress:%3d IOCount:%4d\n\r",iModbusRequest_Fun, iModbusRequest_Addr,iModbusRequest_IOCount); //printCom1("Modbus Request\n\r In==>"); //for(i=0;i<*CommandLength;i++) // printCom1("[%02X] ",CommandData[i]&0xFF); // do nothing CommandData=CommandData; CommandLength=CommandLength; } void Modbus_Response_Event(char* ResponseData,int* ResponseLength) { /* char* ResponseData: For Modbus/TCP, it includes 6 leading bytes. For Modbus/RTU, it doesn't include 6 leading bytes int* CommandLength: For Modbus/TCP, it includes 6 leading bytes. For Modbus/RTU, it doesn't include 6 leading bytes */ //If you want to change the content of the ResponseData, //you have to do 2 steps for Modbus/TCP or Modbus/RTU. //Step1: Change content (Note:you must know the modbus protocol well) //ResponseData[6]=0x19; //ResponseData[7]=0x75; //ResponseData[8]=0x04; //ResponseData[9]=0x01; //Step2: Update data length //*ResponseLength=10; //int i; //printCom1("\n\r Out==>"); //for(i=0;i<*ResponseLength;i++) // printCom1("[%02X] ",ResponseData[i]&0xFF); //printCom1("\n\r"); // do nothing ResponseData=ResponseData; ResponseLength=ResponseLength; }