Fixed Issue 651.
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/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
13 using System.Globalization;
15 using System.Threading;
17 namespace OpenHardwareMonitor.Hardware.TBalancer {
18 internal class TBalancerGroup : IGroup {
20 private readonly List<TBalancer> hardware = new List<TBalancer>();
21 private readonly StringBuilder report = new StringBuilder();
23 public TBalancerGroup(ISettings settings) {
27 if (FTD2XX.FT_CreateDeviceInfoList(out numDevices) != FT_STATUS.FT_OK) {
28 report.AppendLine("Status: FT_CreateDeviceInfoList failed");
31 } catch (DllNotFoundException) { return; }
32 catch (ArgumentNullException) { return; }
33 catch (EntryPointNotFoundException) { return; }
34 catch (BadImageFormatException) { return; }
36 FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
37 if (FTD2XX.FT_GetDeviceInfoList(info, ref numDevices) != FT_STATUS.FT_OK)
39 report.AppendLine("Status: FT_GetDeviceInfoList failed");
43 // make sure numDevices is not larger than the info array
44 if (numDevices > info.Length)
45 numDevices = (uint)info.Length;
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());
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");
60 FT_STATUS status = FTD2XX.FT_Open(i, out handle);
61 if (status != FT_STATUS.FT_OK) {
62 report.AppendLine("Open Status: " + status);
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,
70 FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
71 FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
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);
81 byte protocolVersion = 0;
84 while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
88 if (FTD2XX.BytesToRead(handle) > 0) {
89 if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
90 while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
94 int length = FTD2XX.BytesToRead(handle);
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);
101 // check protocol version 2X (protocols seen: 2C, 2A, 28)
102 isValid = (data[274] & 0xF0) == 0x20;
103 protocolVersion = data[274];
105 report.Append("Status: Wrong Protocol Version: 0x");
107 protocolVersion.ToString("X", CultureInfo.InvariantCulture));
110 report.AppendLine("Status: Wrong Message Length: " + length);
113 report.AppendLine("Status: Wrong Startflag");
116 report.AppendLine("Status: No Response");
119 FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
120 FTD2XX.FT_Close(handle);
123 report.AppendLine("Status: OK");
124 hardware.Add(new TBalancer(i, protocolVersion, settings));
127 if (i < numDevices - 1)
132 public IHardware[] Hardware {
134 return hardware.ToArray();
138 public string GetReport() {
139 if (report.Length > 0) {
140 StringBuilder r = new StringBuilder();
141 r.AppendLine("FTD2XX");
150 public void Close() {
151 foreach (TBalancer tbalancer in hardware)