Hardware/TBalancer/TBalancerGroup.cs
author moel.mich
Tue, 16 Feb 2010 21:44:25 +0000
changeset 47 1d02fde1bb23
parent 45 eadad41d21a6
child 51 e5fc5fd57dbe
permissions -rw-r--r--
Restored CTSHolding check and added more report output for T-Balancer enumeration.
     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           
    60           bool isValid = false;
    61           byte protocolVersion = 0;
    62           report.Append("Port Name: "); report.AppendLine(portNames[i]);
    63 
    64           try {
    65             serialPort.Open();
    66           } catch (UnauthorizedAccessException) {
    67             report.AppendLine("Exception: Access Denied");
    68           }
    69        
    70           if (serialPort.IsOpen) {
    71             if (serialPort.CtsHolding) {
    72               serialPort.DiscardInBuffer();
    73               serialPort.DiscardOutBuffer();
    74               serialPort.Write(new byte[] { 0x38 }, 0, 1);
    75               int j = 0;
    76               while (serialPort.BytesToRead == 0 && j < 2) {
    77                 Thread.Sleep(100);
    78                 j++;
    79               }
    80               if (serialPort.BytesToRead > 0) {
    81                 if (serialPort.ReadByte() == TBalancer.STARTFLAG) {
    82                   while (serialPort.BytesToRead < 284 && j < 5) {
    83                     Thread.Sleep(100);
    84                     j++;
    85                   }
    86                   int length = serialPort.BytesToRead;
    87                   if (length >= 284) {
    88                     byte[] data = new byte[285];
    89                     data[0] = TBalancer.STARTFLAG;
    90                     for (int k = 1; k < data.Length; k++)
    91                       data[k] = (byte)serialPort.ReadByte();
    92 
    93                     // check protocol version 2X (protocols seen: 2C, 2A, 28)
    94                     isValid = (data[274] & 0xF0) == 0x20;
    95                     protocolVersion = data[274];
    96                     if (!isValid) {
    97                       report.Append("Status: Wrong Protocol Version: 0x");
    98                       report.AppendLine(protocolVersion.ToString("X"));
    99                     }
   100                   } else {
   101                     report.AppendLine("Status: Wrong Message Length: " +length);
   102                   }
   103                 } else {
   104                   report.AppendLine("Status: Wrong Startflag");
   105                 }
   106               } else {
   107                 report.AppendLine("Status: No Response");
   108               }
   109             } else {
   110               report.AppendLine("Status: Not Clear to Send");
   111             }
   112             serialPort.DiscardInBuffer();
   113             serialPort.Close();
   114           } else {            
   115             report.AppendLine("Status: Port not Open");
   116           }                          
   117           if (isValid) {
   118             report.AppendLine("Status: OK");
   119             hardware.Add(new TBalancer(portNames[i], protocolVersion));
   120             return;
   121           }
   122         } catch (IOException ioe) {
   123           report.AppendLine(ioe.ToString());
   124         } catch (NullReferenceException ne) {
   125           report.AppendLine(ne.ToString());
   126         }
   127         report.AppendLine();
   128       }
   129     }
   130 
   131     public IHardware[] Hardware {
   132       get {
   133         return hardware.ToArray();
   134       }
   135     }
   136 
   137     public string GetReport() {
   138       if (report.Length > 0) {
   139         report.Insert(0, "Serial Port T-Balancer" + Environment.NewLine +
   140           Environment.NewLine);
   141         report.AppendLine();
   142         return report.ToString();
   143       } else
   144         return null;
   145     }
   146 
   147     public void Close() {
   148       foreach (TBalancer tbalancer in hardware)
   149         tbalancer.Close();
   150     }
   151   }
   152 }