Random out of range exception in PerformReadRegisters while reading holding registers · Issue #200 · NModbus/NModbus · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
` [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?
The text was updated successfully, but these errors were encountered:
` [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++;
`
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?
The text was updated successfully, but these errors were encountered: