Hardware/TBalancer/TBalancerGroup.cs
author StephaneLenclud
Thu, 18 Apr 2013 23:25:10 +0200
branchMiniDisplay
changeset 444 9b09e2ee0968
parent 344 3145aadca3d2
permissions -rw-r--r--
Front View plug-in does not init if no sensor added.
Fixing some format to make strings shorter.
Now trying to start SoundGraphAccess.exe process from same directory.
Packed mode now can display three sensors along with the current time.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Globalization;
    14 using System.Text;
    15 using System.Threading;
    16 
    17 namespace OpenHardwareMonitor.Hardware.TBalancer {
    18   internal class TBalancerGroup : IGroup {
    19 
    20     private readonly List<TBalancer> hardware = new List<TBalancer>();
    21     private readonly StringBuilder report = new StringBuilder();
    22 
    23     public TBalancerGroup(ISettings settings) {
    24 
    25       uint numDevices;
    26       try {
    27         if (FTD2XX.FT_CreateDeviceInfoList(out numDevices) != FT_STATUS.FT_OK) {
    28           report.AppendLine("Status: FT_CreateDeviceInfoList failed");
    29           return;
    30         }
    31       } catch (DllNotFoundException) { return; } 
    32         catch (ArgumentNullException) { return; }
    33         catch (EntryPointNotFoundException) { return; }
    34         catch (BadImageFormatException) { return; }
    35    
    36       FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
    37       if (FTD2XX.FT_GetDeviceInfoList(info, ref numDevices) != FT_STATUS.FT_OK) 
    38       {
    39         report.AppendLine("Status: FT_GetDeviceInfoList failed");        
    40         return;
    41       }
    42  
    43       // make sure numDevices is not larger than the info array
    44       if (numDevices > info.Length)
    45         numDevices = (uint)info.Length;
    46 
    47       for (int i = 0; i < numDevices; i++) {
    48         report.Append("Device Index: ");
    49         report.AppendLine(i.ToString(CultureInfo.InvariantCulture));
    50         report.Append("Device Type: ");
    51         report.AppendLine(info[i].Type.ToString());
    52 
    53         // the T-Balancer always uses an FT232BM
    54         if (info[i].Type != FT_DEVICE.FT_DEVICE_232BM) {
    55           report.AppendLine("Status: Wrong device type");
    56           continue;
    57         }
    58 
    59         FT_HANDLE handle;
    60         FT_STATUS status = FTD2XX.FT_Open(i, out handle);
    61         if (status != FT_STATUS.FT_OK) {
    62           report.AppendLine("Open Status: " + status);
    63           continue;
    64         }
    65 
    66         FTD2XX.FT_SetBaudRate(handle, 19200);
    67         FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
    68         FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 
    69           0x13);
    70         FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
    71         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
    72         
    73         status = FTD2XX.Write(handle, new byte[] { 0x38 });
    74         if (status != FT_STATUS.FT_OK) {
    75           report.AppendLine("Write Status: " + status);
    76           FTD2XX.FT_Close(handle);
    77           continue;
    78         }
    79 
    80         bool isValid = false;
    81         byte protocolVersion = 0;
    82 
    83         int j = 0;
    84         while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
    85           Thread.Sleep(100);
    86           j++;
    87         }
    88         if (FTD2XX.BytesToRead(handle) > 0) {
    89           if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
    90             while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
    91               Thread.Sleep(100);
    92               j++;
    93             }
    94             int length = FTD2XX.BytesToRead(handle);
    95             if (length >= 284) {
    96               byte[] data = new byte[285];
    97               data[0] = TBalancer.STARTFLAG;
    98               for (int k = 1; k < data.Length; k++)
    99                 data[k] = FTD2XX.ReadByte(handle);
   100 
   101               // check protocol version 2X (protocols seen: 2C, 2A, 28)
   102               isValid = (data[274] & 0xF0) == 0x20;
   103               protocolVersion = data[274];
   104               if (!isValid) {
   105                 report.Append("Status: Wrong Protocol Version: 0x");
   106                 report.AppendLine(
   107                   protocolVersion.ToString("X", CultureInfo.InvariantCulture));
   108               }
   109             } else {
   110               report.AppendLine("Status: Wrong Message Length: " + length);
   111             }
   112           } else {
   113             report.AppendLine("Status: Wrong Startflag");
   114           }
   115         } else {
   116           report.AppendLine("Status: No Response");
   117         }
   118 
   119         FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
   120         FTD2XX.FT_Close(handle);
   121 
   122         if (isValid) {
   123           report.AppendLine("Status: OK");
   124           hardware.Add(new TBalancer(i, protocolVersion, settings));          
   125         }
   126 
   127         if (i < numDevices - 1)
   128           report.AppendLine();
   129       }
   130     }
   131 
   132     public IHardware[] Hardware {
   133       get {
   134         return hardware.ToArray();
   135       }
   136     }
   137 
   138     public string GetReport() {
   139       if (report.Length > 0) {
   140         StringBuilder r = new StringBuilder();
   141         r.AppendLine("FTD2XX");
   142         r.AppendLine();
   143         r.Append(report);
   144         r.AppendLine();
   145         return r.ToString();
   146       } else
   147         return null;
   148     }
   149 
   150     public void Close() {
   151       foreach (TBalancer tbalancer in hardware)
   152         tbalancer.Close();
   153     }
   154   }
   155 }