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