Hardware/TBalancer/TBalancerGroup.cs
author moel.mich
Mon, 08 Feb 2010 20:18:25 +0000
changeset 34 dc276daadb2c
parent 33 800a36a1ca8e
child 41 d92bacc2116e
permissions -rw-r--r--
Added support for Winbond W83627EHF, W83667HG, W83667HG-B mainboard chips.
     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) 2009-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;
    41 using System.IO.Ports;
    42 using System.Text;
    43 using System.Threading;
    44 
    45 namespace OpenHardwareMonitor.Hardware.TBalancer {
    46   public class TBalancerGroup : IGroup {
    47 
    48     private List<TBalancer> hardware = new List<TBalancer>();
    49     private StringBuilder report = new StringBuilder();
    50 
    51     public TBalancerGroup() {
    52    
    53       string[] portNames = SerialPort.GetPortNames();      
    54       for (int i = portNames.Length - 1; i >= 0; i--) {
    55         try {
    56 
    57           SerialPort serialPort =
    58             new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One);
    59           serialPort.Open();
    60           bool isValid = false;
    61           byte protocolVersion = 0;
    62           if (serialPort.IsOpen && serialPort.CDHolding &&
    63             serialPort.CtsHolding) {
    64 
    65             report.Append("Port Name: "); report.AppendLine(portNames[i]);            
    66 
    67             serialPort.DiscardInBuffer();
    68             serialPort.DiscardOutBuffer();
    69             serialPort.Write(new byte[] { 0x38 }, 0, 1);
    70             int j = 0;
    71             while (serialPort.BytesToRead == 0 && j < 2) {
    72               Thread.Sleep(100);
    73               j++;
    74             }
    75             if (serialPort.BytesToRead > 0 &&
    76               serialPort.ReadByte() == TBalancer.STARTFLAG) {
    77               while (serialPort.BytesToRead < 284 && j < 5) {
    78                 Thread.Sleep(100);
    79                 j++;
    80               }
    81               int length = serialPort.BytesToRead;
    82               if (length >= 284) {
    83                 byte[] data = new byte[285];
    84                 data[0] = TBalancer.STARTFLAG;
    85                 for (int k = 1; k < data.Length; k++)
    86                   data[k] = (byte)serialPort.ReadByte();
    87 
    88                 // check protocol version
    89                 isValid = 
    90                   data[274] == TBalancer.PROTOCOL_VERSION_2A ||
    91                   data[274] == TBalancer.PROTOCOL_VERSION_2C;
    92                 protocolVersion = data[274];
    93                 if (!isValid) {
    94                   report.Append("Status: Wrong Protocol Version: 0x");
    95                   report.AppendLine(protocolVersion.ToString("X"));
    96                 }
    97               } else {
    98                 report.AppendLine("Status: Wrong Message Length: " + length);
    99               }
   100             } else {
   101               report.AppendLine("Status: Wrong Startflag");
   102             }
   103           }
   104           serialPort.DiscardInBuffer();
   105           serialPort.Close();
   106           if (isValid) {
   107             report.AppendLine("Status: OK");
   108             hardware.Add(new TBalancer(portNames[i], protocolVersion));
   109             return;
   110           }
   111         } catch (IOException ioe) {
   112           report.AppendLine(ioe.ToString());
   113         } catch (UnauthorizedAccessException uae) {
   114           report.AppendLine(uae.ToString());
   115         } catch (NullReferenceException ne) {
   116           report.AppendLine(ne.ToString());
   117         }
   118       }
   119     }
   120 
   121     public IHardware[] Hardware {
   122       get {
   123         return hardware.ToArray();
   124       }
   125     }
   126 
   127     public string GetReport() {
   128       if (report.Length > 0) {
   129         report.Insert(0, "Serial Port T-Balancer" + Environment.NewLine +
   130           Environment.NewLine);
   131         report.AppendLine();
   132         return report.ToString();
   133       } else
   134         return null;
   135     }
   136 
   137     public void Close() {
   138       foreach (TBalancer tbalancer in hardware)
   139         tbalancer.Close();
   140     }
   141   }
   142 }