0
Answer

Modbus RTU master software design

Emma Emma

Emma Emma

11y
7.2k
1
Hi

I am working one designing one windows based software which can read the register values from Modbus RTU slave register or man can say PLC. I got the NMODBUS library, here is the code: 

 using (SerialPort port = new SerialPort(ComBox.Text))
            {
                // configure serial port
                port.BaudRate = Convert.ToInt16(BaudBox.Text.ToString());
                port.DataBits = 8;
                port.Parity = Parity.None;
                port.StopBits = StopBits.One;
                port.Open();

                // create modbus master
                IModbusSerialMaster master = ModbusSerialMaster. CreateRtu(port);
                
                byte slaveId =Convert.ToByte(SlaveID2.Text.ToString());
                ushort startAddress = 40000;
                ushort numRegisters = 10;

                // read five registers
                ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);

                for (int i = 0; i < numRegisters; i++)
                    Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
            }


I got one problem like this: 
The problem is : 
Exception of type 'Modbus.SlaveException' was thrown.
Function Code: 131
Exception Code: 2 - The data address received in the query is not an allowable address for the server (or slave). More specifically, the combination of reference number and transfer length is invalid. For a controller with 100 registers, the PDU addresses the first register as 0, and the last one as 99. If a request is submitted with a starting register address of 96 and a quantity of registers of 4, then this request will successfully operate (address-wise at least) on registers 96, 97, 98, 99. If a request is submitted with a starting register address of 96 and a quantity of registers of 5, then this request will fail with Exception Code 0x02 "Illegal Data Address" since it attempts to operate on registers 96, 97, 98, 99 and 100, and there is no register with address 100.




Please anyone can help???Thanks so much...

Next Recommended Forum