Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ModbusUnitCoilNode.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Threading.Tasks;
12
13namespace Waher.Things.Modbus
14{
19 {
24 : base()
25 {
26 }
27
31 [Page(4, "Modbus", 100)]
32 [Header(8, "Coil Number:")]
33 [ToolTip(9, "Coil number on the Modbus unit.")]
34 [Range(0, 65535)]
35 [Required]
36 public int CoilNr { get; set; }
37
41 [Page(4, "Modbus", 100)]
42 [Header(11, "Field Name:")]
43 [ToolTip(12, "Custom field name for value.")]
44 [DefaultValueStringEmpty]
45 public string FieldName { get; set; }
46
52 public override Task<string> GetTypeNameAsync(Language Language)
53 {
54 return Language.GetStringAsync(typeof(ModbusGatewayNode), 3, "Coil (0x)");
55 }
56
63 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
64 {
65 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
66
67 Result.AddLast(new Int32Parameter("Nr", await Language.GetStringAsync(typeof(ModbusGatewayNode), 10, "Nr"), this.CoilNr));
68
69 return Result;
70 }
71
76 public async Task StartReadout(ISensorReadout Request)
77 {
78 ModbusTcpClient Client = await (await this.GetGateway()).GetTcpIpConnection();
79 await Client.Enter();
80 try
81 {
82 BitArray Bits = await Client.ReadCoils((byte)(await this.GetUnitNode()).UnitId, (ushort)this.CoilNr, 1);
83
84 await Request.ReportFields(true, new BooleanField(await this.GetReportAs(), DateTime.UtcNow, this.GetFieldName(), Bits[0], FieldType.Momentary, FieldQoS.AutomaticReadout, true));
85 }
86 catch (Exception ex)
87 {
88 await Request.ReportErrors(true, new ThingError(this, ex.Message));
89 }
90 finally
91 {
92 await Client.Leave();
93 }
94 }
95
100 public string GetFieldName()
101 {
102 if (string.IsNullOrEmpty(this.FieldName))
103 return "Value";
104 else
105 return this.FieldName;
106 }
107
112 public Task<ControlParameter[]> GetControlParameters()
113 {
114 return Task.FromResult(new ControlParameter[]
115 {
116 new BooleanControlParameter("Value", "Modbus", this.GetFieldName(), "Coil output",
117 async (Node) =>
118 {
119 ModbusTcpClient Client = await (await this.GetGateway()).GetTcpIpConnection();
120 await Client.Enter();
121 try
122 {
123 BitArray Bits = await Client.ReadCoils((byte)(await this.GetUnitNode()).UnitId, (ushort)this.CoilNr, 1);
124 return Bits[0];
125 }
126 finally
127 {
128 await Client.Leave();
129 }
130 },
131 async (Node, Value) =>
132 {
133 ModbusTcpClient Client = await (await this.GetGateway()).GetTcpIpConnection();
134 await Client.Enter();
135 try
136 {
137 bool WritenValue = await Client.WriteCoil((byte)(await this.GetUnitNode()).UnitId, (ushort)this.CoilNr, Value);
138
139 if (WritenValue != Value)
140 throw new Exception("Coil value not changed.");
141 }
142 finally
143 {
144 await Client.Leave();
145 }
146 })
147 });
148 }
149 }
150}
Task Leave()
Leaves unique access to the TCP client. Must be called exactly one for each call to Enter.
async Task< BitArray > ReadCoils(byte UnitAddress, ushort ReferenceNumber, ushort NrBits)
Reads coils from a Modbus unit.
async Task< bool > WriteCoil(byte UnitAddress, ushort ReferenceNumber, bool Value)
Write to a single coil
Task Enter()
Enters unique access to the TCP client. Must be followed by exactly one Leave call.
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Abstract base class for control parameters.
Node representing a TCP/IP connection to a Modbus Gateway
Abstract base class for child nodes to Mobus Unit nodes.
async Task< ModbusGatewayNode > GetGateway()
Modbus Gateway node.
async Task< ModbusUnitNode > GetUnitNode()
Modbus Unit node.
async Task< ThingReference > GetReportAs()
Under what node fields are to be reported.
Represents a coil on a Modbus unit node.
string GetFieldName()
Gets the field name of the node.
async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
ModbusUnitCoilNode()
Represents a coil on a Modbus unit node.
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Task< ControlParameter[]> GetControlParameters()
Get control parameters for the actuator.
Tokens available in request.
Definition: RequestOrigin.cs:9
Represents a boolean value that can be either true or false.
Definition: BooleanField.cs:11
Contains information about an error on a thing
Definition: ThingError.cs:10
Interface for actuator nodes.
Definition: IActuator.cs:10
Interface for sensor nodes.
Definition: ISensor.cs:9
Interface for classes managing sensor data readouts.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
FieldQoS
Field Quality of Service flags
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10