Hardware/TBalancer/TBalancerGroup.cs
author moel.mich
Fri, 12 Feb 2010 22:46:31 +0000
changeset 42 47385d4fc990
parent 34 dc276daadb2c
child 45 eadad41d21a6
permissions -rw-r--r--
Tray sensor display default color is black and color can be changed now. Fixed CPU load reading for AMD CPUs and added additional misc device for AMD core 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.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           report.Append("Port Name: "); report.AppendLine(portNames[i]);
    63 
    64           if (serialPort.IsOpen) {
    65             serialPort.DiscardInBuffer();
    66             serialPort.DiscardOutBuffer();
    67             serialPort.Write(new byte[] { 0x38 }, 0, 1);
    68             int j = 0;
    69             while (serialPort.BytesToRead == 0 && j < 2) {
    70               Thread.Sleep(100);
    71               j++;
    72             }
    73             if (serialPort.BytesToRead > 0) {
    74               if (serialPort.ReadByte() == TBalancer.STARTFLAG) {
    75                 while (serialPort.BytesToRead < 284 && j < 5) {
    76                   Thread.Sleep(100);
    77                   j++;
    78                 }
    79                 int length = serialPort.BytesToRead;
    80                 if (length >= 284) {
    81                   byte[] data = new byte[285];
    82                   data[0] = TBalancer.STARTFLAG;
    83                   for (int k = 1; k < data.Length; k++)
    84                     data[k] = (byte)serialPort.ReadByte();
    85 
    86                   // check protocol version
    87                   isValid =
    88                     data[274] == TBalancer.PROTOCOL_VERSION_2A ||
    89                     data[274] == TBalancer.PROTOCOL_VERSION_2C;
    90                   protocolVersion = data[274];
    91                   if (!isValid) {
    92                     report.Append("Status: Wrong Protocol Version: 0x");
    93                     report.AppendLine(protocolVersion.ToString("X"));
    94                   }
    95                 } else {
    96                   report.AppendLine("Status: Wrong Message Length: " + length);
    97                 }
    98               } else {
    99                 report.AppendLine("Status: Wrong Startflag");
   100               }
   101             } else {
   102               report.AppendLine("Status: No Response");
   103             }
   104           } else {
   105             report.AppendLine("Status: Port not Open");
   106           }
   107           serialPort.DiscardInBuffer();
   108           serialPort.Close();
   109           if (isValid) {
   110             report.AppendLine("Status: OK");
   111             hardware.Add(new TBalancer(portNames[i], protocolVersion));
   112             return;
   113           }
   114         } catch (IOException ioe) {
   115           report.AppendLine(ioe.ToString());
   116         } catch (UnauthorizedAccessException uae) {
   117           report.AppendLine(uae.ToString());
   118         } catch (NullReferenceException ne) {
   119           report.AppendLine(ne.ToString());
   120         }
   121         report.AppendLine();
   122       }
   123     }
   124 
   125     public IHardware[] Hardware {
   126       get {
   127         return hardware.ToArray();
   128       }
   129     }
   130 
   131     public string GetReport() {
   132       if (report.Length > 0) {
   133         report.Insert(0, "Serial Port T-Balancer" + Environment.NewLine +
   134           Environment.NewLine);        
   135         return report.ToString();
   136       } else
   137         return null;
   138     }
   139 
   140     public void Close() {
   141       foreach (TBalancer tbalancer in hardware)
   142         tbalancer.Close();
   143     }
   144   }
   145 }