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