Hardware/Heatmaster/HeatmasterGroup.cs
author moel.mich
Sun, 22 Aug 2010 21:53:11 +0000
changeset 171 81ab5e53122e
child 172 c9d8de472546
permissions -rw-r--r--
Added a first implementation for the Heatmaster fan controller.
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@171
    41
using System.Text;
moel@171
    42
using System.Threading;
moel@171
    43
moel@171
    44
namespace OpenHardwareMonitor.Hardware.Heatmaster {
moel@171
    45
  internal class HeatmasterGroup : IGroup {
moel@171
    46
moel@171
    47
    private List<Heatmaster> hardware = new List<Heatmaster>();
moel@171
    48
    private StringBuilder report = new StringBuilder();
moel@171
    49
moel@171
    50
    private static string ReadLine(SerialPort port, int timeout) {
moel@171
    51
      int i = 0;
moel@171
    52
      StringBuilder builder = new StringBuilder();
moel@171
    53
      while (i < timeout) {
moel@171
    54
        while (port.BytesToRead > 0) {
moel@171
    55
          byte b = (byte)port.ReadByte();
moel@171
    56
          switch (b) {
moel@171
    57
            case 0xAA: return ((char)b).ToString();
moel@171
    58
            case 0x0D: return builder.ToString();
moel@171
    59
            default: builder.Append((char)b); break;
moel@171
    60
          }
moel@171
    61
        }
moel@171
    62
        i++;
moel@171
    63
        Thread.Sleep(1);
moel@171
    64
      }
moel@171
    65
      throw new TimeoutException();
moel@171
    66
    }   
moel@171
    67
moel@171
    68
    public HeatmasterGroup(ISettings settings) {
moel@171
    69
moel@171
    70
      string[] portNames = SerialPort.GetPortNames();      
moel@171
    71
      for (int i = portNames.Length - 1; i >= 0; i--) {
moel@171
    72
        try {
moel@171
    73
moel@171
    74
          SerialPort serialPort =
moel@171
    75
            new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One);
moel@171
    76
          serialPort.NewLine = ((char)0x0D).ToString();
moel@171
    77
moel@171
    78
          bool isValid = false;
moel@171
    79
          report.Append("Port Name: "); report.AppendLine(portNames[i]);
moel@171
    80
moel@171
    81
          try {
moel@171
    82
            serialPort.Open();
moel@171
    83
          } catch (UnauthorizedAccessException) {
moel@171
    84
            report.AppendLine("Exception: Access Denied");
moel@171
    85
          }
moel@171
    86
moel@171
    87
          if (serialPort.IsOpen) {
moel@171
    88
            if (serialPort.CtsHolding) {
moel@171
    89
              serialPort.DiscardInBuffer();
moel@171
    90
              serialPort.DiscardOutBuffer(); 
moel@171
    91
              serialPort.Write(new byte[] { 0xAA }, 0, 1);
moel@171
    92
moel@171
    93
              int j = 0;
moel@171
    94
              while (serialPort.BytesToRead == 0 && j < 10) {
moel@171
    95
                Thread.Sleep(20);
moel@171
    96
                j++;
moel@171
    97
              }
moel@171
    98
              if (serialPort.BytesToRead > 0) {
moel@171
    99
                bool flag = false;
moel@171
   100
                while (serialPort.BytesToRead > 0 && !flag) {
moel@171
   101
                  flag |= (serialPort.ReadByte() == 0xAA);
moel@171
   102
                }
moel@171
   103
                if (flag) {
moel@171
   104
                  serialPort.WriteLine("[0:0]RH");
moel@171
   105
                  try {
moel@171
   106
                    int k = 0;
moel@171
   107
                    int revision = 0;
moel@171
   108
                    while (k < 5) {
moel@171
   109
                      string line = ReadLine(serialPort, 100);
moel@171
   110
                      if (line.StartsWith("-[0:0]RH:")) {
moel@171
   111
                        int.TryParse(line.Substring(9), out revision);
moel@171
   112
                        break;
moel@171
   113
                      }
moel@171
   114
                      k++;
moel@171
   115
                    }
moel@171
   116
                    isValid = (revision == 770); 
moel@171
   117
                    if (!isValid) {
moel@171
   118
                      report.Append("Status: Wrong Hardware Revision " + 
moel@171
   119
                        revision.ToString());
moel@171
   120
                    }                 
moel@171
   121
                  } catch (TimeoutException) {
moel@171
   122
                    report.AppendLine("Status: Timeout Reading Revision");
moel@171
   123
                  }
moel@171
   124
                } else {
moel@171
   125
                  report.AppendLine("Status: Wrong Startflag");
moel@171
   126
                }
moel@171
   127
              } else {
moel@171
   128
                report.AppendLine("Status: No Response");
moel@171
   129
              }
moel@171
   130
            } else {
moel@171
   131
              report.AppendLine("Status: Not Clear to Send");
moel@171
   132
            }
moel@171
   133
            serialPort.DiscardInBuffer();
moel@171
   134
            serialPort.Close();
moel@171
   135
            serialPort.Dispose();
moel@171
   136
          } else {
moel@171
   137
            report.AppendLine("Status: Port not Open");
moel@171
   138
          }                          
moel@171
   139
          if (isValid) {
moel@171
   140
            report.AppendLine("Status: OK");
moel@171
   141
            hardware.Add(new Heatmaster(portNames[i], settings));
moel@171
   142
            return;
moel@171
   143
          }
moel@171
   144
        } catch (Exception e) {
moel@171
   145
          report.AppendLine(e.ToString());
moel@171
   146
        } 
moel@171
   147
        report.AppendLine();
moel@171
   148
      }
moel@171
   149
    }
moel@171
   150
moel@171
   151
    public IHardware[] Hardware {
moel@171
   152
      get {
moel@171
   153
        return hardware.ToArray();
moel@171
   154
      }
moel@171
   155
    }
moel@171
   156
moel@171
   157
    public string GetReport() {
moel@171
   158
      if (report.Length > 0) {
moel@171
   159
        report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine +
moel@171
   160
          Environment.NewLine);
moel@171
   161
        report.AppendLine();
moel@171
   162
        return report.ToString();
moel@171
   163
      } else
moel@171
   164
        return null;
moel@171
   165
    }
moel@171
   166
moel@171
   167
    public void Close() {
moel@171
   168
      foreach (Heatmaster heatmaster in hardware)
moel@171
   169
        heatmaster.Close();
moel@171
   170
    }
moel@171
   171
  }
moel@171
   172
}