Hardware/TBalancer/TBalancerGroup.cs
author moel.mich
Sun, 31 Jan 2010 19:30:00 +0000
changeset 13 d32fc5f2e822
child 32 a7f5e400f0da
permissions -rw-r--r--
Added support for W83627DHG chips. Changed Core i7 temperature reading. Fixed IT87 temperature reading.
     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.Threading;
    43 
    44 namespace OpenHardwareMonitor.Hardware.TBalancer {
    45   public class TBalancerGroup : IGroup {
    46 
    47     private List<TBalancer> hardware = new List<TBalancer>();
    48 
    49     public TBalancerGroup() {
    50 
    51       string[] portNames = SerialPort.GetPortNames();
    52       for (int i = portNames.Length - 1; i >= 0; i--) {
    53         try {
    54           SerialPort serialPort =
    55             new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One);
    56           serialPort.Open();
    57           bool isValid = false;
    58           if (serialPort.IsOpen && serialPort.CDHolding &&
    59             serialPort.CtsHolding) {
    60             serialPort.DiscardInBuffer();
    61             serialPort.DiscardOutBuffer();
    62             serialPort.Write(new byte[] { 0x38 }, 0, 1);
    63             int j = 0;
    64             while (serialPort.BytesToRead == 0 && j < 2) {
    65               Thread.Sleep(100);
    66               j++;
    67             }
    68             if (serialPort.BytesToRead > 0 && 
    69               serialPort.ReadByte() == TBalancer.STARTFLAG) 
    70             {
    71               while (serialPort.BytesToRead < 284 && j < 5) {
    72                 Thread.Sleep(100);
    73                 j++;
    74               }
    75               if (serialPort.BytesToRead == 284) {
    76                 int[] data = new int[285];
    77                 data[0] = TBalancer.STARTFLAG;
    78                 for (int k = 1; k < data.Length; k++)
    79                   data[k] = serialPort.ReadByte();
    80                 
    81                 // check protocol version
    82                 isValid = (data[274] == TBalancer.PROTOCOL_VERSION); 
    83               }
    84             }
    85           }
    86           serialPort.DiscardInBuffer();
    87           serialPort.Close();
    88           if (isValid) {
    89             hardware.Add(new TBalancer(portNames[i]));
    90             return;
    91           }
    92         } catch (IOException) { } catch (UnauthorizedAccessException) { } 
    93           catch (NullReferenceException) { }
    94       }
    95     }
    96 
    97     public IHardware[] Hardware {
    98       get {
    99         return hardware.ToArray();
   100       }
   101     }
   102 
   103     public string GetReport() {
   104       return null;
   105     }
   106 
   107     public void Close() {
   108       foreach (TBalancer tbalancer in hardware)
   109         tbalancer.Close();
   110     }
   111   }
   112 }