0

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.

enter image description here

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.

5
  • What does GetDatasAsync do? (Post the code) Commented Jan 23 at 12:39
  • 1
    Since we're encouraged to blaim your code: Data is plural, the term datas does not exist. Commented Jan 23 at 13:06
  • ^^ Same goes for "informations", btw (from the guy that keeps on fixing that in the company code base since 2019) Commented Jan 23 at 13:18
  • 1
    Whatever it is, it's taking 5 seconds longer than the other "writes". Sounds like a "timeout" or "busy". When dealing with "devices", you need more awareness of "buffer" and "block" sizes (on the device); which can introduce "frames" into the processing cycle. Commented Jan 23 at 19:24
  • 1
    Consider testing with something like mbpoll or modpoll (both have adjustable poll rates) to see if these also fail in a similar way (thus establishing if the issue is your code/Modbus.Net, or the device itself). Commented Jan 24 at 4:06

1 Answer 1

0

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.