Hardware/Heatmaster/HeatmasterGroup.cs
author moel.mich
Tue, 07 Sep 2010 18:51:42 +0000
changeset 180 d40f49d45614
parent 173 fb96c0ca3c2d
child 182 4801e9eaf979
permissions -rw-r--r--
Fixed the GadgetWindow AlwaysOnTop option (could be lost by tracking "Show Desktop" events even when AlwaysOnTop = true).
moel@171
     1
/*
moel@171
     2
  
moel@171
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@171
     4
moel@171
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@171
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@171
     7
  the License. You may obtain a copy of the License at
moel@171
     8
 
moel@171
     9
  http://www.mozilla.org/MPL/
moel@171
    10
moel@171
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@171
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@171
    13
  for the specific language governing rights and limitations under the License.
moel@171
    14
moel@171
    15
  The Original Code is the Open Hardware Monitor code.
moel@171
    16
moel@171
    17
  The Initial Developer of the Original Code is 
moel@171
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@171
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@171
    20
  the Initial Developer. All Rights Reserved.
moel@171
    21
moel@171
    22
  Contributor(s):
moel@171
    23
moel@171
    24
  Alternatively, the contents of this file may be used under the terms of
moel@171
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@171
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@171
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@171
    28
  of those above. If you wish to allow use of your version of this file only
moel@171
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@171
    30
  use your version of this file under the terms of the MPL, indicate your
moel@171
    31
  decision by deleting the provisions above and replace them with the notice
moel@171
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@171
    33
  the provisions above, a recipient may use your version of this file under
moel@171
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@171
    35
 
moel@171
    36
*/
moel@171
    37
moel@171
    38
using System;
moel@171
    39
using System.Collections.Generic;
moel@171
    40
using System.IO.Ports;
moel@172
    41
using System.Security;
moel@171
    42
using System.Text;
moel@171
    43
using System.Threading;
moel@172
    44
using Microsoft.Win32;
moel@171
    45
moel@171
    46
