Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ModBusClient.cs
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4using System.Xml;
9
11{
16 {
17 private readonly SemaphoreSlim synchObj = new SemaphoreSlim(1);
18 private ModbusTcpClient client;
19 private ISniffer sniffer;
20 private string host;
21 private int port;
22
29 : base(Parent, Model)
30 {
31 }
32
42 {
43 }
44
48 public override string LocalName => nameof(ModBusClient);
49
54 public override Task FromXml(XmlElement Definition)
55 {
56 this.host = XML.Attribute(Definition, "host", "localhost");
57 this.port = XML.Attribute(Definition, "port", ModbusTcpClient.DefaultPort);
58
59 return base.FromXml(Definition);
60 }
61
69 {
70 return new ModBusClient(Parent, Model);
71 }
72
81 public override Task<Actor> CreateInstanceAsync(int InstanceIndex, string InstanceId)
82 {
83 return Task.FromResult<Actor>(new ModBusClient(this, this.Model, InstanceIndex, InstanceId)
84 {
85 host = this.host,
86 port = this.port
87 });
88 }
89
93 public override Task InitializeInstance()
94 {
95 this.client = null;
96 return Task.CompletedTask;
97 }
98
102 public override async Task StartInstance()
103 {
104 this.sniffer = this.Model.GetSniffer(this.InstanceId, BinaryPresentationMethod.Hexadecimal);
105 this.client = await ModbusTcpClient.Connect(this.host, this.port, this.sniffer);
106 }
107
111 public override async Task FinalizeInstance()
112 {
113 await this.Lock();
114 try
115 {
116 this.client?.Dispose();
117 this.client = null;
118
119 if (!(this.sniffer is null))
120 {
121 if (this.sniffer is IDisposable Disposable)
122 Disposable.Dispose();
123
124 this.sniffer = null;
125 }
126 }
127 finally
128 {
129 this.Unlock();
130 }
131 }
132
136 public ModbusTcpClient Client => this.client;
137
141 public async Task Lock()
142 {
143 await this.synchObj.WaitAsync();
144 }
145
149 public void Unlock()
150 {
151 this.synchObj.Release();
152 }
153 }
154}
Abstract base class for ModBus actors.
Definition: ModBusActor.cs:9
Represents a simulated ModBus client
Definition: ModBusClient.cs:16
ModBusClient(ISimulationNode Parent, Model Model, int InstanceIndex, string InstanceId)
Represents a simulated ModBus client
Definition: ModBusClient.cs:40
override Task< Actor > CreateInstanceAsync(int InstanceIndex, string InstanceId)
Creates an instance of the actor.
Definition: ModBusClient.cs:81
void Unlock()
Unlocks the client.
override Task InitializeInstance()
Initializes an instance of an actor.
Definition: ModBusClient.cs:93
override async Task FinalizeInstance()
Finalizes an instance of an actor.
ModBusClient(ISimulationNode Parent, Model Model)
Represents a simulated ModBus client
Definition: ModBusClient.cs:28
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: ModBusClient.cs:68
ModbusTcpClient Client
ModBus TCP client reference.
override async Task StartInstance()
Starts an instance of an actor.
async Task Lock()
Locks the client for use by one caller.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: ModBusClient.cs:54
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
string InstanceId
ID of actor instance.
Definition: Actor.cs:57
int InstanceIndex
Actor instance index.
Definition: Actor.cs:67
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
static async Task< ModbusTcpClient > Connect(string Host, int Port, params ISniffer[] Sniffers)
Connects to an Modbus TCP/IP Gateway
const int DefaultPort
Default Modbus port (502)
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
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.