Hardware/Heatmaster/HeatmasterGroup.cs
author moel.mich
Tue, 24 Aug 2010 19:06:49 +0000
changeset 173 fb96c0ca3c2d
parent 172 c9d8de472546
child 175 e4ee19d583bd
permissions -rw-r--r--
Added another registry path for getting the Heatmaster serial port.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.IO.Ports;
    41 using System.Security;
    42 using System.Text;
    43 using System.Threading;
    44 using Microsoft.Win32;
    45 
    46 namespace OpenHardwareMonitor.Hardware.Heatmaster {
    47   internal class HeatmasterGroup : IGroup {
    48 
    49     private List<Heatmaster> hardware = new List<Heatmaster>();
    50     private StringBuilder report = new StringBuilder();
    51 
    52     private static string ReadLine(SerialPort port, int timeout) {
    53       int i = 0;
    54       StringBuilder builder = new StringBuilder();
    55       while (i < timeout) {
    56         while (port.BytesToRead > 0) {
    57           byte b = (byte)port.ReadByte();
    58           switch (b) {
    59             case 0xAA: return ((char)b).ToString();
    60             case 0x0D: return builder.ToString();
    61             default: builder.Append((char)b); break;
    62           }
    63         }
    64         i++;
    65         Thread.Sleep(1);
    66       }
    67       throw new TimeoutException();
    68     }
    69 
    70     private static string[] GetRegistryPortNames() {
    71       List<string> result = new List<string>();
    72       try {
    73         RegistryKey key = Registry.LocalMachine.OpenSubKey(
    74           @"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60");
    75         if (key == null)
    76           key = Registry.LocalMachine.OpenSubKey(
    77             @"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60&MI_00");           
    78         if (key != null) {
    79           foreach (string subKeyName in key.GetSubKeyNames()) {
    80             RegistryKey subKey =
    81               key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
    82             if (subKey != null) {
    83               string name = subKey.GetValue("PortName") as string;
    84               if (name != null)
    85                 result.Add((string)name);
    86             }
    87           }
    88         }
    89       } catch (SecurityException) { }
    90       return result.ToArray();
    91     }
    92 
    93     public HeatmasterGroup(ISettings settings) {
    94       
    95       // No implementation for Heatmaster on Unix systems
    96       int p = (int)System.Environment.OSVersion.Platform;
    97       if ((p == 4) || (p == 128))
    98         return;
    99 
   100       string[] portNames = GetRegistryPortNames();      
   101       for (int i = portNames.Length - 1; i >= 0; i--) {
   102         try {
   103 
   104           SerialPort serialPort =
   105             new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One);
   106           serialPort.NewLine = ((char)0x0D).ToString();
   107 
   108           bool isValid = false;
   109           report.Append("Port Name: "); report.AppendLine(portNames[i]);
   110 
   111           try {
   112             serialPort.Open();
   113           } catch (UnauthorizedAccessException) {
   114             report.AppendLine("Exception: Access Denied");
   115           }
   116 
   117           if (serialPort.IsOpen) {
   118             serialPort.DiscardInBuffer();
   119             serialPort.DiscardOutBuffer(); 
   120             serialPort.Write(new byte[] { 0xAA }, 0, 1);
   121 
   122             int j = 0;
   123             while (serialPort.BytesToRead == 0 && j < 10) {
   124               Thread.Sleep(20);
   125               j++;
   126             }
   127             if (serialPort.BytesToRead > 0) {
   128               bool flag = false;
   129               while (serialPort.BytesToRead > 0 && !flag) {
   130                 flag |= (serialPort.ReadByte() == 0xAA);
   131               }
   132               if (flag) {
   133                 serialPort.WriteLine("[0:0]RH");
   134                 try {
   135                   int k = 0;
   136                   int revision = 0;
   137                   while (k < 5) {
   138                     string line = ReadLine(serialPort, 100);
   139                     if (line.StartsWith("-[0:0]RH:")) {
   140                       int.TryParse(line.Substring(9), out revision);
   141                       break;
   142                     }
   143                     k++;
   144                   }
   145                   isValid = (revision == 770); 
   146                   if (!isValid) {
   147                     report.Append("Status: Wrong Hardware Revision " + 
   148                       revision.ToString());
   149                   }                 
   150                 } catch (TimeoutException) {
   151                   report.AppendLine("Status: Timeout Reading Revision");
   152                 }
   153               } else {
   154                 report.AppendLine("Status: Wrong Startflag");
   155               }
   156             } else {
   157               report.AppendLine("Status: No Response");
   158             }
   159             serialPort.DiscardInBuffer();
   160             serialPort.Close();
   161             serialPort.Dispose();
   162           } else {
   163             report.AppendLine("Status: Port not Open");
   164           }                          
   165           if (isValid) {
   166             report.AppendLine("Status: OK");
   167             hardware.Add(new Heatmaster(portNames[i], settings));
   168             return;
   169           }
   170         } catch (Exception e) {
   171           report.AppendLine(e.ToString());
   172         } 
   173         report.AppendLine();
   174       }
   175     }
   176 
   177     public IHardware[] Hardware {
   178       get {
   179         return hardware.ToArray();
   180       }
   181     }
   182 
   183     public string GetReport() {
   184       if (report.Length > 0) {
   185         report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine +
   186           Environment.NewLine);
   187         report.AppendLine();
   188         return report.ToString();
   189       } else
   190         return null;
   191     }
   192 
   193     public void Close() {
   194       foreach (Heatmaster heatmaster in hardware)
   195         heatmaster.Close();
   196     }
   197   }
   198 }