/* MBDemo00: Modbus/RTU controller protoltype Compiler: BC++ 3.1 Compile mode: large Project: MBDemo00.c ..\Lib\7188XBL.Lib ..\Lib\MBR7Bnnn.Lib, with nnn being the version of Modbus/RTU library. Communication: Modbus Master <= Modbus/RTU => COM1 of 7188XB (the Uplink port can be changed) <==> [Converter Kernel] <==> COM2 of 7188XB <==> Modbus/RTU slave device This program has 3 main functions 1. 1 to N port Modbus/RTU converter One COM port is used to receive Modbus/Request from Modbus/RTU master. Other COM ports are used to link to Modbus/RTU slave devices. When gets Modbus/RTU request, the kernel can decide to send request to which COM port and return the response to the Modbus/RTU master. 2. X board supports Modbus/RTU protocol Modbus/RTU master can send request to specific NetID(station number) to access the X board that mount on io expansion bus. 3. Supports user-defined registers The internal registers are open for X board and user-defined process. You can use the internal register to store some special information. Thus, the Modbus/RTU master can access the special internal registers. Hardware: 7188XB [2006,Feb,17] by Kevin Use new I/O memory mapping method. */ #include "..\Lib\7188xb.h" #include "..\Lib\MBRTU.h" //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]; int main(void) { int iRet; int iScanCOMPort=1; InitLib(); //To use XB library version 2.0 (after 2004/Mar/08). // ***** Begin changing COM port setting ****** // Call following codes once when you change the COM port setting. // After saving the setting to the EEPROM, you should remove the codes. // Save COM1 setting to EEPROM ComData[1-1].baud=115200; ComData[1-1].databit=8; ComData[1-1].parity=0; ComData[1-1].stopbit=1; VcomSaveComData(1-1); // Save COM2 setting to EEPROM ComData[2-1].baud=115200; ComData[2-1].databit=8; ComData[2-1].parity=0; ComData[2-1].stopbit=1; VcomSaveComData(2-1); // ***** End changing COM port setting ****** // ***** Begin Modbus configuration ***** // Before the Modbus Utility can support the 7188XB -MRTU, // You can add following functions to configure the Modbus settings. Set_NetID(1); Set_StationCountPerCOMPort(10); Set_COMEnableMode(1,_ModbusRTU_Slave); //(COM1 be a Modbus/RTU slave port) Set_COMEnableMode(2,_ModbusASCII_Slave); //(COM1 be a Modbus/ASCII slave port) // **** End Modbus configuration ***** iRet=InitModbus(iMemory_DI,iMemory_DO,iMemory_AI,iMemory_AO); if(iRet) return iRet; // Put values to internal registers at initial. iMemory_AI[0]=0x7188; iMemory_AI[1]=0x4113; iMemory_AO[0]=0x1234; iMemory_AO[1]=0x5678; iMemory_DI[0]=12; // all non-zero values make DI high iMemory_DI[1]=1; // all non-zero values make DI high iMemory_DI[3]=0; // only 0 make DI low iMemory_DO[0]=1; // all non-zero values make DO hig iMemory_DO[1]=56; // all non-zero values make DO high iMemory_DO[2]=0; // only 0 make DO low for(;;) { // ******** Begin of the 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 ? iScanCOMPort++; if(iScanCOMPort>iTotalCOMPort) iScanCOMPort=1; // ******** End of the Modbus Kernel ******* } } 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); } 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"); }