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