Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ModBusServer.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
9
11{
16 {
17 private ModBusTcpServer server;
18 private ISniffer sniffer;
19 private int port;
20
27 : base(Parent, Model)
28 {
29 }
30
34 public override string Namespace => ModBusActor.ComSimModBusNamespace;
35
40
44 public override string LocalName => nameof(ModBusServer);
45
50 public ModBusTcpServer Server => this.server;
51
59 {
60 return new ModBusServer(Parent, Model);
61 }
62
67 public override Task FromXml(XmlElement Definition)
68 {
69 this.port = XML.Attribute(Definition, "port", 502);
70
71 return base.FromXml(Definition);
72 }
73
78 public void Register(IActor Actor)
79 {
80 if (this.Parent is IActors Actors)
82 }
83
87 public override async Task Initialize()
88 {
89 this.sniffer = this.Model.GetSniffer("ModBus" + this.port.ToString(), BinaryPresentationMethod.Hexadecimal);
90 this.server = await ModBusTcpServer.CreateAsync(this.port, this.sniffer);
91
92 await base.Initialize();
93 }
94
98 public override Task Start()
99 {
100 return base.Start();
101 }
102
106 public override Task Finalize()
107 {
108 this.server.Dispose();
109 this.server = null;
110
111 if (!(this.sniffer is null))
112 {
113 if (this.sniffer is IDisposable Disposable)
114 Disposable.Dispose();
115
116 this.sniffer = null;
117 }
118
119 return base.Finalize();
120 }
121 }
122}
Abstract base class for ModBus actors.
Definition: ModBusActor.cs:9
const string ComSimModBusSchema
Resource name of ModBus schema.
Definition: ModBusActor.cs:18
const string ComSimModBusNamespace
http://lab.tagroot.io/Schema/ComSim.xsd
Definition: ModBusActor.cs:13
override string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
Definition: ModBusServer.cs:39
override async Task Initialize()
Initialized the node before simulation.
Definition: ModBusServer.cs:87
void Register(IActor Actor)
Registers an actor with the collection of actors.
Definition: ModBusServer.cs:78
override Task Finalize()
Finalizes the node after simulation.
override string LocalName
Local name of XML element defining contents of class.
Definition: ModBusServer.cs:44
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: ModBusServer.cs:58
ModBusTcpServer Server
Reference to the ModBus TCP server object. (Only available on initialized instances....
Definition: ModBusServer.cs:50
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: ModBusServer.cs:67
ModBusServer(ISimulationNode Parent, Model Model)
Hosts a ModBus IP Gateway
Definition: ModBusServer.cs:26
override string Namespace
XML Namespace where the element is defined.
Definition: ModBusServer.cs:34
override Task Start()
Starts the node.
Definition: ModBusServer.cs:98
Root node of a simulation model
Definition: Model.cs:49
ISniffer GetSniffer(string Actor)
Gets a sniffer, if sniffer output is desired.
Definition: Model.cs:863
Abstract base class for actors
Definition: Actor.cs:15
void Register(IActor Actor)
Registers an actor with the collection of actors.
Definition: Actors.cs:42
Abstract base class for simulation nodes with children
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
void Dispose()
Closes and disposes of the server.
static async Task< ModBusTcpServer > CreateAsync(int Port, params ISniffer[] Sniffers)
Creates and opens a ModBus server.
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
Interface for collections of actors.
Definition: IActors.cs:7
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:11
BinaryPresentationMethod
How binary data is to be presented.