2

I'm working on a c# project Which have to comunicate with PLC by TCP Modbus. I'm using Nmodbus Library and it works fine. The problem is when I try to read/write Registry over 12000. I get this exception:

  Exception 'Modbus.SlaveException'  Function Code: 131

This is the part of the code which generates the exception:

private TcpClient tcpClient;
     private ModbusIpMaster master;
     private void connect(){
    // connect to Modbus TCP Server
    string ipAddress = "192.168.77.7"; //Input WISE IP
    int tcpPort = 502;
    tcpClient = new TcpClient(ipAddress, tcpPort);

    // create Modbus TCP Master by the tcp client
    master = ModbusIpMaster.CreateIp(tcpClient);

    // rewrite the value of AO ch0 (40020~40021) by float
    byte slaveID = 1;
   // ushort rewriteAddress = 20;
   // ushort[] rewriteValue = new ushort[2] { 0, 0 };
   // float[] floatData = new float[1] { 223.456F };
   // Buffer.BlockCopy(floatData, 0, rewriteValue, 0, 4);
    Random random = new Random();

    // read the holding register 12001~12005 
    // write the holding register 301~305

    ushort startAddress = 12000;
    ushort numOfPoints = 5;
    master.Transport.ReadTimeout = 1000;
    while (!_shouldStop1)
    {
        try
        {
            ushort[] register = master.ReadHoldingRegisters(slaveID, startAddress, numOfPoints);

            for (int index = 0; index <register.Length; index++)
            {
                Console.WriteLine(string.Format("AO[{0}] = {1}", index,register[index]));
            }

            for (ushort index = 0; index < 4; index++)
            {
                master.WriteSingleRegister(slaveID, (ushort)(301 + index),(ushort) data[index]);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Any suggestion will be appreciated. Thanks, Federico.

  • That's a lot of code... can you narrow it down to the exact lines causing the exception? – g t Mar 30 '17 at 11:10
  • This is the line which causes the exception: `ushort[] register = master.ReadHoldingRegisters(slaveID, startAddress, numOfPoints);` where `startAddress=12001` and `numOfPoints=5`. If I try to read addresses lower than 12000 it works fine. – Federico Pinciaroli Mar 30 '17 at 14:30
  • Can you get more detail from the Exception (Try `e.Message` or `e.ToString()`). "Code 131" just means "Exception in ReadHoldingRegisters". Or check if you can read above 12000 indirectly, i.e. can you set `startAddress` to 11999 and still read 5 points? – g t Mar 31 '17 at 05:56
  • The `e.Message` content change startig from different addresses. For `startAddress=11999` the message is _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........_ while for `startAddress=12001` is _Exception Code: 4 - An unrecoverable error occurred while the server (or slave) was attempting to perform the requested action._ – Federico Pinciaroli Mar 31 '17 at 09:31
  • Could it be that you are reading `float`s? These require 32-bits, i.e. 2 registers. If you pass in 5 for `numOfPoints`, it can only read two-and-a-half registers and returns an error? Maybe try 4 or 6 instead? – g t Mar 31 '17 at 09:46
  • I'm reading int values. However I tried to pass 6 and 4 as you suggested me but it still fails. – Federico Pinciaroli Mar 31 '17 at 10:02
  • Perhaps it is the server rejecting the request. Have you tried using the [ModScan](https://www.win-tech.com/html/modbus1.htm) app to send the request? – g t Mar 31 '17 at 11:34

0 Answers0