miranhelbl / nmodbus

Automatically exported from code.google.com/p/nmodbus
0 stars 0 forks source link

Library throw an exception instead of return a exception response for illegal function #74

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Send a illegal function message to a Modbus Slave Device
2.
3.

What is the expected output? What do you see instead?
Library throw a exception instead of response a Illegal Function Message

What version of the product are you using? On what operating system?
Windows 7 x64, NModbus_net-3.5_1.11.0.0-source.zip  

Please provide any additional information below.

I had to build a basic SlaveExceptionRequest class:

namespace Modbus.Message
{
    internal class SlaveExceptionRequest : ModbusMessage, IModbusRequest
    {
        public SlaveExceptionRequest()
        {
        }

        public SlaveExceptionRequest(FunctionCode functionCode, byte slaveAddress)
            : base(slaveAddress, functionCode)
        {

        }

        public override int MinimumFrameSize
        {
            get { return 2; }
        }

        protected override void InitializeUnique(byte[] frame)
        {
            SlaveAddress = frame[0];
            FunctionCode = (FunctionCode)frame[1];
        }

        public void ValidateResponse(IModbusMessage response)
        {
        }
    }
}

And make the following changes:

ModbusMessageFactory.cs:

public static IModbusMessage CreateModbusRequest(byte[] frame)
        {
...
switch (functionCode)
            {
...
                default:

                    request = CreateModbusMessage<SlaveExceptionRequest>(frame);
                    break;
...

ModbusSlave.cs:

internal IModbusMessage ApplyRequest(IModbusMessage request)
        {           
...
switch (request.FunctionCode)
            {
...
                default:
                   response = new SlaveExceptionResponse(request.SlaveAddress, (FunctionCode)((byte)request.FunctionCode | Modbus.ExceptionOffset), Modbus.IllegalFunction);
                    break;
            }           

            return response;
        }
    }
}

Original issue reported on code.google.com by alexrosa...@gmail.com on 12 Aug 2014 at 2:04