moel@1
|
1 |
/*
|
moel@1
|
2 |
|
moel@1
|
3 |
Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
moel@1
|
4 |
|
moel@1
|
5 |
The contents of this file are subject to the Mozilla Public License Version
|
moel@1
|
6 |
1.1 (the "License"); you may not use this file except in compliance with
|
moel@1
|
7 |
the License. You may obtain a copy of the License at
|
moel@1
|
8 |
|
moel@1
|
9 |
http://www.mozilla.org/MPL/
|
moel@1
|
10 |
|
moel@1
|
11 |
Software distributed under the License is distributed on an "AS IS" basis,
|
moel@1
|
12 |
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
moel@1
|
13 |
for the specific language governing rights and limitations under the License.
|
moel@1
|
14 |
|
moel@1
|
15 |
The Original Code is the Open Hardware Monitor code.
|
moel@1
|
16 |
|
moel@1
|
17 |
The Initial Developer of the Original Code is
|
moel@1
|
18 |
Michael Möller <m.moeller@gmx.ch>.
|
moel@1
|
19 |
Portions created by the Initial Developer are Copyright (C) 2009-2010
|
moel@1
|
20 |
the Initial Developer. All Rights Reserved.
|
moel@1
|
21 |
|
moel@1
|
22 |
Contributor(s):
|
moel@1
|
23 |
|
moel@1
|
24 |
Alternatively, the contents of this file may be used under the terms of
|
moel@1
|
25 |
either the GNU General Public License Version 2 or later (the "GPL"), or
|
moel@1
|
26 |
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
moel@1
|
27 |
in which case the provisions of the GPL or the LGPL are applicable instead
|
moel@1
|
28 |
of those above. If you wish to allow use of your version of this file only
|
moel@1
|
29 |
under the terms of either the GPL or the LGPL, and not to allow others to
|
moel@1
|
30 |
use your version of this file under the terms of the MPL, indicate your
|
moel@1
|
31 |
decision by deleting the provisions above and replace them with the notice
|
moel@1
|
32 |
and other provisions required by the GPL or the LGPL. If you do not delete
|
moel@1
|
33 |
the provisions above, a recipient may use your version of this file under
|
moel@1
|
34 |
the terms of any one of the MPL, the GPL or the LGPL.
|
moel@1
|
35 |
|
moel@1
|
36 |
*/
|
moel@1
|
37 |
|
moel@1
|
38 |
using System;
|
moel@1
|
39 |
using System.Collections.Generic;
|
moel@1
|
40 |
using System.IO;
|
moel@1
|
41 |
using System.IO.Ports;
|
moel@32
|
42 |
using System.Text;
|
moel@1
|
43 |
using System.Threading;
|
moel@1
|
44 |
|
moel@1
|
45 |
namespace OpenHardwareMonitor.Hardware.TBalancer {
|
moel@1
|
46 |
public class TBalancerGroup : IGroup {
|
moel@1
|
47 |
|
moel@1
|
48 |
private List<TBalancer> hardware = new List<TBalancer>();
|
moel@32
|
49 |
private StringBuilder report = new StringBuilder();
|
moel@1
|
50 |
|
moel@1
|
51 |
public TBalancerGroup() {
|
moel@32
|
52 |
|
moel@87
|
53 |
uint numDevices;
|
moel@87
|
54 |
try {
|
moel@87
|
55 |
FTD2XX.FT_CreateDeviceInfoList(out numDevices);
|
moel@87
|
56 |
} catch (DllNotFoundException) { return; }
|
moel@87
|
57 |
catch (ArgumentNullException) { return; }
|
moel@87
|
58 |
|
moel@87
|
59 |
FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
|
moel@87
|
60 |
FTD2XX.FT_GetDeviceInfoList(info, ref numDevices);
|
moel@32
|
61 |
|
moel@87
|
62 |
for (int i = 0; i < numDevices; i++) {
|
moel@87
|
63 |
report.Append("Device Index: "); report.AppendLine(i.ToString());
|
moel@87
|
64 |
|
moel@87
|
65 |
FT_HANDLE handle;
|
moel@87
|
66 |
FT_STATUS status;
|
moel@87
|
67 |
status = FTD2XX.FT_Open(i, out handle);
|
moel@87
|
68 |
if (status != FT_STATUS.FT_OK) {
|
moel@87
|
69 |
report.AppendLine("Open Status: " + status);
|
moel@87
|
70 |
continue;
|
moel@87
|
71 |
}
|
moel@32
|
72 |
|
moel@87
|
73 |
FTD2XX.FT_SetBaudRate(handle, 19200);
|
moel@87
|
74 |
FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
|
moel@87
|
75 |
FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11,
|
moel@87
|
76 |
0x13);
|
moel@87
|
77 |
FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
|
moel@87
|
78 |
FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
|
moel@87
|
79 |
|
moel@87
|
80 |
status = FTD2XX.Write(handle, new byte[] { 0x38 });
|
moel@87
|
81 |
if (status != FT_STATUS.FT_OK) {
|
moel@87
|
82 |
report.AppendLine("Write Status: " + status);
|
moel@87
|
83 |
FTD2XX.FT_Close(handle);
|
moel@87
|
84 |
continue;
|
moel@87
|
85 |
}
|
moel@87
|
86 |
|
moel@87
|
87 |
bool isValid = false;
|
moel@87
|
88 |
byte protocolVersion = 0;
|
moel@87
|
89 |
|
moel@87
|
90 |
int j = 0;
|
moel@87
|
91 |
while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
|
moel@87
|
92 |
Thread.Sleep(100);
|
moel@87
|
93 |
j++;
|
moel@87
|
94 |
}
|
moel@87
|
95 |
if (FTD2XX.BytesToRead(handle) > 0) {
|
moel@87
|
96 |
if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
|
moel@87
|
97 |
while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
|
moel@87
|
98 |
Thread.Sleep(100);
|
moel@87
|
99 |
j++;
|
moel@87
|
100 |
}
|
moel@87
|
101 |
int length = FTD2XX.BytesToRead(handle);
|
moel@87
|
102 |
if (length >= 284) {
|
moel@87
|
103 |
byte[] data = new byte[285];
|
moel@87
|
104 |
data[0] = TBalancer.STARTFLAG;
|
moel@87
|
105 |
for (int k = 1; k < data.Length; k++)
|
moel@87
|
106 |
data[k] = FTD2XX.ReadByte(handle);
|
moel@87
|
107 |
|
moel@87
|
108 |
// check protocol version 2X (protocols seen: 2C, 2A, 28)
|
moel@87
|
109 |
isValid = (data[274] & 0xF0) == 0x20;
|
moel@87
|
110 |
protocolVersion = data[274];
|
moel@87
|
111 |
if (!isValid) {
|
moel@87
|
112 |
report.Append("Status: Wrong Protocol Version: 0x");
|
moel@87
|
113 |
report.AppendLine(protocolVersion.ToString("X"));
|
moel@1
|
114 |
}
|
moel@32
|
115 |
} else {
|
moel@87
|
116 |
report.AppendLine("Status: Wrong Message Length: " + length);
|
moel@1
|
117 |
}
|
moel@87
|
118 |
} else {
|
moel@87
|
119 |
report.AppendLine("Status: Wrong Startflag");
|
moel@1
|
120 |
}
|
moel@87
|
121 |
} else {
|
moel@87
|
122 |
report.AppendLine("Status: No Response");
|
moel@87
|
123 |
}
|
moel@87
|
124 |
|
moel@87
|
125 |
FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
|
moel@87
|
126 |
FTD2XX.FT_Close(handle);
|
moel@87
|
127 |
|
moel@87
|
128 |
if (isValid) {
|
moel@87
|
129 |
report.AppendLine("Status: OK");
|
moel@87
|
130 |
hardware.Add(new TBalancer(i, protocolVersion));
|
moel@87
|
131 |
return;
|
moel@87
|
132 |
}
|
moel@41
|
133 |
report.AppendLine();
|
moel@1
|
134 |
}
|
moel@1
|
135 |
}
|
moel@1
|
136 |
|
moel@1
|
137 |
public IHardware[] Hardware {
|
moel@1
|
138 |
get {
|
moel@1
|
139 |
return hardware.ToArray();
|
moel@1
|
140 |
}
|
moel@1
|
141 |
}
|
moel@1
|
142 |
|
moel@1
|
143 |
public string GetReport() {
|
moel@32
|
144 |
if (report.Length > 0) {
|
moel@87
|
145 |
report.Insert(0, "FTD2XX" + Environment.NewLine +
|
moel@45
|
146 |
Environment.NewLine);
|
moel@45
|
147 |
report.AppendLine();
|
moel@32
|
148 |
return report.ToString();
|
moel@32
|
149 |
} else
|
moel@32
|
150 |
return null;
|
moel@1
|
151 |
}
|
moel@1
|
152 |
|
moel@1
|
153 |
public void Close() {
|
moel@1
|
154 |
foreach (TBalancer tbalancer in hardware)
|
moel@1
|
155 |
tbalancer.Close();
|
moel@1
|
156 |
}
|
moel@1
|
157 |
}
|
moel@1
|
158 |
}
|