Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WriteModBusCoil.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8using Waher.Script;
9
11{
16 {
23 : base(Parent, Model)
24 {
25 }
26
30 public override string LocalName => nameof(WriteModBusCoil);
31
39 {
40 return new WriteModBusCoil(Parent, Model);
41 }
42
48 public override async Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
49 {
50 ModBusClient Client = await this.GetClient(Variables);
51 byte Address = await this.address.GetUInt8ValueAsync(Variables);
52 ushort Register = await this.register.GetUInt16ValueAsync(Variables);
53
54 if (this.value is null)
55 throw new Exception("Value not defined.");
56
57 double Value = Expression.ToDouble(await this.value.EvaluateAsync(Variables));
58 bool Result;
59
60 await Client.Lock();
61 try
62 {
63 if (!(Client.Client is null))
64 Result = await Client.Client.WriteCoil(Address, Register, Value != 0);
65 else
66 return null;
67 }
68 finally
69 {
70 Client.Unlock();
71 }
72
73 if (!(this.responseVariable is null))
74 {
75 string VariableName = await this.responseVariable.GetValueAsync(Variables);
76 Variables[VariableName] = Result;
77 }
78
79 return null;
80 }
81 }
82}
DoubleAttribute address
ModBus device Address attribute
async Task< ModBusClient > GetClient(Variables Variables)
Gets the ModBus TCP Client to use in the operation.
Abstract base class for ModBus write operations.
void Register(IValue Value)
Registers a value for the argument.
StringAttribute responseVariable
Response variable attribute.
Represents a simulated ModBus client
Definition: ModBusClient.cs:16
void Unlock()
Unlocks the client.
ModbusTcpClient Client
ModBus TCP client reference.
async Task Lock()
Locks the client for use by one caller.
override string LocalName
Local name of XML element defining contents of class.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
WriteModBusCoil(ISimulationNode Parent, Model Model)
Writes a coil value.
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Root node of a simulation model
Definition: Model.cs:49
async Task< bool > WriteCoil(byte UnitAddress, ushort ReferenceNumber, bool Value)
Write to a single coil
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
Collection of variables.
Definition: Variables.cs:25
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.