2010-08-22 21:53:11 +00:00
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
|
|
|
|
|
|
The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.mozilla.org/MPL/
|
|
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
for the specific language governing rights and limitations under the License.
|
|
|
|
|
|
|
|
|
|
The Original Code is the Open Hardware Monitor code.
|
|
|
|
|
|
|
|
|
|
The Initial Developer of the Original Code is
|
|
|
|
|
Michael Möller <m.moeller@gmx.ch>.
|
|
|
|
|
Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
|
the Initial Developer. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Contributor(s):
|
|
|
|
|
|
|
|
|
|
Alternatively, the contents of this file may be used under the terms of
|
|
|
|
|
either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
|
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
|
in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
|
of those above. If you wish to allow use of your version of this file only
|
|
|
|
|
under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
|
use your version of this file under the terms of the MPL, indicate your
|
|
|
|
|
decision by deleting the provisions above and replace them with the notice
|
|
|
|
|
and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
|
the provisions above, a recipient may use your version of this file under
|
|
|
|
|
the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2010-09-08 19:29:58 +00:00
|
|
|
|
using System.Globalization;
|
2010-08-22 21:53:11 +00:00
|
|
|
|
using System.IO.Ports;
|
2010-08-23 20:00:06 +00:00
|
|
|
|
using System.Security;
|
2010-08-22 21:53:11 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2010-08-23 20:00:06 +00:00
|
|
|
|
using Microsoft.Win32;
|
2010-08-22 21:53:11 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenHardwareMonitor.Hardware.Heatmaster {
|
|
|
|
|
internal class HeatmasterGroup : IGroup {
|
|
|
|
|
|
|
|
|
|
private List<Heatmaster> hardware = new List<Heatmaster>();
|
|
|
|
|
private StringBuilder report = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
private static string ReadLine(SerialPort port, int timeout) {
|
|
|
|
|
int i = 0;
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
while (i < timeout) {
|
|
|
|
|
while (port.BytesToRead > 0) {
|
|
|
|
|
byte b = (byte)port.ReadByte();
|
|
|
|
|
switch (b) {
|
|
|
|
|
case 0xAA: return ((char)b).ToString();
|
|
|
|
|
case 0x0D: return builder.ToString();
|
|
|
|
|
default: builder.Append((char)b); break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
}
|
|
|
|
|
throw new TimeoutException();
|
2010-08-23 20:00:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string[] GetRegistryPortNames() {
|
|
|
|
|
List<string> result = new List<string>();
|
2010-08-24 22:11:10 +00:00
|
|
|
|
string[] paths = { "", "&MI_00" };
|
2010-08-23 20:00:06 +00:00
|
|
|
|
try {
|
2010-08-24 22:11:10 +00:00
|
|
|
|
foreach (string path in paths) {
|
|
|
|
|
RegistryKey key = Registry.LocalMachine.OpenSubKey(
|
|
|
|
|
@"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60" + path);
|
|
|
|
|
if (key != null) {
|
|
|
|
|
foreach (string subKeyName in key.GetSubKeyNames()) {
|
|
|
|
|
RegistryKey subKey =
|
|
|
|
|
key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
|
|
|
|
|
if (subKey != null) {
|
|
|
|
|
string name = subKey.GetValue("PortName") as string;
|
|
|
|
|
if (name != null && !result.Contains(name))
|
|
|
|
|
result.Add((string)name);
|
|
|
|
|
}
|
2010-08-23 20:00:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (SecurityException) { }
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
}
|
2010-08-22 21:53:11 +00:00
|
|
|
|
|
|
|
|
|
public HeatmasterGroup(ISettings settings) {
|
2010-08-23 20:00:06 +00:00
|
|
|
|
|
|
|
|
|
// No implementation for Heatmaster on Unix systems
|
|
|
|
|
int p = (int)System.Environment.OSVersion.Platform;
|
|
|
|
|
if ((p == 4) || (p == 128))
|
|
|
|
|
return;
|
2010-08-22 21:53:11 +00:00
|
|
|
|
|
2010-08-23 20:00:06 +00:00
|
|
|
|
string[] portNames = GetRegistryPortNames();
|
2010-08-22 21:53:11 +00:00
|
|
|
|
for (int i = portNames.Length - 1; i >= 0; i--) {
|
2010-09-08 19:29:58 +00:00
|
|
|
|
bool isValid = false;
|
|
|
|
|
try {
|
|
|
|
|
using (SerialPort serialPort =
|
|
|
|
|
new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One)) {
|
|
|
|
|
serialPort.NewLine = ((char)0x0D).ToString();
|
|
|
|
|
report.Append("Port Name: "); report.AppendLine(portNames[i]);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
serialPort.Open();
|
|
|
|
|
} catch (UnauthorizedAccessException) {
|
|
|
|
|
report.AppendLine("Exception: Access Denied");
|
|
|
|
|
}
|
2010-08-22 21:53:11 +00:00
|
|
|
|
|
2010-09-08 19:29:58 +00:00
|
|
|
|
if (serialPort.IsOpen) {
|
|
|
|
|
serialPort.DiscardInBuffer();
|
|
|
|
|
serialPort.DiscardOutBuffer();
|
|
|
|
|
serialPort.Write(new byte[] { 0xAA }, 0, 1);
|
2010-08-23 20:00:06 +00:00
|
|
|
|
|
2010-09-08 19:29:58 +00:00
|
|
|
|
int j = 0;
|
|
|
|
|
while (serialPort.BytesToRead == 0 && j < 10) {
|
|
|
|
|
Thread.Sleep(20);
|
|
|
|
|
j++;
|
2010-08-22 21:53:11 +00:00
|
|
|
|
}
|
2010-09-08 19:29:58 +00:00
|
|
|
|
if (serialPort.BytesToRead > 0) {
|
|
|
|
|
bool flag = false;
|
|
|
|
|
while (serialPort.BytesToRead > 0 && !flag) {
|
|
|
|
|
flag |= (serialPort.ReadByte() == 0xAA);
|
|
|
|
|
}
|
|
|
|
|
if (flag) {
|
|
|
|
|
serialPort.WriteLine("[0:0]RH");
|
|
|
|
|
try {
|
|
|
|
|
int k = 0;
|
|
|
|
|
int revision = 0;
|
|
|
|
|
while (k < 5) {
|
|
|
|
|
string line = ReadLine(serialPort, 100);
|
|
|
|
|
if (line.StartsWith("-[0:0]RH:",
|
|
|
|
|
StringComparison.Ordinal)) {
|
|
|
|
|
revision = int.Parse(line.Substring(9),
|
|
|
|
|
CultureInfo.InvariantCulture);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
k++;
|
|
|
|
|
}
|
|
|
|
|
isValid = (revision == 770);
|
|
|
|
|
if (!isValid) {
|
|
|
|
|
report.Append("Status: Wrong Hardware Revision " +
|
|
|
|
|
revision.ToString(CultureInfo.InvariantCulture));
|
2010-08-22 21:53:11 +00:00
|
|
|
|
}
|
2010-09-08 19:29:58 +00:00
|
|
|
|
} catch (TimeoutException) {
|
|
|
|
|
report.AppendLine("Status: Timeout Reading Revision");
|
2010-08-22 21:53:11 +00:00
|
|
|
|
}
|
2010-09-08 19:29:58 +00:00
|
|
|
|
} else {
|
|
|
|
|
report.AppendLine("Status: Wrong Startflag");
|
2010-08-22 21:53:11 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2010-09-08 19:29:58 +00:00
|
|
|
|
report.AppendLine("Status: No Response");
|
2010-08-22 21:53:11 +00:00
|
|
|
|
}
|
2010-09-08 19:29:58 +00:00
|
|
|
|
serialPort.DiscardInBuffer();
|
2010-08-22 21:53:11 +00:00
|
|
|
|
} else {
|
2010-09-08 19:29:58 +00:00
|
|
|
|
report.AppendLine("Status: Port not Open");
|
|
|
|
|
}
|
2010-08-22 21:53:11 +00:00
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
report.AppendLine(e.ToString());
|
2010-09-08 19:29:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isValid) {
|
|
|
|
|
report.AppendLine("Status: OK");
|
|
|
|
|
hardware.Add(new Heatmaster(portNames[i], settings));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-08-22 21:53:11 +00:00
|
|
|
|
report.AppendLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IHardware[] Hardware {
|
|
|
|
|
get {
|
|
|
|
|
return hardware.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetReport() {
|
|
|
|
|
if (report.Length > 0) {
|
|
|
|
|
report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine +
|
|
|
|
|
Environment.NewLine);
|
|
|
|
|
report.AppendLine();
|
|
|
|
|
return report.ToString();
|
|
|
|
|
} else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Close() {
|
|
|
|
|
foreach (Heatmaster heatmaster in hardware)
|
|
|
|
|
heatmaster.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|