Fixed Issue 368. Fixed Issue 369. Fixed Issue 370.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2010 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
13 using System.Globalization;
14 using System.IO.Ports;
15 using System.Security;
17 using System.Threading;
18 using Microsoft.Win32;
20 namespace OpenHardwareMonitor.Hardware.Heatmaster {
21 internal class HeatmasterGroup : IGroup {
23 private readonly List<Heatmaster> hardware = new List<Heatmaster>();
24 private readonly StringBuilder report = new StringBuilder();
26 private static string ReadLine(SerialPort port, int timeout) {
28 StringBuilder builder = new StringBuilder();
30 while (port.BytesToRead > 0) {
31 byte b = (byte)port.ReadByte();
33 case 0xAA: return ((char)b).ToString();
34 case 0x0D: return builder.ToString();
35 default: builder.Append((char)b); break;
41 throw new TimeoutException();
44 private static string[] GetRegistryPortNames() {
45 List<string> result = new List<string>();
46 string[] paths = { "", "&MI_00" };
48 foreach (string path in paths) {
49 RegistryKey key = Registry.LocalMachine.OpenSubKey(
50 @"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60" + path);
52 foreach (string subKeyName in key.GetSubKeyNames()) {
54 key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
56 string name = subKey.GetValue("PortName") as string;
57 if (name != null && !result.Contains(name))
63 } catch (SecurityException) { }
64 return result.ToArray();
67 public HeatmasterGroup(ISettings settings) {
69 // No implementation for Heatmaster on Unix systems
70 int p = (int)Environment.OSVersion.Platform;
71 if ((p == 4) || (p == 128))
74 string[] portNames = GetRegistryPortNames();
75 for (int i = 0; i < portNames.Length; i++) {
78 using (SerialPort serialPort =
79 new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One)) {
80 serialPort.NewLine = ((char)0x0D).ToString();
81 report.Append("Port Name: "); report.AppendLine(portNames[i]);
85 } catch (UnauthorizedAccessException) {
86 report.AppendLine("Exception: Access Denied");
89 if (serialPort.IsOpen) {
90 serialPort.DiscardInBuffer();
91 serialPort.DiscardOutBuffer();
92 serialPort.Write(new byte[] { 0xAA }, 0, 1);
95 while (serialPort.BytesToRead == 0 && j < 10) {
99 if (serialPort.BytesToRead > 0) {
101 while (serialPort.BytesToRead > 0 && !flag) {
102 flag |= (serialPort.ReadByte() == 0xAA);
105 serialPort.WriteLine("[0:0]RH");
110 string line = ReadLine(serialPort, 100);
111 if (line.StartsWith("-[0:0]RH:",
112 StringComparison.Ordinal)) {
113 revision = int.Parse(line.Substring(9),
114 CultureInfo.InvariantCulture);
119 isValid = (revision == 770);
121 report.Append("Status: Wrong Hardware Revision " +
122 revision.ToString(CultureInfo.InvariantCulture));
124 } catch (TimeoutException) {
125 report.AppendLine("Status: Timeout Reading Revision");
128 report.AppendLine("Status: Wrong Startflag");
131 report.AppendLine("Status: No Response");
133 serialPort.DiscardInBuffer();
135 report.AppendLine("Status: Port not Open");
138 } catch (Exception e) {
139 report.AppendLine(e.ToString());
143 report.AppendLine("Status: OK");
144 hardware.Add(new Heatmaster(portNames[i], settings));
150 public IHardware[] Hardware {
152 return hardware.ToArray();
156 public string GetReport() {
157 if (report.Length > 0) {
158 StringBuilder r = new StringBuilder();
159 r.AppendLine("Serial Port Heatmaster");
168 public void Close() {
169 foreach (Heatmaster heatmaster in hardware)