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