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