Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ModbusUnitInputRegisterNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
10
11namespace Waher.Things.Modbus
12{
17 {
22 : base()
23 {
24 this.Multiplier = 1.0;
25 this.Divisor = 1.0;
26 }
27
31 [Page(4, "Modbus", 100)]
32 [Header(13, "Register Number:")]
33 [ToolTip(14, "Register number on the Modbus unit.")]
34 [Range(0, 65535)]
35 [Required]
36 public int RegisterNr { get; set; }
37
41 [Page(4, "Modbus", 100)]
42 [Header(25, "Raw Name:")]
43 [ToolTip(26, "Custom field name for raw value.")]
44 [DefaultValueStringEmpty]
45 public string RawName { get; set; }
46
50 [Page(4, "Modbus", 100)]
51 [Header(11, "Field Name:")]
52 [ToolTip(12, "Custom field name for value.")]
53 [DefaultValueStringEmpty]
54 public string FieldName { get; set; }
55
59 [Page(4, "Modbus", 100)]
60 [Header(18, "Multiplier:")]
61 [ToolTip(19, "Multiplier will be multiplied to the register before reporting value.")]
62 [DefaultValue(1.0)]
63 [Text(TextPosition.BeforeField, 24, "Raw value will be transformed as follows: Value = ((Raw * Multiplier) / Divisor) + Offset. To this transformed value, the unit will be added.")]
64 public double Multiplier { get; set; }
65
69 [Page(4, "Modbus", 100)]
70 [Header(20, "Divisor:")]
71 [ToolTip(21, "Divisor will be divided from the register (after multiplication) before reporting value.")]
72 [DefaultValue(1.0)]
73 public double Divisor { get; set; }
74
78 [Page(4, "Modbus", 100)]
79 [Header(22, "Offset:")]
80 [ToolTip(23, "Offset will be addded to the register (after division) before reporting value.")]
81 [DefaultValue(0.0)]
82 public double Offset { get; set; }
83
87 [Page(4, "Modbus", 100)]
88 [Header(16, "Unit:")]
89 [ToolTip(17, "Unit of register value (after scaling and offset).")]
90 [DefaultValueStringEmpty]
91 public string Unit { get; set; }
92
96 [Page(4, "Modbus", 100)]
97 [Header(46, "Switch byte order.")]
98 [ToolTip(47, "If checked, byte order in registers will be reversed.")]
99 [DefaultValue(false)]
100 public bool SwitchByteOrder { get; set; }
101
107 public override Task<string> GetTypeNameAsync(Language Language)
108 {
109 return Language.GetStringAsync(typeof(ModbusGatewayNode), 15, "Input Register (3x)");
110 }
111
118 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
119 {
120 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
121
122 Result.AddLast(new Int32Parameter("Nr", await Language.GetStringAsync(typeof(ModbusGatewayNode), 10, "Nr"), this.RegisterNr));
123
124 return Result;
125 }
126
131 public async Task StartReadout(ISensorReadout Request)
132 {
133 ModbusTcpClient Client = await (await this.GetGateway()).GetTcpIpConnection();
134 await Client.Enter();
135 try
136 {
137 ushort[] Values = await Client.ReadInputRegisters((byte)(await this.GetUnitNode()).UnitId, (ushort)this.RegisterNr, 1);
138 ushort Raw = ModbusUnitHoldingRegisterNode.CheckOrder(this.SwitchByteOrder, Values[0]);
139 double Value = ((Raw * this.Multiplier) / this.Divisor) + this.Offset;
140 int NrDec = Math.Min(255, Math.Max(0, (int)Math.Ceiling(-Math.Log10(this.Multiplier / this.Divisor))));
141 DateTime TP = DateTime.UtcNow;
142 ThingReference This = await this.GetReportAs();
143 List<Field> Fields = new List<Field>
144 {
145 new QuantityField(This, TP, this.GetFieldName(), Value, (byte)NrDec, this.Unit, FieldType.Momentary, FieldQoS.AutomaticReadout, true)
146 };
147
148 if (!string.IsNullOrEmpty(this.RawName))
149 Fields.Add(new Int32Field(This, TP, this.RawName, Raw, FieldType.Momentary, FieldQoS.AutomaticReadout, true));
150
151 await Request.ReportFields(true, Fields.ToArray());
152 }
153 catch (Exception ex)
154 {
155 await Request.ReportErrors(true, new ThingError(this, ex.Message));
156 }
157 finally
158 {
159 await Client.Leave();
160 }
161 }
162
167 public string GetFieldName()
168 {
169 if (string.IsNullOrEmpty(this.FieldName))
170 return "Value";
171 else
172 return this.FieldName;
173 }
174
175 }
176}
Task Leave()
Leaves unique access to the TCP client. Must be called exactly one for each call to Enter.
async Task< ushort[]> ReadInputRegisters(byte UnitAddress, ushort ReferenceNumber, ushort NrWords)
Reads input registers from a Modbus unit.
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
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 holding register on a Modbus unit node.
Represents an input register on a Modbus unit node.
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
bool SwitchByteOrder
If the byte order in words should be switched.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
async Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
string GetFieldName()
Gets the field name of the node.
ModbusUnitInputRegisterNode()
Represents a register on a Modbus unit node.
Tokens available in request.
Definition: RequestOrigin.cs:9
Represents a 32-bit integer value.
Definition: Int32Field.cs:10
Represents a physical quantity value.
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.
TextPosition
Where the instructions are to be place.
Definition: TextAttribute.cs:9
FieldQoS
Field Quality of Service flags
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10