Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ModbusUnitDiscreteInputNode.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Threading.Tasks;
11
12namespace Waher.Things.Modbus
13{
18 {
23 : base()
24 {
25 }
26
30 [Page(4, "Modbus", 100)]
31 [Header(13, "Register Number:")]
32 [ToolTip(14, "Register number on the Modbus unit.")]
33 [Range(0, 65535)]
34 [Required]
35 public int RegisterNr { get; set; }
36
40 [Page(4, "Modbus", 100)]
41 [Header(11, "Field Name:")]
42 [ToolTip(12, "Custom field name for value.")]
43 [DefaultValueStringEmpty]
44 public string FieldName { get; set; }
45
51 public override Task<string> GetTypeNameAsync(Language Language)
52 {
53 return Language.GetStringAsync(typeof(ModbusGatewayNode), 29, "Discrete Input (1x)");
54 }
55
62 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
63 {
64 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
65
66 Result.AddLast(new Int32Parameter("Nr", await Language.GetStringAsync(typeof(ModbusGatewayNode), 10, "Nr"), this.RegisterNr));
67
68 return Result;
69 }
70
75 public async Task StartReadout(ISensorReadout Request)
76 {
77 ModbusTcpClient Client = await (await this.GetGateway()).GetTcpIpConnection();
78 await Client.Enter();
79 try
80 {
81 BitArray Values = await Client.ReadInputDiscretes((byte)(await this.GetUnitNode()).UnitId, (ushort)this.RegisterNr, 1);
82 DateTime TP = DateTime.UtcNow;
83
84 ThingReference This = await this.GetReportAs();
85
86 await Request.ReportFields(true,
87 new BooleanField(This, TP, this.GetFieldName(), Values[0], FieldType.Momentary, FieldQoS.AutomaticReadout));
88 }
89 catch (Exception ex)
90 {
91 await Request.ReportErrors(true, new ThingError(this, ex.Message));
92 }
93 finally
94 {
95 await Client.Leave();
96 }
97 }
98
103 public string GetFieldName()
104 {
105 if (string.IsNullOrEmpty(this.FieldName))
106 return "Value";
107 else
108 return this.FieldName;
109 }
110 }
111}
Task Leave()
Leaves unique access to the TCP client. Must be called exactly one for each call to Enter.
Task Enter()
Enters unique access to the TCP client. Must be followed by exactly one Leave call.
async Task< BitArray > ReadInputDiscretes(byte UnitAddress, ushort ReferenceNumber, ushort NrBits)
Reads input discretes from a Modbus unit.
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
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 deiscrete input register on a Modbus unit node.
ModbusUnitDiscreteInputNode()
Represents a deiscrete input register on a Modbus unit node.
string GetFieldName()
Gets the field name of the node.
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
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
Contains a reference to a thing
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