Hardware/Heatmaster/HeatmasterGroup.cs
changeset 171 81ab5e53122e
child 172 c9d8de472546
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/Heatmaster/HeatmasterGroup.cs	Sun Aug 22 21:53:11 2010 +0000
     1.3 @@ -0,0 +1,172 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.IO.Ports;
    1.44 +using System.Text;
    1.45 +using System.Threading;
    1.46 +
    1.47 +namespace OpenHardwareMonitor.Hardware.Heatmaster {
    1.48 +  internal class HeatmasterGroup : IGroup {
    1.49 +
    1.50 +    private List<Heatmaster> hardware = new List<Heatmaster>();
    1.51 +    private StringBuilder report = new StringBuilder();
    1.52 +
    1.53 +    private static string ReadLine(SerialPort port, int timeout) {
    1.54 +      int i = 0;
    1.55 +      StringBuilder builder = new StringBuilder();
    1.56 +      while (i < timeout) {
    1.57 +        while (port.BytesToRead > 0) {
    1.58 +          byte b = (byte)port.ReadByte();
    1.59 +          switch (b) {
    1.60 +            case 0xAA: return ((char)b).ToString();
    1.61 +            case 0x0D: return builder.ToString();
    1.62 +            default: builder.Append((char)b); break;
    1.63 +          }
    1.64 +        }
    1.65 +        i++;
    1.66 +        Thread.Sleep(1);
    1.67 +      }
    1.68 +      throw new TimeoutException();
    1.69 +    }   
    1.70 +
    1.71 +    public HeatmasterGroup(ISettings settings) {
    1.72 +
    1.73 +      string[] portNames = SerialPort.GetPortNames();      
    1.74 +      for (int i = portNames.Length - 1; i >= 0; i--) {
    1.75 +        try {
    1.76 +
    1.77 +          SerialPort serialPort =
    1.78 +            new SerialPort(portNames[i], 38400, Parity.None, 8, StopBits.One);
    1.79 +          serialPort.NewLine = ((char)0x0D).ToString();
    1.80 +
    1.81 +          bool isValid = false;
    1.82 +          report.Append("Port Name: "); report.AppendLine(portNames[i]);
    1.83 +
    1.84 +          try {
    1.85 +            serialPort.Open();
    1.86 +          } catch (UnauthorizedAccessException) {
    1.87 +            report.AppendLine("Exception: Access Denied");
    1.88 +          }
    1.89 +
    1.90 +          if (serialPort.IsOpen) {
    1.91 +            if (serialPort.CtsHolding) {
    1.92 +              serialPort.DiscardInBuffer();
    1.93 +              serialPort.DiscardOutBuffer(); 
    1.94 +              serialPort.Write(new byte[] { 0xAA }, 0, 1);
    1.95 +
    1.96 +              int j = 0;
    1.97 +              while (serialPort.BytesToRead == 0 && j < 10) {
    1.98 +                Thread.Sleep(20);
    1.99 +                j++;
   1.100 +              }
   1.101 +              if (serialPort.BytesToRead > 0) {
   1.102 +                bool flag = false;
   1.103 +                while (serialPort.BytesToRead > 0 && !flag) {
   1.104 +                  flag |= (serialPort.ReadByte() == 0xAA);
   1.105 +                }
   1.106 +                if (flag) {
   1.107 +                  serialPort.WriteLine("[0:0]RH");
   1.108 +                  try {
   1.109 +                    int k = 0;
   1.110 +                    int revision = 0;
   1.111 +                    while (k < 5) {
   1.112 +                      string line = ReadLine(serialPort, 100);
   1.113 +                      if (line.StartsWith("-[0:0]RH:")) {
   1.114 +                        int.TryParse(line.Substring(9), out revision);
   1.115 +                        break;
   1.116 +                      }
   1.117 +                      k++;
   1.118 +                    }
   1.119 +                    isValid = (revision == 770); 
   1.120 +                    if (!isValid) {
   1.121 +                      report.Append("Status: Wrong Hardware Revision " + 
   1.122 +                        revision.ToString());
   1.123 +                    }                 
   1.124 +                  } catch (TimeoutException) {
   1.125 +                    report.AppendLine("Status: Timeout Reading Revision");
   1.126 +                  }
   1.127 +                } else {
   1.128 +                  report.AppendLine("Status: Wrong Startflag");
   1.129 +                }
   1.130 +              } else {
   1.131 +                report.AppendLine("Status: No Response");
   1.132 +              }
   1.133 +            } else {
   1.134 +              report.AppendLine("Status: Not Clear to Send");
   1.135 +            }
   1.136 +            serialPort.DiscardInBuffer();
   1.137 +            serialPort.Close();
   1.138 +            serialPort.Dispose();
   1.139 +          } else {
   1.140 +            report.AppendLine("Status: Port not Open");
   1.141 +          }                          
   1.142 +          if (isValid) {
   1.143 +            report.AppendLine("Status: OK");
   1.144 +            hardware.Add(new Heatmaster(portNames[i], settings));
   1.145 +            return;
   1.146 +          }
   1.147 +        } catch (Exception e) {
   1.148 +          report.AppendLine(e.ToString());
   1.149 +        } 
   1.150 +        report.AppendLine();
   1.151 +      }
   1.152 +    }
   1.153 +
   1.154 +    public IHardware[] Hardware {
   1.155 +      get {
   1.156 +        return hardware.ToArray();
   1.157 +      }
   1.158 +    }
   1.159 +
   1.160 +    public string GetReport() {
   1.161 +      if (report.Length > 0) {
   1.162 +        report.Insert(0, "Serial Port Heatmaster" + Environment.NewLine +
   1.163 +          Environment.NewLine);
   1.164 +        report.AppendLine();
   1.165 +        return report.ToString();
   1.166 +      } else
   1.167 +        return null;
   1.168 +    }
   1.169 +
   1.170 +    public void Close() {
   1.171 +      foreach (Heatmaster heatmaster in hardware)
   1.172 +        heatmaster.Close();
   1.173 +    }
   1.174 +  }
   1.175 +}