Reputation: 1
I implemented a Modbus.Net v1.4.3 client and poll data peridoically in a main program and experience strange behaviour.
Every 6th poll gives:
Data = null
Success = false
ErrorCode = 0
ErrorMsg = ""
The other polls work just fine.
Feel free to blaim my code along the way.
In the following code only this line does the poll :
//Get data from machine
var result = await _machine.GetMachineMethods<IMachineMethodDatas>().GetDatasAsync(MachineDataType.CommunicationTag);`
Modbus Client class:
using Microsoft.Extensions.Logging;
using Modbus.Net;
using Modbus.Net.Modbus;
/// <summary>
/// Modbus using Modbus.Net
/// </summary>
internal class ModbusClient
{
public ModbusClient(string ip, byte id, List<AddressUnit<int, int, int>> addressConfig)
{
// init machine object with address-unit config
_machine = new ModbusMachine<int, int>(id, ModbusType.Tcp, ip, addressConfig, true, id, 0, 2);
}
/// <summary>
/// Modbus.Net Machine
/// </summary>
private IMachineProperty<int> _machine;
/// <summary>
/// Poll configured addresses.
/// </summary>
/// <returns>Collection of <see cref="ReturnUnit{TReturn}"/></returns>
/// <exception cref="IOException">Poll failed.</exception>
public async Task<ICollection<ReturnUnit<double>>> PollReturnUnits()
{
//Get data from machine
var result = await _machine.GetMachineMethods<IMachineMethodDatas>().GetDatasAsync(MachineDataType.CommunicationTag);
//Error handling
if (result.Datas is null || !result.IsSuccess)
{
throw new IOException($"code:{result.ErrorCode}, msg:{result.ErrorMsg}");
}
//Return AdressUnits
return result.Datas.Values;
}
}
Edit:
-Please don't rely on the documentation of the package, since it's outdated. Download package and paste my code to browse.
-The GetDatasAsync
reads some configured registers. It returns a "ReturnStruct" with the properties (which partly have wrong plural): Datas, Success, ErrorMsg, ErrorCode.
Upvotes: 0
Views: 54
Reputation: 1
I have tested the slave with this Modbus Scanner: https://store.chipkin.com/products/tools/cas-modbus-scanner
With an hour of observation I was able replicate the behaviour, altough it occours, that it's much less frequent with the scanner, which lead me to blame the modbus library first.
I guess thats it for this question. Thanks to everyone!
Upvotes: 0