namespace OpenHardwareMonitor.Hardware.Heatmaster {
moel@171
    47
  internal class HeatmasterGroup : IGroup {
moel@171
    48
moel@171
    49
    private List<Heatmaster> hardware = new List<Heatmaster>();
moel@171
    50
    private StringBuilder report = new StringBuilder();
moel@171
    51
moel@171
    52
    private static string ReadLine(SerialPort port, int timeout) {
moel@171
    53
      int i = 0;
moel@171
    54
      StringBuilder builder = new StringBuilder();
moel@171
    55
      while (i < timeout) {
moel@171
    56
        while (port.BytesToRead > 0) {
moel@171
    57
          byte b = (byte)port.ReadByte();
moel@171
    58
          switch (b) {
moel@171
    59
            case 0xAA: return ((char)b).ToString();
moel@171
    60
            case 0x0D: return builder.ToString();
moel@171
    61
            default: builder.Append((char)b); break;
moel@171
    62
          }
moel@171
    63
        }
moel@171
    64
        i++;
moel@171
    65
        Thread.Sleep(1);
moel@171
    66
      }
moel@171
    67
      throw new TimeoutException();
moel@172
    68
    }
moel@172
    69
moel@172
    70
    private static string[] GetRegistryPortNames() {
moel@172
    71
      List<string> result = new List<string>();
moel@175
    72
      string[] paths = { "", "&MI_00" };
moel@172
    73
      try {
moel@175
    74
        foreach (string path in paths) {
moel@175
    75
          RegistryKey key = Registry.LocalMachine.OpenSubKey(
moel@175
    76
            @"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60" + path);
moel@175
    77
          if (key != null) {
moel@175
    78
            foreach (string subKeyName in key.GetSubKeyNames()) {
moel@175
    79
              RegistryKey subKey =
moel@175
    80
                key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
moel@175
    81
              if (subKey != null) {
moel@175
    82
                string name = subKey.GetValue("PortName") as string;
moel@175
    83
                if (name != null && !result.Contains(name))
moel@175
    84
                  result.Add((string)name);
moel@175
    85
              }
moel@172
    86
            }
moel@172
    87
          }
moel@172
    88
        }
moel@172
    89
      } catch (SecurityException) { }
moel@172
    90
      return result.ToArray();
moel@172
    91
    }
moel@171
    92
moel@171
    93
    public HeatmasterGroup(ISettings settings) {
moel@172
    94
      
moel@172
    95
      // No implementation for Heatmaster on Unix systems
moel@172
    96
      int p = (int)System.Environment.OSVersion.Platform;
moel@172
    97
      if ((p == 4) || (p == 128))
moel@172
    98
        return;
moel@171
    99
moel@172
   100
      string[] portNames = GetRegistryPortNames();      
moel@171
   101
      for (int i = portNames.Length - 1; i >= 0; i--) {
moel@171
   102
        try {
moel@171
   103
moel@171
   104
          SerialPort serialPort =
moel@171
   105
            new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One);
moel@171
   106
          serialPort.NewLine = ((char)0x0D).ToString();
moel@171
   107
moel@171
   108
          bool isValid = false;
moel@171
   109
          report.Append("Port Name: "); report.AppendLine(portNames[i]);
moel@171
   110
moel@171
   111
          try {
moel@171
   112
            serialPort.Open();
moel@171
   113
          } catch (UnauthorizedAccessException) {
moel@171
   114
            report.AppendLine("Exception: Access Denied");
moel@171
   115
          }
moel@171
   116
moel@171
   117
          if (serialPort.IsOpen) {
moel@172
   118
            serialPort.DiscardInBuffer();
moel@172
   119
            serialPort.DiscardOutBuffer(); 
moel@172
   120
            serialPort.Write(new byte[] { 0xAA }, 0, 1);
moel@171
   121
moel@172
   122
            int j = 0;
moel@172
   123
            while (serialPort.BytesToRead == 0 && j < 10) {
moel@172
   124
              Thread.Sleep(20);
moel@172
   125
              j++;
moel@172
   126
            }
moel@172
   127
            if (serialPort.BytesToRead > 0) {
moel@172
   128
              bool flag = false;
moel@172
   129
              while (serialPort.BytesToRead > 0 && !flag) {
moel@172
   130
                flag |= (serialPort.ReadByte() == 0xAA);
moel@171
   131
              }
moel@172
   132
              if (flag) {
moel@172
   133
                serialPort.WriteLine("[0:0]RH");
moel@172
   134
                try {
moel@172
   135
                  int k = 0;
moel@172
   136
                  int revision = 0;
moel@172
   137
                  while (k < 5) {
moel@172
   138
                    string line = ReadLine(serialPort, 100);
moel@172
   139
                    if (line.StartsWith("-[0:0]RH:")) {
moel@172
   140
                      int.TryParse(line.Substring(9), out revision);
moel@172
   141
                      break;
moel@171
   142
                    }
moel@172
   143
                    k++;
moel@171
   144
                  }
moel@172
   145
                  isValid = (revision == 770); 
moel@172
   146
                  if (!isValid) {
moel@172
   147
                    report.Append("Status: Wrong Hardware Revision " + 
moel@172
   148
                      revision.ToString());
moel@172
   149
                  }                 
moel@172
   150
                } catch (TimeoutException) {
moel@172
   151
                  report.AppendLine("Status: Timeout Reading Revision");
moel@171
   152
                }
moel@171
   153
              } else {
moel@172
   154
                report.AppendLine("Status: Wrong Startflag");
moel@171
   155
              }
moel@171
   156
            } else {
moel@172
   157
              report.AppendLine("Status: No Response");
moel@171
   158
            }
moel@171
   159
            serialPort.DiscardInBuffer();
moel@171
   160
            serialPort.Close();
moel@171
   161
            serialPort.Dispose();
moel@171
   162
          } else {
moel@171
   163
            report.AppendLine("Status: Port not Open");
moel@171
   164
          }                          
moel@171
   165
          if (isValid) {
moel@171
   166
            report.AppendLine("Status: OK");
moel@171
   167
            hardware.Add(new Heatmaster(portNames[i], settings));
moel@171
   168
            return;
moel@171
   169
          }
moel@171
   170
        } catch (Exception e) {
moel@171
   171
          report.AppendLine(e.ToString());
moel@171
   172
        } 
moel@171
   173
        report.AppendLine();
moel@171
   174
      }
moel@171
   175
    }
moel@171
   176
moel@171
   177
    public IHardware[] Hardware {
moel@171
   178
      get {
moel@171
   179
        return hardware.ToArray();
moel@171
   180
      }
moel@171
   181
    }
moel@171
   182
moel@171
   183
    public string GetReport() {
moel@171
   184
      if (report.Length > 0) {
moel@171
   185
        report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine +
moel@171
   186
          Environment.NewLine);
moel@171
   187
        report.AppendLine();
moel@171
   188
        return report.ToString();
moel@171
   189
      } else
moel@171
   190
        return null;
moel@171
   191
    }
moel@171
   192
moel@171
   193
    public void Close() {
moel@171
   194
      foreach (Heatmaster heatmaster in hardware)
moel@171
   195
        heatmaster.Close();
moel@171
   196
    }
moel@171
   197
  }
moel@171
   198
}