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@171
|
19 |
Portions created by the Initial Developer are Copyright (C) 2010
|
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@171
|
39 |
using System.Collections.Generic;
|
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@171
|
47 |
internal class Heatmaster : Hardware {
|
moel@171
|
48 |
|
moel@171
|
49 |
private string portName;
|
moel@171
|
50 |
private SerialPort serialPort;
|
moel@171
|
51 |
|
moel@171
|
52 |
private int hardwareRevision;
|
moel@171
|
53 |
private int firmwareRevision;
|
moel@171
|
54 |
private int firmwareCRC;
|
moel@171
|
55 |
|
moel@171
|
56 |
private Sensor[] fans;
|
moel@171
|
57 |
private Sensor[] controls;
|
moel@171
|
58 |
private Sensor[] temperatures;
|
moel@171
|
59 |
private Sensor[] flows;
|
moel@171
|
60 |
private Sensor[] relays;
|
moel@171
|
61 |
|
moel@171
|
62 |
private bool available = false;
|
moel@171
|
63 |
|
moel@171
|
64 |
private string ReadLine(int timeout) {
|
moel@171
|
65 |
int i = 0;
|
moel@171
|
66 |
StringBuilder builder = new StringBuilder();
|
moel@171
|
67 |
while (i <= timeout) {
|
moel@171
|
68 |
while (serialPort.BytesToRead > 0) {
|
moel@171
|
69 |
byte b = (byte)serialPort.ReadByte();
|
moel@171
|
70 |
switch (b) {
|
moel@171
|
71 |
case 0xAA: return ((char)b).ToString();
|
moel@171
|
72 |
case 0x0D: return builder.ToString();
|
moel@171
|
73 |
default: builder.Append((char)b); break;
|
moel@171
|
74 |
}
|
moel@171
|
75 |
}
|
moel@171
|
76 |
i++;
|
moel@171
|
77 |
Thread.Sleep(1);
|
moel@171
|
78 |
}
|
moel@171
|
79 |
throw new TimeoutException();
|
moel@171
|
80 |
}
|
moel@171
|
81 |
|
moel@171
|
82 |
private string ReadField(int device, char field) {
|
moel@171
|
83 |
serialPort.WriteLine("[0:" + device + "]R" + field);
|
moel@171
|
84 |
for (int i = 0; i < 5; i++) {
|
moel@171
|
85 |
string s = ReadLine(100);
|
moel@171
|
86 |
Match match = Regex.Match(s, @"-\[0:" + device.ToString() + @"\]R" +
|
moel@171
|
87 |
Regex.Escape(field.ToString()) + ":(.*)");
|
moel@171
|
88 |
if (match.Success)
|
moel@171
|
89 |
return match.Groups[1].Value;
|
moel@171
|
90 |
}
|
moel@171
|
91 |
return null;
|
moel@171
|
92 |
}
|
moel@171
|
93 |
|
moel@171
|
94 |
private string ReadString(int device, char field) {
|
moel@171
|
95 |
string s = ReadField(device, field);
|
moel@171
|
96 |
if (s != null && s[0] == '"' && s[s.Length - 1] == '"')
|
moel@171
|
97 |
return s.Substring(1, s.Length - 2);
|
moel@171
|
98 |
else
|
moel@171
|
99 |
return null;
|
moel@171
|
100 |
}
|
moel@171
|
101 |
|
moel@171
|
102 |
private int ReadInteger(int device, char field) {
|
moel@171
|
103 |
string s = ReadField(device, field);
|
moel@171
|
104 |
int i;
|
moel@171
|
105 |
if (int.TryParse(s, out i))
|
moel@171
|
106 |
return i;
|
moel@171
|
107 |
else
|
moel@171
|
108 |
return 0;
|
moel@171
|
109 |
}
|
moel@171
|
110 |
|
moel@171
|
111 |
private bool WriteField(int device, char field, string value) {
|
moel@171
|
112 |
serialPort.WriteLine("[0:" + device + "]W" + field + ":" + value);
|
moel@171
|
113 |
for (int i = 0; i < 5; i++) {
|
moel@171
|
114 |
string s = ReadLine(100);
|
moel@171
|
115 |
Match match = Regex.Match(s, @"-\[0:" + device.ToString() + @"\]W" +
|
moel@171
|
116 |
Regex.Escape(field.ToString()) + ":" + value);
|
moel@171
|
117 |
if (match.Success)
|
moel@171
|
118 |
return true;
|
moel@171
|
119 |
}
|
moel@171
|
120 |
return false;
|
moel@171
|
121 |
}
|
moel@171
|
122 |
|
moel@171
|
123 |
private bool WriteInteger(int device, char field, int value) {
|
moel@171
|
124 |
return WriteField(device, field, value.ToString());
|
moel@171
|
125 |
}
|
moel@171
|
126 |
|
moel@171
|
127 |
private bool WriteString(int device, char field, string value) {
|
moel@171
|
128 |
return WriteField(device, field, '"' + value + '"');
|
moel@171
|
129 |
}
|
moel@171
|
130 |
|
moel@171
|
131 |
public Heatmaster(string portName, ISettings settings) {
|
moel@171
|
132 |
|
moel@171
|
133 |
this.portName = portName;
|
moel@171
|
134 |
try {
|
moel@171
|
135 |
serialPort = new SerialPort(portName, 38400, Parity.None, 8,
|
moel@171
|
136 |
StopBits.One);
|
moel@171
|
137 |
serialPort.Open();
|
moel@171
|
138 |
serialPort.NewLine = ((char)0x0D).ToString();
|
moel@171
|
139 |
|
moel@171
|
140 |
hardwareRevision = ReadInteger(0, 'H');
|
moel@171
|
141 |
firmwareRevision = ReadInteger(0, 'V');
|
moel@171
|
142 |
firmwareCRC = ReadInteger(0, 'C');
|
moel@171
|
143 |
|
moel@171
|
144 |
int fanCount = ReadInteger(32, '?');
|
moel@171
|
145 |
int temperatureCount = ReadInteger(48, '?');
|
moel@171
|
146 |
int flowCount = ReadInteger(64, '?');
|
moel@171
|
147 |
int relayCount = ReadInteger(80, '?');
|
moel@171
|
148 |
|
moel@171
|
149 |
fans = new Sensor[fanCount];
|
moel@171
|
150 |
controls = new Sensor[fanCount];
|
moel@171
|
151 |
for (int i = 0; i < fanCount; i++) {
|
moel@171
|
152 |
int device = 33 + i;
|
moel@171
|
153 |
string name = ReadString(device, 'C');
|
moel@171
|
154 |
fans[i] = new Sensor(name, device, SensorType.Fan, this, settings);
|
moel@171
|
155 |
fans[i].Value = ReadInteger(device, 'R');
|
moel@171
|
156 |
ActivateSensor(fans[i]);
|
moel@171
|
157 |
controls[i] =
|
moel@171
|
158 |
new Sensor(name, device, SensorType.Control, this, settings);
|
moel@171
|
159 |
controls[i].Value = (100 / 255.0f) * ReadInteger(device, 'P');
|
moel@171
|
160 |
ActivateSensor(controls[i]);
|
moel@171
|
161 |
}
|
moel@171
|
162 |
|
moel@171
|
163 |
for (int i = 0; i < fanCount; i++) {
|
moel@171
|
164 |
int device = 33 + i;
|
moel@171
|
165 |
string name = ReadString(device, 'C');
|
moel@171
|
166 |
|
moel@171
|
167 |
fans[i].Value = ReadInteger(device, 'R');
|
moel@171
|
168 |
ActivateSensor(fans[i]);
|
moel@171
|
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@171
|
214 |
public override Identifier Identifier {
|
moel@171
|
215 |
get {
|
moel@171
|
216 |
return new Identifier("heatmaster",
|
moel@171
|
217 |
serialPort.PortName.TrimStart(new char[]{'/'}).ToLowerInvariant());
|
moel@171
|
218 |
}
|
moel@171
|
219 |
}
|
moel@171
|
220 |
|
moel@171
|
221 |
public override string Name {
|
moel@171
|
222 |
get { return "Heatmaster"; }
|
moel@171
|
223 |
}
|
moel@171
|
224 |
|
moel@171
|
225 |
public override void Update() {
|
moel@171
|
226 |
if (!available)
|
moel@171
|
227 |
return;
|
moel@171
|
228 |
|
moel@171
|
229 |
while (serialPort.BytesToRead > 0) {
|
moel@171
|
230 |
Match match = Regex.Match(ReadLine(0), @">\[0:(\d+)\]([0-9:\|-]+)");
|
moel@171
|
231 |
if (match.Success) {
|
moel@171
|
232 |
int device;
|
moel@171
|
233 |
if (int.TryParse(match.Groups[1].Value, out device)) {
|
moel@171
|
234 |
foreach (string s in match.Groups[2].Value.Split('|')) {
|
moel@171
|
235 |
string[] strings = s.Split(':');
|
moel@171
|
236 |
int[] ints = new int[strings.Length];
|
moel@171
|
237 |
for (int i = 0; i < ints.Length; i++)
|
moel@171
|
238 |
ints[i] = int.Parse(strings[i]);
|
moel@171
|
239 |
switch (device) {
|
moel@171
|
240 |
case 32:
|
moel@171
|
241 |
if (ints.Length == 3 && ints[0] <= fans.Length) {
|
moel@171
|
242 |
fans[ints[0] - 1].Value = ints[1];
|
moel@171
|
243 |
controls[ints[0] - 1].Value = (100 / 255.0f) * ints[2];
|
moel@171
|
244 |
}
|
moel@171
|
245 |
break;
|
moel@171
|
246 |
case 48:
|
moel@171
|
247 |
if (ints.Length == 2 && ints[0] <= temperatures.Length)
|
moel@171
|
248 |
temperatures[ints[0] - 1].Value = 0.1f * ints[1];
|
moel@171
|
249 |
break;
|
moel@171
|
250 |
case 64:
|
moel@171
|
251 |
if (ints.Length == 3 && ints[0] <= flows.Length)
|
moel@171
|
252 |
flows[ints[0] - 1].Value = 0.1f * ints[1];
|
moel@171
|
253 |
break;
|
moel@171
|
254 |
case 80:
|
moel@171
|
255 |
if (ints.Length == 2 && ints[0] <= relays.Length)
|
moel@171
|
256 |
relays[ints[0] - 1].Value = 100 * ints[1];
|
moel@171
|
257 |
break;
|
moel@171
|
258 |
}
|
moel@171
|
259 |
}
|
moel@171
|
260 |
}
|
moel@171
|
261 |
}
|
moel@171
|
262 |
}
|
moel@171
|
263 |
}
|
moel@171
|
264 |
|
moel@171
|
265 |
public override string GetReport() {
|
moel@171
|
266 |
StringBuilder r = new StringBuilder();
|
moel@171
|
267 |
|
moel@171
|
268 |
r.AppendLine("Heatmaster");
|
moel@171
|
269 |
r.AppendLine();
|
moel@171
|
270 |
r.Append("Port: ");
|
moel@171
|
271 |
r.AppendLine(portName);
|
moel@171
|
272 |
r.Append("Hardware Revision: ");
|
moel@171
|
273 |
r.AppendLine(hardwareRevision.ToString());
|
moel@171
|
274 |
r.Append("Firmware Revision: ");
|
moel@171
|
275 |
r.AppendLine(firmwareRevision.ToString());
|
moel@171
|
276 |
r.Append("Firmware CRC: ");
|
moel@171
|
277 |
r.AppendLine(firmwareCRC.ToString());
|
moel@171
|
278 |
r.AppendLine();
|
moel@171
|
279 |
|
moel@171
|
280 |
return r.ToString();
|
moel@171
|
281 |
}
|
moel@171
|
282 |
|
moel@171
|
283 |
public void Close() {
|
moel@171
|
284 |
serialPort.Close();
|
moel@171
|
285 |
}
|
moel@171
|
286 |
}
|
moel@171
|
287 |
}
|