Skip to content

4.2.Three API calling ways

luosheng edited this page Jun 7, 2023 · 2 revisions

At the beginning, before using Modbus.Net, Modbus.Net.dll is not the only framework that required but also at least a concrete protocol framework like Modbus.Net.Modbus.dll.

Utility


  1. Initialize an Utility, e.g.:
IUtilityProperty utility = new ModbusUtility(ModbusType.Tcp, "192.168.1.10", 2, 0, Endian.LittleEndian);
  • Modbus.Net has three different Endians:
    • Little endian: Endian.LittleEndian, least significant bit.
    • Big endian: Endian.BigEndianLsb, least significant bit.
    • Big endian Msb: Endian.BigEndianMsb, most significant bit.
  1. Call method in Utility, IUtilityProperty has a method that could load method group.
public TUtilityMethod GetUtilityMethods<TUtilityMethod>() where TUtilityMethod : class, IUtilityMethod

Use this function like:

utility.GetUtilityMethods<IUtilityMethodDatas>()?.GetDatasAsync(...);

GetUtilityMethods will return null when utility doesn't implement some IUtilityMethod type in template.

For BaseUtility:

  • BaseUtility implement IUtilityMethodData as default, including GetDatasAsync and SetDatasAsync, they are used for read data and write data.
  • Utility only has a string address, this address describe full messages for address, different protocol has different meaning with that due to the AddressTranslator.
  • e.g. 11th bit of Modbus.Net.Mdobus 30005 address is "3X 5.10" using AddressTranslatorModbus. Bit should minus 1 because Modbus.Net use 0 as the 1st bit but Modbus standard is 1.

Machine


  1. Initialize a Machine instance, e.g.:
IMachineProperty machine = new ModbusMachine(ModbusType.Tcp, "192.168.1.10", new List<AddressUnit>(...), true, 2, 0, Endian.LittleEndian);
  • Attention for AddressUnit:
    • AddressUnit will be combined using area and address in BaseMachine, so whatever you want, please write one and only fake Area and Address even though protocol dosen't use it (For Modbus.Net.OPC users).
  1. Call method in Machine, IMachineProperty has a method that could load method group.
public TMachineMethod GetMachineMethods<TUtilityMethod>() where TMachineMethod : class, IMachineMethod

Use this function like:

machine.GetMachineMethods<IMachineMethodDatas>()?.GetDatasAsync(...);

GetMachineMethods will return null when machine doesn't implement some IMachineMethod type in template.

For BaseMachine:

  • BaseMachine implement IMachineMethodData as default, including GetDatasAsync and SetDatasAsync, they are used for read data and write data.
  • BaseMachine has four return keywords, Id returns Id of AddressUnit, Name returns Name of AddressUnit, CommunicationTag returns CommunicationTag of AddressUnit, Address return string format address of AddressUnit.
  • CommunicationTag is used for block other keywords in AddressUnit, it is recommended in json for short the json length and block privacy messages. CommuncationTag must be only in a device like Id for AddressUnit.

MachineJobScheduler

  1. Initialize a machinejobscheduler.
var machineJobScheduler = await MachineJobSchedulerCreator.CreateScheduler("Trigger1", -1, 10);
  1. Add a job chain, e.g. aquire data from machine every 10 seconds.
var job = machineJobScheduler.From(machine.Id + ".From", machine, MachineDataType.Name).Result.Query(machine.Id + ".ConsoleQuery", QueryConsole).Result;
  1. Run job.
await job.Run();

Home

Clone this wiki locally