Fixed Issue 164.
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.Globalization;
41 using System.IO.Ports;
42 using System.Security;
44 using System.Threading;
45 using Microsoft.Win32;
47 namespace OpenHardwareMonitor.Hardware.Heatmaster {
48 internal class HeatmasterGroup : IGroup {
50 private readonly List<Heatmaster> hardware = new List<Heatmaster>();
51 private readonly StringBuilder report = new StringBuilder();
53 private static string ReadLine(SerialPort port, int timeout) {
55 StringBuilder builder = new StringBuilder();
57 while (port.BytesToRead > 0) {
58 byte b = (byte)port.ReadByte();
60 case 0xAA: return ((char)b).ToString();
61 case 0x0D: return builder.ToString();
62 default: builder.Append((char)b); break;
68 throw new TimeoutException();
71 private static string[] GetRegistryPortNames() {
72 List<string> result = new List<string>();
73 string[] paths = { "", "&MI_00" };
75 foreach (string path in paths) {
76 RegistryKey key = Registry.LocalMachine.OpenSubKey(
77 @"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60" + path);
79 foreach (string subKeyName in key.GetSubKeyNames()) {
81 key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
83 string name = subKey.GetValue("PortName") as string;
84 if (name != null && !result.Contains(name))
90 } catch (SecurityException) { }
91 return result.ToArray();
94 public HeatmasterGroup(ISettings settings) {
96 // No implementation for Heatmaster on Unix systems
97 int p = (int)Environment.OSVersion.Platform;
98 if ((p == 4) || (p == 128))
101 string[] portNames = GetRegistryPortNames();
102 for (int i = 0; i < portNames.Length; i++) {
103 bool isValid = false;
105 using (SerialPort serialPort =
106 new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One)) {
107 serialPort.NewLine = ((char)0x0D).ToString();
108 report.Append("Port Name: "); report.AppendLine(portNames[i]);
112 } catch (UnauthorizedAccessException) {
113 report.AppendLine("Exception: Access Denied");
116 if (serialPort.IsOpen) {
117 serialPort.DiscardInBuffer();
118 serialPort.DiscardOutBuffer();
119 serialPort.Write(new byte[] { 0xAA }, 0, 1);
122 while (serialPort.BytesToRead == 0 && j < 10) {
126 if (serialPort.BytesToRead > 0) {
128 while (serialPort.BytesToRead > 0 && !flag) {
129 flag |= (serialPort.ReadByte() == 0xAA);
132 serialPort.WriteLine("[0:0]RH");
137 string line = ReadLine(serialPort, 100);
138 if (line.StartsWith("-[0:0]RH:",
139 StringComparison.Ordinal)) {
140 revision = int.Parse(line.Substring(9),
141 CultureInfo.InvariantCulture);
146 isValid = (revision == 770);
148 report.Append("Status: Wrong Hardware Revision " +
149 revision.ToString(CultureInfo.InvariantCulture));
151 } catch (TimeoutException) {
152 report.AppendLine("Status: Timeout Reading Revision");
155 report.AppendLine("Status: Wrong Startflag");
158 report.AppendLine("Status: No Response");
160 serialPort.DiscardInBuffer();
162 report.AppendLine("Status: Port not Open");
165 } catch (Exception e) {
166 report.AppendLine(e.ToString());
170 report.AppendLine("Status: OK");
171 hardware.Add(new Heatmaster(portNames[i], settings));
177 public IHardware[] Hardware {
179 return hardware.ToArray();
183 public string GetReport() {
184 if (report.Length > 0) {
185 StringBuilder r = new StringBuilder();
186 r.AppendLine("Serial Port Heatmaster");
195 public void Close() {
196 foreach (Heatmaster heatmaster in hardware)