8000 Random out of range exception in PerformReadRegisters while reading holding registers · Issue #200 · NModbus/NModbus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Random out of range exception in PerformReadRegisters while reading holding registers #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
krunalc opened this issue Nov 12, 2024 · 0 comments

Comments

@krunalc
Copy link
krunalc commented Nov 12, 2024

` [RelayCommand]
public void ReadMultipleRegisters()
{
RegisterValues = new ObservableCollection
(Enumerable.Range(0, 120).Select(i => new RegisterValuesVM()));
int i = 1;
foreach (var reg in RegisterValues)
reg.RegisterNo = i++;

        var IpAddressValue = _configuration["IPConfiguration:IPAddress"];
        var PortValue = Convert.ToInt32(_configuration["IPConfiguration:Port"]);
        client = new TcpClient(IpAddressValue, PortValue);

        _timer = new System.Threading.Timer(Timer_Tick, null, 0, 100);
    }

    private void Timer_Tick(object sender)
    {
        //Task.Run(() =>
        //{
        System.Diagnostics.Debug.WriteLine("Time tick");
            ReadMultipleRegistersWithTimer();
        //});
    }

    public async void ReadMultipleRegistersWithTimer()
    {
        int startReg = 1, regno = 1, endReg = 120;
        var data = _modbusService.ReadMultipleRegisters(SlaveAddressValue, startReg, endReg, client);
        if (data != null && data.Acknowledge == AcknowledgeType.Success)
        {
            for (var i = 0; i < 120; i++)
            {
                RegisterValues[i].Value = data.Data[i];
            }
        }
        else
        {
            //_timer.Dispose();
        }
    }

    public ReadMultipleRegisterResponse ReadMultipleRegisters(string slaveAddress, int address, int length, TcpClient client)
    {
        try
        {
            {
                var requesttime = DateTime.Now;
                var factory = new ModbusFactory();
                IModbusMaster master = factory.CreateMaster(client);
                DateTime preTime = DateTime.Now;
                // read five input values
                ushort startAddress = (ushort)(address - 1);
                ushort numInputs = (ushort)length;
                Debug.WriteLine($"{slaveAddress} - {startAddress} - {numInputs}");
                ushort[] inputs = master.ReadHoldingRegisters(byte.Parse(slaveAddress), startAddress, numInputs);
                var responetime = DateTime.Now.Ticks;

                return new ReadMultipleRegisterResponse()
                {
                    Data = inputs,
                    Acknowledge = AcknowledgeType.Success,
                };
            }
        }
        catch (Exception ex)
        {
            return new ReadMultipleRegisterResponse()
            {
                Data = new ushort[0],
                Acknowledge = AcknowledgeType.Failure,
            };
        }
    }

`

I'm trying to read data from modbus server at periodic interval of 100 ms. For that I have setup a time to send request when it triggers. Above is the code I have written.

With above code, I see randomly NModbus library throws an out of range exception and following is the exception stack.

at NModbus.IO.ModbusTransport.CreateResponse[T](Byte[] frame)
at NModbus.IO.ModbusIpTransport.CreateMessageAndInitializeTransactionId[T](Byte[] fullFrame)
at NModbus.IO.ModbusTransport.UnicastMessage[T](IModbusMessage message)
at NModbus.Device.ModbusMaster.PerformReadRegisters(ReadHoldingInputRegistersRequest request)
at PrinterApp.Services.ModbusService.ReadMultipleRegisters(String slaveAddress, Int32 address, Int32 length, TcpClient client)

Exception come very randomly even if no action is being performed by client and I'm clueless why it is causing exception. Any help in this regard?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0