2using System.Collections.Generic;
3using System.Threading.Tasks;
16 private readonly Dictionary<ushort, ModBusDevice> devicesPerAddress =
new Dictionary<ushort, ModBusDevice>();
17 private readonly Dictionary<ushort, ModBusRegister> registersByNr =
new Dictionary<ushort, ModBusRegister>();
18 private readonly
object synchObj =
new object();
19 private byte startAddress;
20 private byte instanceAddress;
69 public override Task
FromXml(XmlElement Definition)
71 this.startAddress = (byte)
XML.
Attribute(Definition,
"startAddress", 0);
73 return base.FromXml(Definition);
88 startAddress = this.startAddress,
89 instanceAddress = (byte)(this.startAddress +
InstanceIndex - 1)
100 this.devicesPerAddress.Clear();
101 this.registersByNr.Clear();
109 if (this.devicesPerAddress.ContainsKey(Device.instanceAddress))
110 throw new Exception(
"Multiple devices with the same address: " + Device.instanceAddress.ToString());
112 this.devicesPerAddress[Device.instanceAddress] = Device;
123 if (this.registersByNr.ContainsKey(
Register.RegisterNr))
124 throw new Exception(
"Multiple registers with the same number: " +
Register.RegisterNr.ToString());
142 this.devicesPerAddress.Clear();
143 this.registersByNr.Clear();
146 return base.Finalize();
154 return Task.CompletedTask;
162 return Task.CompletedTask;
170 return Task.CompletedTask;
182 if (this.devicesPerAddress.TryGetValue(Address, out
ModBusDevice Instance))
Abstract base class for ModBus actors.
Represents a simulated ModBus device
ModBusDevice(ISimulationNode Parent, Model Model, int InstanceIndex, string InstanceId)
Represents a simulated ModBus device
ModBusRegister FindRegister(ushort RegisterNr)
Finds the register corresponding to a register number.
override Task InitializeInstance()
Initializes an instance of an actor.
override string LocalName
Local name of XML element defining contents of class.
override Task Start()
Starts the node.
override Task< Actor > CreateInstanceAsync(int InstanceIndex, string InstanceId)
Creates an instance of the actor.
override Task FinalizeInstance()
Finalizes an instance of an actor.
override Task Finalize()
Finalizes the node after simulation.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
ModBusDevice(ISimulationNode Parent, Model Model)
Represents a simulated ModBus device
byte InstanceAddress
Instance address
ModBusDevice FindInstance(ushort Address)
Finds the instance corresponding to a unit address.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
override Task StartInstance()
Starts an instance of an actor.
Abstract base class for ModBus registers.
Root node of a simulation model
Abstract base class for actors
string InstanceId
ID of actor instance.
int InstanceIndex
Actor instance index.
IActor[] Instances
References to created instances.
void Register(IExternalEvent ExternalEvent)
Registers an external event on the actor.
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
ISimulationNode[] Children
Child nodes.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...