Hardware/TBalancer/TBalancerGroup.cs
changeset 1 361e324a0ed4
child 32 a7f5e400f0da
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/TBalancer/TBalancerGroup.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,112 @@
     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) 2009-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;
    1.44 +using System.IO.Ports;
    1.45 +using System.Threading;
    1.46 +
    1.47 +namespace OpenHardwareMonitor.Hardware.TBalancer {
    1.48 +  public class TBalancerGroup : IGroup {
    1.49 +
    1.50 +    private List<TBalancer> hardware = new List<TBalancer>();
    1.51 +
    1.52 +    public TBalancerGroup() {
    1.53 +
    1.54 +      string[] portNames = SerialPort.GetPortNames();
    1.55 +      for (int i = portNames.Length - 1; i >= 0; i--) {
    1.56 +        try {
    1.57 +          SerialPort serialPort =
    1.58 +            new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One);
    1.59 +          serialPort.Open();
    1.60 +          bool isValid = false;
    1.61 +          if (serialPort.IsOpen && serialPort.CDHolding &&
    1.62 +            serialPort.CtsHolding) {
    1.63 +            serialPort.DiscardInBuffer();
    1.64 +            serialPort.DiscardOutBuffer();
    1.65 +            serialPort.Write(new byte[] { 0x38 }, 0, 1);
    1.66 +            int j = 0;
    1.67 +            while (serialPort.BytesToRead == 0 && j < 2) {
    1.68 +              Thread.Sleep(100);
    1.69 +              j++;
    1.70 +            }
    1.71 +            if (serialPort.BytesToRead > 0 && 
    1.72 +              serialPort.ReadByte() == TBalancer.STARTFLAG) 
    1.73 +            {
    1.74 +              while (serialPort.BytesToRead < 284 && j < 5) {
    1.75 +                Thread.Sleep(100);
    1.76 +                j++;
    1.77 +              }
    1.78 +              if (serialPort.BytesToRead == 284) {
    1.79 +                int[] data = new int[285];
    1.80 +                data[0] = TBalancer.STARTFLAG;
    1.81 +                for (int k = 1; k < data.Length; k++)
    1.82 +                  data[k] = serialPort.ReadByte();
    1.83 +                
    1.84 +                // check protocol version
    1.85 +                isValid = (data[274] == TBalancer.PROTOCOL_VERSION); 
    1.86 +              }
    1.87 +            }
    1.88 +          }
    1.89 +          serialPort.DiscardInBuffer();
    1.90 +          serialPort.Close();
    1.91 +          if (isValid) {
    1.92 +            hardware.Add(new TBalancer(portNames[i]));
    1.93 +            return;
    1.94 +          }
    1.95 +        } catch (IOException) { } catch (UnauthorizedAccessException) { } 
    1.96 +          catch (NullReferenceException) { }
    1.97 +      }
    1.98 +    }
    1.99 +
   1.100 +    public IHardware[] Hardware {
   1.101 +      get {
   1.102 +        return hardware.ToArray();
   1.103 +      }
   1.104 +    }
   1.105 +
   1.106 +    public string GetReport() {
   1.107 +      return null;
   1.108 +    }
   1.109 +
   1.110 +    public void Close() {
   1.111 +      foreach (TBalancer tbalancer in hardware)
   1.112 +        tbalancer.Close();
   1.113 +    }
   1.114 +  }
   1.115 +}