/* MBRTU_M.c: Modbus RTU Master demo Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) (free from http://cc.codegear.com/free/cpp) Compile mode: Large Project: MBRTU_M.c lib\G4500.lib lib\G45MBRTU.lib Hardware: G-4500-2G [Dec 26, 2014] by William */ #include "lib\G4500.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 i, iRet, iSettingCom, tmp; //-- Initial G-4500 library InitLib(); //-- 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 COM2 setting to EEPROM (for RS-485 port) iSettingCom = 2; ComData[iSettingCom-1].baud = 9600; ComData[iSettingCom-1].databit = 8; ComData[iSettingCom-1].parity = 0; ComData[iSettingCom-1].stopbit = 1; VcomSaveComData(iSettingCom-1); //-- Modbus configuration Set_COMEnableMode(2, _Programming); //-- Initial Modbus library iRet=InitModbus(iMemory_DI, iMemory_DO, iMemory_AI, iMemory_AO); if(iRet!=0) { // Initial Modbus library error. } else { // Initial Modbus library ok. while(1) { // press "ESC" to exit if(Kbhit()) { tmp = Getch(); if( tmp == 27 || tmp == 'q') break; } iRet=ModbusRTU_Master(2, 1, 2, 0, 0, 5, 200, 1); // COM port = 2 // NetID = 1 // Function Code = 2 -> Read DI // beginning index of register of G-4513 is 0 // beginning index of register of Modub/RTU slave device is 0 // I/O channel = 5 // Timeout = 500 ms // blocked communication mode if(iRet==0) { Print("[Modbus/RTU Slave]==> DI ok "); for(i=0; i<5; i++) Print("DI[%02X]=%d ", i, iMemory_DI[i]); Print("\r\n"); } else if(iRet>0 && iRet<256) { Print("error code return by Modbus/RTU device is %d\r\n", iRet); } else if(iRet<0) { Print("[Modbus/RTU Slave] Process[0]==> DI error code=%d\r\n", iRet); } else if(iRet==502 || iRet==2) { Print("Wait for DI transaction response %d\n\r",iRet); } else //for return value:500,501,503 { //only for non-blocked mode } DelayMs(1000); } } Print("Please press ENTER to exit...\r\n"); Getch(); return 0; } 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; //Print("FC:%2d StartAddress:%3d IOCount:%4d\n\r",iModbusRequest_Fun, iModbusRequest_Addr,iModbusRequest_IOCount); //Print("Modbus Request\n\r In==>"); //for(i=0;i<*CommandLength;i++) // Print("[%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; //Print("\n\r Out==>"); //for(i=0;i<*ResponseLength;i++) // Print("[%02X] ",ResponseData[i]&0xFF); //Print("\n\r"); // do nothing ResponseData=ResponseData; ResponseLength=ResponseLength; }