|
@@ -0,0 +1,283 @@
|
|
1
|
+/*
|
|
2
|
+ * Author: Jon Trulson <jtrulson@ics.com>
|
|
3
|
+ * Copyright (c) 2016 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ * Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+ * a copy of this software and associated documentation files (the
|
|
7
|
+ * "Software"), to deal in the Software without restriction, including
|
|
8
|
+ * without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+ * distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+ * permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+ * the following conditions:
|
|
12
|
+ *
|
|
13
|
+ * The above copyright notice and this permission notice shall be
|
|
14
|
+ * included in all copies or substantial portions of the Software.
|
|
15
|
+ *
|
|
16
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+ */
|
|
24
|
+#pragma once
|
|
25
|
+
|
|
26
|
+#include <string>
|
|
27
|
+
|
|
28
|
+#include <modbus/modbus.h>
|
|
29
|
+
|
|
30
|
+namespace upm {
|
|
31
|
+
|
|
32
|
+ /**
|
|
33
|
+ * @brief HWXPXX Hardware Protocol Humidity and Temperature Sensor
|
|
34
|
+ * @defgroup hwxpxx libupm-hwxpxx
|
|
35
|
+ * @ingroup uart temp
|
|
36
|
+ */
|
|
37
|
+
|
|
38
|
+ /**
|
|
39
|
+ * @library hwxpxx
|
|
40
|
+ * @sensor hwxpxx
|
|
41
|
+ * @comname UPM API for the Veris HWXPXX Hardware Protocol Humidity and Temperature Sensor
|
|
42
|
+ * @type temp
|
|
43
|
+ * @man veris
|
|
44
|
+ * @con uart
|
|
45
|
+ * @web http://cpengineerscorp.net/veris-industries-hwxphtx.html
|
|
46
|
+ *
|
|
47
|
+ * @brief UPM API for the Veris HWXPXX Hardware Protocol Humidity and Temperature Sensor
|
|
48
|
+ *
|
|
49
|
+ * This module implements support for the Veris HWXPHTX Hardware
|
|
50
|
+ * Protocol Humidity and Temperature Sensor family. It uses MODBUS
|
|
51
|
+ * over an RS485 interface. You must have libmodbus v3.1.2 (or
|
|
52
|
+ * greater) installed to compile and use this driver.
|
|
53
|
+ *
|
|
54
|
+ * This module was developed using libmodbus 3.1.2, and the HWXPHTX.
|
|
55
|
+ * This sensor supports humidity, and optionally, temperature,
|
|
56
|
+ * slider switch, and override switch reporting. The HWXPHTX used to
|
|
57
|
+ * develop this driver did not include the optional slider or
|
|
58
|
+ * override switches, however support for them is provided.
|
|
59
|
+ *
|
|
60
|
+ * It was developed using an RS232->RS485 inteface. You cannot use
|
|
61
|
+ * the built in MCU TTL UART pins for accessing this device -- you
|
|
62
|
+ * must use a full serial RS232->RS485 interface connected via USB.
|
|
63
|
+ *
|
|
64
|
+ * @snippet hwxpxx.cxx Interesting
|
|
65
|
+ */
|
|
66
|
+
|
|
67
|
+ class HWXPXX {
|
|
68
|
+ public:
|
|
69
|
+ // MODBUS input registers
|
|
70
|
+ typedef enum {
|
|
71
|
+ INPUT_HUMIDITY = 0x0000,
|
|
72
|
+ // optional temp sensor
|
|
73
|
+ INPUT_TEMPERATURE = 0x0001,
|
|
74
|
+ // optional slider input
|
|
75
|
+ INPUT_SLIDER = 0x0002
|
|
76
|
+ } INPUT_REGS_T;
|
|
77
|
+
|
|
78
|
+ // MODBUS coil registers
|
|
79
|
+ typedef enum {
|
|
80
|
+ // device reports in C or F?
|
|
81
|
+ COIL_TEMP_SCALE = 0x0000,
|
|
82
|
+
|
|
83
|
+ // optional override button status
|
|
84
|
+ COIL_OVERRIDE = 0x0001
|
|
85
|
+ } COIL_REGS_T;
|
|
86
|
+
|
|
87
|
+ // MODBUS holding registers
|
|
88
|
+ typedef enum {
|
|
89
|
+ HOLDING_TEMP_OFFSET = 0x0000,
|
|
90
|
+ HOLDING_HUM_OFFSET = 0x0001
|
|
91
|
+ } HOLDING_REGS_T;
|
|
92
|
+
|
|
93
|
+ /**
|
|
94
|
+ * HWXPXX constructor
|
|
95
|
+ *
|
|
96
|
+ * @param device Path to the serial device
|
|
97
|
+ * @param address The MODBUS slave address
|
|
98
|
+ * @param baud The baudrate of the device. Default: 19200
|
|
99
|
+ * @param bits The number of bits per byte. Default: 8
|
|
100
|
+ * @param parity The parity of the connection, 'N' for None, 'E'
|
|
101
|
+ * for Even, 'O' for Odd. Default: 'N'
|
|
102
|
+ * @param stopBits The number of stop bits. Default: 2
|
|
103
|
+ */
|
|
104
|
+ HWXPXX(std::string device, int address, int baud=19200, int bits=8,
|
|
105
|
+ char parity='N', int stopBits=2);
|
|
106
|
+
|
|
107
|
+ /**
|
|
108
|
+ * HWXPXX Destructor
|
|
109
|
+ */
|
|
110
|
+ ~HWXPXX();
|
|
111
|
+
|
|
112
|
+ /**
|
|
113
|
+ * Read current values from the sensor and update internal stored
|
|
114
|
+ * values. This method must be called prior to querying any
|
|
115
|
+ * values, such as temperature, humidity, override switch status,
|
|
116
|
+ * or slider switch status.
|
|
117
|
+ */
|
|
118
|
+ void update();
|
|
119
|
+
|
|
120
|
+ /**
|
|
121
|
+ * Get the current temperature. update() must have been called
|
|
122
|
+ * prior to calling this method. If this option was not
|
|
123
|
+ * installed, this method will always return 0C/0F, depending on
|
|
124
|
+ * the scale the device is operating in natively.
|
|
125
|
+ *
|
|
126
|
+ * @param fahrenheit true to return the temperature in degrees
|
|
127
|
+ * fahrenheit, false to return the temperature in degrees celcius.
|
|
128
|
+ * The default is false (degrees Celcius).
|
|
129
|
+ * @return The last temperature reading in Celcius or Fahrenheit
|
|
130
|
+ */
|
|
131
|
+ float getTemperature(bool fahrenheit=false);
|
|
132
|
+
|
|
133
|
+ /**
|
|
134
|
+ * Get the current relative humidity. update() must have been called
|
|
135
|
+ * prior to calling this method.
|
|
136
|
+ *
|
|
137
|
+ * @return The last humidity reading
|
|
138
|
+ */
|
|
139
|
+ float getHumidity();
|
|
140
|
+
|
|
141
|
+ /**
|
|
142
|
+ * Get the current slider switch position. update() must have
|
|
143
|
+ * been called prior to calling this method. This returns a value
|
|
144
|
+ * between 0-100 corresponding to the position of the slider
|
|
145
|
+ * switch. If this option is not installed, this method will
|
|
146
|
+ * always return 0.
|
|
147
|
+ *
|
|
148
|
+ * @return The last slider switch reading
|
|
149
|
+ */
|
|
150
|
+ int getSlider();
|
|
151
|
+
|
|
152
|
+ /**
|
|
153
|
+ * Get the current override switch status. update() must have
|
|
154
|
+ * been called prior to calling this method. This returns true if
|
|
155
|
+ * the override switch was pressed. Use clearOverrideSwitch() to
|
|
156
|
+ * reset this value to false. If this option is not installed,
|
|
157
|
+ * then this method will always return false. It is not possible
|
|
158
|
+ * to programatically set this value to true - that can only be
|
|
159
|
+ * done by physically pressing the override switch.
|
|
160
|
+ *
|
|
161
|
+ * @return The last overide switch status reading
|
|
162
|
+ */
|
|
163
|
+ bool getOverrideSwitchStatus();
|
|
164
|
+
|
|
165
|
+ /**
|
|
166
|
+ * Clear the override switch status (set it to false). If this
|
|
167
|
+ * option is not installed, then this method will have no effect
|
|
168
|
+ * (the overide switch status will always be false).
|
|
169
|
+ *
|
|
170
|
+ */
|
|
171
|
+ void clearOverrideSwitch();
|
|
172
|
+
|
|
173
|
+ /**
|
|
174
|
+ * Return the current temperature offset stored on the device.
|
|
175
|
+ * This is a value between -50 and +50, specified in tenths of a
|
|
176
|
+ * degree in whatever scale (Celcius or Fahrenheit) is in use.
|
|
177
|
+ * This offset is applied to the returned temperature reading by the
|
|
178
|
+ * device.
|
|
179
|
+ *
|
|
180
|
+ * @return The current temperature offset in tenths of a degree
|
|
181
|
+ */
|
|
182
|
+ int getTemperatureOffset();
|
|
183
|
+
|
|
184
|
+ /**
|
|
185
|
+ * Return the current humidity offset stored on the device. This
|
|
186
|
+ * is a value between -100 and +100, specified in tenths of a
|
|
187
|
+ * percent. This offset is applied to the returned humidity
|
|
188
|
+ * reading by the device.
|
|
189
|
+ *
|
|
190
|
+ * @return The current humidity offset in tenths of a percent
|
|
191
|
+ */
|
|
192
|
+ int getHumidityOffset();
|
|
193
|
+
|
|
194
|
+ /**
|
|
195
|
+ * Set the stored temperature offset on the device. This is a
|
|
196
|
+ * value between -50 and +50, specified in tenths of a degree in
|
|
197
|
+ * what ever scale (Celcius or Fahrenheit) is in use. This offset
|
|
198
|
+ * is applied to the returned temperature reading by the device.
|
|
199
|
+ *
|
|
200
|
+ * @param offset Offset in tenths of a degree with a range of -50 to +50
|
|
201
|
+ */
|
|
202
|
+ void setTemperatureOffset(int offset);
|
|
203
|
+
|
|
204
|
+ /**
|
|
205
|
+ * Set the stored humidity offset on the device. This is a value
|
|
206
|
+ * between -100 and +100, specified in tenths of a percent. This
|
|
207
|
+ * offset is applied to the returned humidity reading by the
|
|
208
|
+ * device.
|
|
209
|
+ *
|
|
210
|
+ * @param offset Offset in tenths of a percent with a range of -100 to +100
|
|
211
|
+ */
|
|
212
|
+ void setHumidityOffset(int offset);
|
|
213
|
+
|
|
214
|
+ /**
|
|
215
|
+ * Set the temperature scale used by the device. This driver
|
|
216
|
+ * detects this setting automatically and adjusts itself
|
|
217
|
+ * accordingly, so this is generally never needed. It is used to
|
|
218
|
+ * set the native reporting scale of the temperature either in
|
|
219
|
+ * degrees Celcius or Fahrenheit. Its setting will not affect
|
|
220
|
+ * the operation of getTemperature().
|
|
221
|
+ *
|
|
222
|
+ * @param fahrenheit true to set Fahrenheit, false to set Celcius
|
|
223
|
+ */
|
|
224
|
+ void setTemperatureScale(bool fahrenheit);
|
|
225
|
+
|
|
226
|
+ /**
|
|
227
|
+ * Return a string corresponding the the device's MODBUS slave ID.
|
|
228
|
+ * This includes information such as the manufacturer, device
|
|
229
|
+ * model number and serial number.
|
|
230
|
+ *
|
|
231
|
+ * @return string represnting the MODBUS slave ID
|
|
232
|
+ */
|
|
233
|
+ std::string getSlaveID();
|
|
234
|
+
|
|
235
|
+ /**
|
|
236
|
+ * Set a new MODBUS slave address. This is useful if you have
|
|
237
|
+ * multiple HWXPXX devices on a single bus. When this method is
|
|
238
|
+ * called, the current temperature scale is re-read so that
|
|
239
|
+ * further update() calls can work correctly.
|
|
240
|
+ *
|
|
241
|
+ * @param addr The new slave address to set
|
|
242
|
+ */
|
|
243
|
+ void setSlaveAddress(int addr);
|
|
244
|
+
|
|
245
|
+ /**
|
|
246
|
+ * Enable or disable debugging output. This primarily enables and
|
|
247
|
+ * disables libmodbus debugging output.
|
|
248
|
+ *
|
|
249
|
+ * @param enable true to enable debugging, false otherwise
|
|
250
|
+ */
|
|
251
|
+ void setDebug(bool enable);
|
|
252
|
+
|
|
253
|
+ protected:
|
|
254
|
+ // input registers
|
|
255
|
+ int readInputRegs(INPUT_REGS_T reg, int len, uint16_t *buf);
|
|
256
|
+ uint16_t readInputReg(INPUT_REGS_T reg);
|
|
257
|
+
|
|
258
|
+ // coils
|
|
259
|
+ int readCoils(COIL_REGS_T reg, int numBits, uint8_t *buf);
|
|
260
|
+ bool readCoil(COIL_REGS_T reg);
|
|
261
|
+ void writeCoil(COIL_REGS_T reg, bool val);
|
|
262
|
+
|
|
263
|
+ // holding registers
|
|
264
|
+ int readHoldingRegs(HOLDING_REGS_T reg, int len, uint16_t *buf);
|
|
265
|
+ uint16_t readHoldingReg(HOLDING_REGS_T reg);
|
|
266
|
+ void writeHoldingReg(HOLDING_REGS_T reg, int value);
|
|
267
|
+
|
|
268
|
+ // MODBUS context
|
|
269
|
+ modbus_t *m_mbContext;
|
|
270
|
+
|
|
271
|
+ // is the device reporting in C or F?
|
|
272
|
+ bool m_isCelcius;
|
|
273
|
+
|
|
274
|
+ private:
|
|
275
|
+ bool m_debugging;
|
|
276
|
+
|
|
277
|
+ // data
|
|
278
|
+ float m_temperature;
|
|
279
|
+ float m_humidity; // relative
|
|
280
|
+ int m_slider; // optional slider value
|
|
281
|
+ bool m_override; // status of override switch
|
|
282
|
+ };
|
|
283
|
+}
|