Hardware/TBalancer/TBalancerGroup.cs
author moel.mich
Sun, 15 Aug 2010 14:46:58 +0000
changeset 167 b7cc9d09aefe
parent 165 813d8bc3192f
child 182 4801e9eaf979
permissions -rw-r--r--
Fixed some Code Analysis warnings.
     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.Globalization;
    41 using System.IO;
    42 using System.IO.Ports;
    43 using System.Text;
    44 using System.Threading;
    45 
    46 namespace OpenHardwareMonitor.Hardware.TBalancer {
    47   internal class TBalancerGroup : IGroup {
    48 
    49     private List<TBalancer> hardware = new List<TBalancer>();
    50     private StringBuilder report = new StringBuilder();
    51 
    52     public TBalancerGroup(ISettings settings) {
    53 
    54       uint numDevices;
    55       try {
    56         FTD2XX.FT_CreateDeviceInfoList(out numDevices);
    57       } catch (DllNotFoundException) { return; } 
    58         catch (ArgumentNullException) { return; }
    59         catch (EntryPointNotFoundException) { return; }
    60         catch (BadImageFormatException) { return; }
    61      
    62       FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
    63       FTD2XX.FT_GetDeviceInfoList(info, ref numDevices);
    64 
    65       for (int i = 0; i < numDevices; i++) {
    66         report.Append("Device Index: ");
    67         report.AppendLine(i.ToString(CultureInfo.InvariantCulture));
    68         
    69         FT_HANDLE handle;
    70         FT_STATUS status;
    71         status = FTD2XX.FT_Open(i, out handle);
    72         if (status != FT_STATUS.FT_OK) {
    73           report.AppendLine("Open Status: " + status);
    74           continue;
    75         }
    76 
    77         FTD2XX.FT_SetBaudRate(handle, 19200);
    78         FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
    79         FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 
    80           0x13);
    81         FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
    82         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
    83         
    84         status = FTD2XX.Write(handle, new byte[] { 0x38 });
    85         if (status != FT_STATUS.FT_OK) {
    86           report.AppendLine("Write Status: " + status);
    87           FTD2XX.FT_Close(handle);
    88           continue;
    89         }
    90 
    91         bool isValid = false;
    92         byte protocolVersion = 0;
    93 
    94         int j = 0;
    95         while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
    96           Thread.Sleep(100);
    97           j++;
    98         }
    99         if (FTD2XX.BytesToRead(handle) > 0) {
   100           if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
   101             while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
   102               Thread.Sleep(100);
   103               j++;
   104             }
   105             int length = FTD2XX.BytesToRead(handle);
   106             if (length >= 284) {
   107               byte[] data = new byte[285];
   108               data[0] = TBalancer.STARTFLAG;
   109               for (int k = 1; k < data.Length; k++)
   110                 data[k] = FTD2XX.ReadByte(handle);
   111 
   112               // check protocol version 2X (protocols seen: 2C, 2A, 28)
   113               isValid = (data[274] & 0xF0) == 0x20;
   114               protocolVersion = data[274];
   115               if (!isValid) {
   116                 report.Append("Status: Wrong Protocol Version: 0x");
   117                 report.AppendLine(
   118                   protocolVersion.ToString("X", CultureInfo.InvariantCulture));
   119               }
   120             } else {
   121               report.AppendLine("Status: Wrong Message Length: " + length);
   122             }
   123           } else {
   124             report.AppendLine("Status: Wrong Startflag");
   125           }
   126         } else {
   127           report.AppendLine("Status: No Response");
   128         }
   129 
   130         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
   131         FTD2XX.FT_Close(handle);
   132 
   133         if (isValid) {
   134           report.AppendLine("Status: OK");
   135           hardware.Add(new TBalancer(i, protocolVersion, settings));
   136           return;
   137         }
   138         report.AppendLine();
   139       }
   140     }
   141 
   142     public IHardware[] Hardware {
   143       get {
   144         return hardware.ToArray();
   145       }
   146     }
   147 
   148     public string GetReport() {
   149       if (report.Length > 0) {
   150         report.Insert(0, "FTD2XX" + Environment.NewLine +
   151           Environment.NewLine);
   152         report.AppendLine();
   153         return report.ToString();
   154       } else
   155         return null;
   156     }
   157 
   158     public void Close() {
   159       foreach (TBalancer tbalancer in hardware)
   160         tbalancer.Close();
   161     }
   162   }
   163 }