Fixed a few problems with the Heatmaster.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
40 using System.IO.Ports;
41 using System.Security;
43 using System.Threading;
44 using Microsoft.Win32;
46 namespace OpenHardwareMonitor.Hardware.Heatmaster {
47 internal class HeatmasterGroup : IGroup {
49 private List<Heatmaster> hardware = new List<Heatmaster>();
50 private StringBuilder report = new StringBuilder();
52 private static string ReadLine(SerialPort port, int timeout) {
54 StringBuilder builder = new StringBuilder();
56 while (port.BytesToRead > 0) {
57 byte b = (byte)port.ReadByte();
59 case 0xAA: return ((char)b).ToString();
60 case 0x0D: return builder.ToString();
61 default: builder.Append((char)b); break;
67 throw new TimeoutException();
70 private static string[] GetRegistryPortNames() {
71 List<string> result = new List<string>();
73 RegistryKey key = Registry.LocalMachine.OpenSubKey(
74 @"SYSTEM\CurrentControlSet\Enum\USB\Vid_10c4&Pid_ea60&Mi_00");
76 foreach (string subKeyName in key.GetSubKeyNames()) {
78 key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
80 string name = subKey.GetValue("PortName") as string;
82 result.Add((string)name);
86 } catch (SecurityException) { }
87 return result.ToArray();
90 public HeatmasterGroup(ISettings settings) {
92 // No implementation for Heatmaster on Unix systems
93 int p = (int)System.Environment.OSVersion.Platform;
94 if ((p == 4) || (p == 128))
97 string[] portNames = GetRegistryPortNames();
98 for (int i = portNames.Length - 1; i >= 0; i--) {
101 SerialPort serialPort =
102 new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One);
103 serialPort.NewLine = ((char)0x0D).ToString();
105 bool isValid = false;
106 report.Append("Port Name: "); report.AppendLine(portNames[i]);
110 } catch (UnauthorizedAccessException) {
111 report.AppendLine("Exception: Access Denied");
114 if (serialPort.IsOpen) {
115 serialPort.DiscardInBuffer();
116 serialPort.DiscardOutBuffer();
117 serialPort.Write(new byte[] { 0xAA }, 0, 1);
120 while (serialPort.BytesToRead == 0 && j < 10) {
124 if (serialPort.BytesToRead > 0) {
126 while (serialPort.BytesToRead > 0 && !flag) {
127 flag |= (serialPort.ReadByte() == 0xAA);
130 serialPort.WriteLine("[0:0]RH");
135 string line = ReadLine(serialPort, 100);
136 if (line.StartsWith("-[0:0]RH:")) {
137 int.TryParse(line.Substring(9), out revision);
142 isValid = (revision == 770);
144 report.Append("Status: Wrong Hardware Revision " +
145 revision.ToString());
147 } catch (TimeoutException) {
148 report.AppendLine("Status: Timeout Reading Revision");
151 report.AppendLine("Status: Wrong Startflag");
154 report.AppendLine("Status: No Response");
156 serialPort.DiscardInBuffer();
158 serialPort.Dispose();
160 report.AppendLine("Status: Port not Open");
163 report.AppendLine("Status: OK");
164 hardware.Add(new Heatmaster(portNames[i], settings));
167 } catch (Exception e) {
168 report.AppendLine(e.ToString());
174 public IHardware[] Hardware {
176 return hardware.ToArray();
180 public string GetReport() {
181 if (report.Length > 0) {
182 report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine +
183 Environment.NewLine);
185 return report.ToString();
190 public void Close() {
191 foreach (Heatmaster heatmaster in hardware)