Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ModBusDevice.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
8
10{
15 {
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;
21
28 : base(Parent, Model)
29 {
30 }
31
41 {
42 }
43
47 public override string LocalName => nameof(ModBusDevice);
48
52 public byte InstanceAddress => this.instanceAddress;
53
61 {
62 return new ModBusDevice(Parent, Model);
63 }
64
69 public override Task FromXml(XmlElement Definition)
70 {
71 this.startAddress = (byte)XML.Attribute(Definition, "startAddress", 0);
72
73 return base.FromXml(Definition);
74 }
75
84 public override Task<Actor> CreateInstanceAsync(int InstanceIndex, string InstanceId)
85 {
86 return Task.FromResult<Actor>(new ModBusDevice(this, this.Model, InstanceIndex, InstanceId)
87 {
88 startAddress = this.startAddress,
89 instanceAddress = (byte)(this.startAddress + InstanceIndex - 1)
90 });
91 }
92
96 public override Task Start()
97 {
98 lock (this.synchObj)
99 {
100 this.devicesPerAddress.Clear();
101 this.registersByNr.Clear();
102
103 if (!(this.Instances is null))
104 {
105 foreach (IActor Actor in this.Instances)
106 {
107 if (Actor is ModBusDevice Device)
108 {
109 if (this.devicesPerAddress.ContainsKey(Device.instanceAddress))
110 throw new Exception("Multiple devices with the same address: " + Device.instanceAddress.ToString());
111
112 this.devicesPerAddress[Device.instanceAddress] = Device;
113 }
114 }
115 }
116
117 if (!(this.Children is null))
118 {
119 foreach (ISimulationNode Node in this.Children)
120 {
121 if (Node is ModBusRegister Register)
122 {
123 if (this.registersByNr.ContainsKey(Register.RegisterNr))
124 throw new Exception("Multiple registers with the same number: " + Register.RegisterNr.ToString());
125
126 this.registersByNr[Register.RegisterNr] = Register;
127 }
128 }
129 }
130 }
131
132 return base.Start();
133 }
134
138 public override Task Finalize()
139 {
140 lock (this.synchObj)
141 {
142 this.devicesPerAddress.Clear();
143 this.registersByNr.Clear();
144 }
145
146 return base.Finalize();
147 }
148
152 public override Task InitializeInstance()
153 {
154 return Task.CompletedTask;
155 }
156
160 public override Task StartInstance()
161 {
162 return Task.CompletedTask;
163 }
164
168 public override Task FinalizeInstance()
169 {
170 return Task.CompletedTask;
171 }
172
178 public ModBusDevice FindInstance(ushort Address)
179 {
180 lock (this.synchObj)
181 {
182 if (this.devicesPerAddress.TryGetValue(Address, out ModBusDevice Instance))
183 return Instance;
184 }
185
186 return null;
187 }
188
194 public ModBusRegister FindRegister(ushort RegisterNr)
195 {
196 lock (this.synchObj)
197 {
198 if (this.registersByNr.TryGetValue(RegisterNr, out ModBusRegister Register))
199 return Register;
200 }
201
202 return null;
203 }
204 }
205}
Abstract base class for ModBus actors.
Definition: ModBusActor.cs:9
Represents a simulated ModBus device
Definition: ModBusDevice.cs:15
ModBusDevice(ISimulationNode Parent, Model Model, int InstanceIndex, string InstanceId)
Represents a simulated ModBus device
Definition: ModBusDevice.cs:39
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.
Definition: ModBusDevice.cs:47
override Task Start()
Starts the node.
Definition: ModBusDevice.cs:96
override Task< Actor > CreateInstanceAsync(int InstanceIndex, string InstanceId)
Creates an instance of the actor.
Definition: ModBusDevice.cs:84
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.
Definition: ModBusDevice.cs:60
ModBusDevice(ISimulationNode Parent, Model Model)
Represents a simulated ModBus device
Definition: ModBusDevice.cs:27
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.
Definition: ModBusDevice.cs:69
override Task StartInstance()
Starts an instance of an actor.
Abstract base class for ModBus registers.
Root node of a simulation model
Definition: Model.cs:49
Abstract base class for actors
Definition: Actor.cs:15
string InstanceId
ID of actor instance.
Definition: Actor.cs:57
int InstanceIndex
Actor instance index.
Definition: Actor.cs:67
IActor[] Instances
References to created instances.
Definition: Actor.cs:77
void Register(IExternalEvent ExternalEvent)
Registers an external event on the actor.
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
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...
Definition: IActor.cs:11