moel@171
|
1 |
/*
|
moel@171
|
2 |
|
moel@171
|
3 |
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
moel@171
|
4 |
|
moel@171
|
5 |
The contents of this file are subject to the Mozilla Public License Version
|
moel@171
|
6 |
1.1 (the "License"); you may not use this file except in compliance with
|
moel@171
|
7 |
the License. You may obtain a copy of the License at
|
moel@171
|
8 |
|
moel@171
|
9 |
http://www.mozilla.org/MPL/
|
moel@171
|
10 |
|
moel@171
|
11 |
Software distributed under the License is distributed on an "AS IS" basis,
|
moel@171
|
12 |
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
moel@171
|
13 |
for the specific language governing rights and limitations under the License.
|
moel@171
|
14 |
|
moel@171
|
15 |
The Original Code is the Open Hardware Monitor code.
|
moel@171
|
16 |
|
moel@171
|
17 |
The Initial Developer of the Original Code is
|
moel@171
|
18 |
Michael Möller <m.moeller@gmx.ch>.
|
moel@261
|
19 |
Portions created by the Initial Developer are Copyright (C) 2010-2011
|
moel@171
|
20 |
the Initial Developer. All Rights Reserved.
|
moel@171
|
21 |
|
moel@171
|
22 |
Contributor(s):
|
moel@171
|
23 |
|
moel@171
|
24 |
Alternatively, the contents of this file may be used under the terms of
|
moel@171
|
25 |
either the GNU General Public License Version 2 or later (the "GPL"), or
|
moel@171
|
26 |
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
moel@171
|
27 |
in which case the provisions of the GPL or the LGPL are applicable instead
|
moel@171
|
28 |
of those above. If you wish to allow use of your version of this file only
|
moel@171
|
29 |
under the terms of either the GPL or the LGPL, and not to allow others to
|
moel@171
|
30 |
use your version of this file under the terms of the MPL, indicate your
|
moel@171
|
31 |
decision by deleting the provisions above and replace them with the notice
|
moel@171
|
32 |
and other provisions required by the GPL or the LGPL. If you do not delete
|
moel@171
|
33 |
the provisions above, a recipient may use your version of this file under
|
moel@171
|
34 |
the terms of any one of the MPL, the GPL or the LGPL.
|
moel@171
|
35 |
|
moel@171
|
36 |
*/
|
moel@171
|
37 |
|
moel@171
|
38 |
using System;
|
moel@182
|
39 |
using System.Globalization;
|
moel@171
|
40 |
using System.IO;
|
moel@171
|
41 |
using System.IO.Ports;
|
moel@171
|
42 |
using System.Text;
|
moel@171
|
43 |
using System.Text.RegularExpressions;
|
moel@171
|
44 |
using System.Threading;
|
moel@171
|
45 |
|
moel@171
|
46 |
namespace OpenHardwareMonitor.Hardware.Heatmaster {
|
moel@182
|
47 |
internal class Heatmaster : Hardware, IDisposable {
|
moel@171
|
48 |
|
moel@195
|
49 |
private readonly string portName;
|
moel@171
|
50 |
private SerialPort serialPort;
|
moel@171
|
51 |
|
moel@195
|
52 |
private readonly int hardwareRevision;
|
moel@195
|
53 |
private readonly int firmwareRevision;
|
moel@195
|
54 |
private readonly int firmwareCRC;
|
moel@171
|
55 |
|
moel@195
|
56 |
private readonly Sensor[] fans;
|
moel@195
|
57 |
private readonly Sensor[] controls;
|
moel@195
|
58 |
private readonly Sensor[] temperatures;
|
moel@195
|
59 |
private readonly Sensor[] flows;
|
moel@195
|
60 |
private readonly Sensor[] relays;
|
moel@171
|
61 |
|
moel@195
|
62 |
private readonly bool available;
|
moel@171
|
63 |
|
moel@195
|
64 |
private readonly StringBuilder buffer = new StringBuilder();
|
moel@172
|
65 |
|
moel@171
|
66 |
private string ReadLine(int timeout) {
|
moel@171
|
67 |
int i = 0;
|
moel@171
|
68 |
StringBuilder builder = new StringBuilder();
|
moel@171
|
69 |
while (i <= timeout) {
|
moel@171
|
70 |
while (serialPort.BytesToRead > 0) {
|
moel@171
|
71 |
byte b = (byte)serialPort.ReadByte();
|
moel@171
|
72 |
switch (b) {
|
moel@171
|
73 |
case 0xAA: return ((char)b).ToString();
|
moel@171
|
74 |
case 0x0D: return builder.ToString();
|
moel@171
|
75 |
default: builder.Append((char)b); break;
|
moel@171
|
76 |
}
|
moel@171
|
77 |
}
|
moel@171
|
78 |
i++;
|
moel@171
|
79 |
Thread.Sleep(1);
|
moel@171
|
80 |
}
|
moel@171
|
81 |
throw new TimeoutException();
|
moel@171
|
82 |
}
|
moel@171
|
83 |
|
moel@171
|
84 |
private string ReadField(int device, char field) {
|
moel@171
|
85 |
serialPort.WriteLine("[0:" + device + "]R" + field);
|
moel@171
|
86 |
for (int i = 0; i < 5; i++) {
|
moel@172
|
87 |
string s = ReadLine(200);
|
moel@182
|
88 |
Match match = Regex.Match(s, @"-\[0:" +
|
moel@182
|
89 |
device.ToString(CultureInfo.InvariantCulture) + @"\]R" +
|
moel@182
|
90 |
Regex.Escape(field.ToString(CultureInfo.InvariantCulture)) + ":(.*)");
|
moel@171
|
91 |
if (match.Success)
|
moel@171
|
92 |
return match.Groups[1].Value;
|
moel@171
|
93 |
}
|
moel@171
|
94 |
return null;
|
moel@171
|
95 |
}
|
moel@171
|
96 |
|
moel@195
|
97 |
protected string ReadString(int device, char field) {
|
moel@171
|
98 |
string s = ReadField(device, field);
|
moel@171
|
99 |
if (s != null && s[0] == '"' && s[s.Length - 1] == '"')
|
moel@171
|
100 |
return s.Substring(1, s.Length - 2);
|
moel@171
|
101 |
else
|
moel@171
|
102 |
return null;
|
moel@171
|
103 |
}
|
moel@171
|
104 |
|
moel@195
|
105 |
protected int ReadInteger(int device, char field) {
|
moel@171
|
106 |
string s = ReadField(device, field);
|
moel@171
|
107 |
int i;
|
moel@171
|
108 |
if (int.TryParse(s, out i))
|
moel@171
|
109 |
return i;
|
moel@171
|
110 |
else
|
moel@171
|
111 |
return 0;
|
moel@171
|
112 |
}
|
moel@171
|
113 |
|
moel@171
|
114 |
private bool WriteField(int device, char field, string value) {
|
moel@171
|
115 |
serialPort.WriteLine("[0:" + device + "]W" + field + ":" + value);
|
moel@171
|
116 |
for (int i = 0; i < 5; i++) {
|
moel@172
|
117 |
string s = ReadLine(200);
|
moel@182
|
118 |
Match match = Regex.Match(s, @"-\[0:" +
|
moel@182
|
119 |
device.ToString(CultureInfo.InvariantCulture) + @"\]W" +
|
moel@182
|
120 |
Regex.Escape(field.ToString(CultureInfo.InvariantCulture)) +
|
moel@182
|
121 |
":" + value);
|
moel@171
|
122 |
if (match.Success)
|
moel@171
|
123 |
return true;
|
moel@171
|
124 |
}
|
moel@171
|
125 |
return false;
|
moel@171
|
126 |
}
|
moel@171
|
127 |
|
moel@195
|
128 |
protected bool WriteInteger(int device, char field, int value) {
|
moel@182
|
129 |
return WriteField(device, field,
|
moel@182
|
130 |
value.ToString(CultureInfo.InvariantCulture));
|
moel@171
|
131 |
}
|
moel@171
|
132 |
|
moel@195
|
133 |
protected bool WriteString(int device, char field, string value) {
|
moel@171
|
134 |
return WriteField(device, field, '"' + value + '"');
|
moel@171
|
135 |
}
|
moel@171
|
136 |
|
moel@275
|
137 |
public Heatmaster(string portName, ISettings settings)
|
moel@275
|
138 |
: base("Heatmaster", new Identifier("heatmaster",
|
moel@275
|
139 |
portName.TrimStart(new [] {'/'}).ToLowerInvariant()), settings)
|
moel@275
|
140 |
{
|
moel@171
|
141 |
this.portName = portName;
|
moel@171
|
142 |
try {
|
moel@171
|
143 |
serialPort = new SerialPort(portName, 38400, Parity.None, 8,
|
moel@171
|
144 |
StopBits.One);
|
moel@171
|
145 |
serialPort.Open();
|
moel@171
|
146 |
serialPort.NewLine = ((char)0x0D).ToString();
|
moel@171
|
147 |
|
moel@171
|
148 |
hardwareRevision = ReadInteger(0, 'H');
|
moel@171
|
149 |
firmwareRevision = ReadInteger(0, 'V');
|
moel@171
|
150 |
firmwareCRC = ReadInteger(0, 'C');
|
moel@171
|
151 |
|
moel@173
|
152 |
int fanCount = Math.Min(ReadInteger(32, '?'), 4);
|
moel@173
|
153 |
int temperatureCount = Math.Min(ReadInteger(48, '?'), 6);
|
moel@173
|
154 |
int flowCount = Math.Min(ReadInteger(64, '?'), 1);
|
moel@173
|
155 |
int relayCount = Math.Min(ReadInteger(80, '?'), 1);
|
moel@171
|
156 |
|
moel@171
|
157 |
fans = new Sensor[fanCount];
|
moel@171
|
158 |
controls = new Sensor[fanCount];
|
moel@171
|
159 |
for (int i = 0; i < fanCount; i++) {
|
moel@171
|
160 |
int device = 33 + i;
|
moel@171
|
161 |
string name = ReadString(device, 'C');
|
moel@171
|
162 |
fans[i] = new Sensor(name, device, SensorType.Fan, this, settings);
|
moel@171
|
163 |
fans[i].Value = ReadInteger(device, 'R');
|
moel@171
|
164 |
ActivateSensor(fans[i]);
|
moel@171
|
165 |
controls[i] =
|
moel@171
|
166 |
new Sensor(name, device, SensorType.Control, this, settings);
|
moel@171
|
167 |
controls[i].Value = (100 / 255.0f) * ReadInteger(device, 'P');
|
moel@171
|
168 |
ActivateSensor(controls[i]);
|
moel@195
|
169 |
}
|
moel@171
|
170 |
|
moel@171
|
171 |
temperatures = new Sensor[temperatureCount];
|
moel@171
|
172 |
for (int i = 0; i < temperatureCount; i++) {
|
moel@171
|
173 |
int device = 49 + i;
|
moel@171
|
174 |
string name = ReadString(device, 'C');
|
moel@171
|
175 |
temperatures[i] =
|
moel@171
|
176 |
new Sensor(name, device, SensorType.Temperature, this, settings);
|
moel@171
|
177 |
int value = ReadInteger(device, 'T');
|
moel@171
|
178 |
temperatures[i].Value = 0.1f * value;
|
moel@171
|
179 |
if (value != -32768)
|
moel@171
|
180 |
ActivateSensor(temperatures[i]);
|
moel@171
|
181 |
}
|
moel@171
|
182 |
|
moel@171
|
183 |
flows = new Sensor[flowCount];
|
moel@171
|
184 |
for (int i = 0; i < flowCount; i++) {
|
moel@171
|
185 |
int device = 65 + i;
|
moel@171
|
186 |
string name = ReadString(device, 'C');
|
moel@171
|
187 |
flows[i] = new Sensor(name, device, SensorType.Flow, this, settings);
|
moel@171
|
188 |
flows[i].Value = 0.1f * ReadInteger(device, 'L');
|
moel@171
|
189 |
ActivateSensor(flows[i]);
|
moel@171
|
190 |
}
|
moel@171
|
191 |
|
moel@171
|
192 |
relays = new Sensor[relayCount];
|
moel@171
|
193 |
for (int i = 0; i < relayCount; i++) {
|
moel@171
|
194 |
int device = 81 + i;
|
moel@171
|
195 |
string name = ReadString(device, 'C');
|
moel@171
|
196 |
relays[i] =
|
moel@171
|
197 |
new Sensor(name, device, SensorType.Control, this, settings);
|
moel@171
|
198 |
relays[i].Value = 100 * ReadInteger(device, 'S');
|
moel@171
|
199 |
ActivateSensor(relays[i]);
|
moel@171
|
200 |
}
|
moel@171
|
201 |
|
moel@171
|
202 |
// set the update rate to 2 Hz
|
moel@171
|
203 |
WriteInteger(0, 'L', 2);
|
moel@171
|
204 |
|
moel@171
|
205 |
available = true;
|
moel@171
|
206 |
|
moel@171
|
207 |
} catch (IOException) { } catch (TimeoutException) { }
|
moel@171
|
208 |
}
|
moel@171
|
209 |
|
moel@171
|
210 |
public override HardwareType HardwareType {
|
moel@171
|
211 |
get { return HardwareType.Heatmaster; }
|
moel@171
|
212 |
}
|
moel@171
|
213 |
|
moel@172
|
214 |
private void ProcessUpdateLine(string line) {
|
moel@172
|
215 |
Match match = Regex.Match(line, @">\[0:(\d+)\]([0-9:\|-]+)");
|
moel@172
|
216 |
if (match.Success) {
|
moel@172
|
217 |
int device;
|
moel@172
|
218 |
if (int.TryParse(match.Groups[1].Value, out device)) {
|
moel@172
|
219 |
foreach (string s in match.Groups[2].Value.Split('|')) {
|
moel@172
|
220 |
string[] strings = s.Split(':');
|
moel@260
|
221 |
int[] ints = new int[strings.Length];
|
moel@260
|
222 |
bool valid = true;
|
moel@172
|
223 |
for (int i = 0; i < ints.Length; i++)
|
moel@260
|
224 |
if (!int.TryParse(strings[i], out ints[i])) {
|
moel@260
|
225 |
valid = false;
|
moel@260
|
226 |
break;
|
moel@260
|
227 |
}
|
moel@260
|
228 |
if (!valid)
|
moel@260
|
229 |
continue;
|
moel@172
|
230 |
switch (device) {
|
moel@172
|
231 |
case 32:
|
moel@172
|
232 |
if (ints.Length == 3 && ints[0] <= fans.Length) {
|
moel@172
|
233 |
fans[ints[0] - 1].Value = ints[1];
|
moel@172
|
234 |
controls[ints[0] - 1].Value = (100 / 255.0f) * ints[2];
|
moel@172
|
235 |
}
|
moel@172
|
236 |
break;
|
moel@172
|
237 |
case 48:
|
moel@172
|
238 |
if (ints.Length == 2 && ints[0] <= temperatures.Length)
|
moel@172
|
239 |
temperatures[ints[0] - 1].Value = 0.1f * ints[1];
|
moel@172
|
240 |
break;
|
moel@172
|
241 |
case 64:
|
moel@172
|
242 |
if (ints.Length == 3 && ints[0] <= flows.Length)
|
moel@172
|
243 |
flows[ints[0] - 1].Value = 0.1f * ints[1];
|
moel@172
|
244 |
break;
|
moel@172
|
245 |
case 80:
|
moel@172
|
246 |
if (ints.Length == 2 && ints[0] <= relays.Length)
|
moel@172
|
247 |
relays[ints[0] - 1].Value = 100 * ints[1];
|
moel@172
|
248 |
break;
|
moel@172
|
249 |
}
|
moel@172
|
250 |
}
|
moel@172
|
251 |
}
|
moel@172
|
252 |
}
|
moel@172
|
253 |
}
|
moel@172
|
254 |
|
moel@171
|
255 |
public override void Update() {
|
moel@171
|
256 |
if (!available)
|
moel@171
|
257 |
return;
|
moel@171
|
258 |
|
moel@261
|
259 |
while (serialPort.IsOpen && serialPort.BytesToRead > 0) {
|
moel@172
|
260 |
byte b = (byte)serialPort.ReadByte();
|
moel@172
|
261 |
if (b == 0x0D) {
|
moel@172
|
262 |
ProcessUpdateLine(buffer.ToString());
|
moel@172
|
263 |
buffer.Length = 0;
|
moel@172
|
264 |
} else {
|
moel@172
|
265 |
buffer.Append((char)b);
|
moel@171
|
266 |
}
|
moel@171
|
267 |
}
|
moel@171
|
268 |
}
|
moel@171
|
269 |
|
moel@171
|
270 |
public override string GetReport() {
|
moel@171
|
271 |
StringBuilder r = new StringBuilder();
|
moel@171
|
272 |
|
moel@171
|
273 |
r.AppendLine("Heatmaster");
|
moel@171
|
274 |
r.AppendLine();
|
moel@171
|
275 |
r.Append("Port: ");
|
moel@171
|
276 |
r.AppendLine(portName);
|
moel@171
|
277 |
r.Append("Hardware Revision: ");
|
moel@182
|
278 |
r.AppendLine(hardwareRevision.ToString(CultureInfo.InvariantCulture));
|
moel@171
|
279 |
r.Append("Firmware Revision: ");
|
moel@182
|
280 |
r.AppendLine(firmwareRevision.ToString(CultureInfo.InvariantCulture));
|
moel@171
|
281 |
r.Append("Firmware CRC: ");
|
moel@182
|
282 |
r.AppendLine(firmwareCRC.ToString(CultureInfo.InvariantCulture));
|
moel@171
|
283 |
r.AppendLine();
|
moel@171
|
284 |
|
moel@171
|
285 |
return r.ToString();
|
moel@171
|
286 |
}
|
moel@171
|
287 |
|
moel@298
|
288 |
public override void Close() {
|
moel@171
|
289 |
serialPort.Close();
|
moel@182
|
290 |
serialPort.Dispose();
|
moel@182
|
291 |
serialPort = null;
|
moel@298
|
292 |
base.Close();
|
moel@182
|
293 |
}
|
moel@182
|
294 |
|
moel@182
|
295 |
public void Dispose() {
|
moel@182
|
296 |
if (serialPort != null) {
|
moel@182
|
297 |
serialPort.Dispose();
|
moel@182
|
298 |
serialPort = null;
|
moel@182
|
299 |
}
|
moel@171
|
300 |
}
|
moel@171
|
301 |
}
|
moel@171
|
302 |
}
|