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@32
|
53 |
string[] portNames = SerialPort.GetPortNames();
|
moel@1
|
54 |
for (int i = portNames.Length - 1; i >= 0; i--) {
|
moel@1
|
55 |
try {
|
moel@32
|
56 |
|
moel@1
|
57 |
SerialPort serialPort =
|
moel@1
|
58 |
new SerialPort(portNames[i], 19200, Parity.None, 8, StopBits.One);
|
moel@47
|
59 |
|
moel@1
|
60 |
bool isValid = false;
|
moel@33
|
61 |
byte protocolVersion = 0;
|
moel@41
|
62 |
report.Append("Port Name: "); report.AppendLine(portNames[i]);
|
moel@32
|
63 |
|
moel@47
|
64 |
try {
|
moel@47
|
65 |
serialPort.Open();
|
moel@47
|
66 |
} catch (UnauthorizedAccessException) {
|
moel@47
|
67 |
report.AppendLine("Exception: Access Denied");
|
moel@47
|
68 |
}
|
moel@47
|
69 |
|
moel@41
|
70 |
if (serialPort.IsOpen) {
|
moel@47
|
71 |
if (serialPort.CtsHolding) {
|
moel@47
|
72 |
serialPort.DiscardInBuffer();
|
moel@47
|
73 |
serialPort.DiscardOutBuffer();
|
moel@47
|
74 |
serialPort.Write(new byte[] { 0x38 }, 0, 1);
|
moel@47
|
75 |
int j = 0;
|
moel@47
|
76 |
while (serialPort.BytesToRead == 0 && j < 2) {
|
moel@47
|
77 |
Thread.Sleep(100);
|
moel@47
|
78 |
j++;
|
moel@47
|
79 |
}
|
moel@47
|
80 |
if (serialPort.BytesToRead > 0) {
|
moel@47
|
81 |
if (serialPort.ReadByte() == TBalancer.STARTFLAG) {
|
moel@47
|
82 |
while (serialPort.BytesToRead < 284 && j < 5) {
|
moel@47
|
83 |
Thread.Sleep(100);
|
moel@47
|
84 |
j++;
|
moel@47
|
85 |
}
|
moel@47
|
86 |
int length = serialPort.BytesToRead;
|
moel@47
|
87 |
if (length >= 284) {
|
moel@47
|
88 |
byte[] data = new byte[285];
|
moel@47
|
89 |
data[0] = TBalancer.STARTFLAG;
|
moel@47
|
90 |
for (int k = 1; k < data.Length; k++)
|
moel@47
|
91 |
data[k] = (byte)serialPort.ReadByte();
|
moel@32
|
92 |
|
moel@47
|
93 |
// check protocol version 2X (protocols seen: 2C, 2A, 28)
|
moel@47
|
94 |
isValid = (data[274] & 0xF0) == 0x20;
|
moel@47
|
95 |
protocolVersion = data[274];
|
moel@47
|
96 |
if (!isValid) {
|
moel@47
|
97 |
report.Append("Status: Wrong Protocol Version: 0x");
|
moel@47
|
98 |
report.AppendLine(protocolVersion.ToString("X"));
|
moel@47
|
99 |
}
|
moel@47
|
100 |
} else {
|
moel@47
|
101 |
report.AppendLine("Status: Wrong Message Length: " +length);
|
moel@41
|
102 |
}
|
moel@41
|
103 |
} else {
|
moel@47
|
104 |
report.AppendLine("Status: Wrong Startflag");
|
moel@32
|
105 |
}
|
moel@32
|
106 |
} else {
|
moel@47
|
107 |
report.AppendLine("Status: No Response");
|
moel@1
|
108 |
}
|
moel@32
|
109 |
} else {
|
moel@47
|
110 |
report.AppendLine("Status: Not Clear to Send");
|
moel@1
|
111 |
}
|
moel@47
|
112 |
serialPort.DiscardInBuffer();
|
moel@47
|
113 |
serialPort.Close();
|
moel@47
|
114 |
} else {
|
moel@41
|
115 |
report.AppendLine("Status: Port not Open");
|
moel@47
|
116 |
}
|
moel@1
|
117 |
if (isValid) {
|
moel@32
|
118 |
report.AppendLine("Status: OK");
|
moel@33
|
119 |
hardware.Add(new TBalancer(portNames[i], protocolVersion));
|
moel@1
|
120 |
return;
|
moel@1
|
121 |
}
|
moel@51
|
122 |
} catch (Exception e) {
|
moel@51
|
123 |
report.AppendLine(e.ToString());
|
moel@51
|
124 |
}
|
moel@41
|
125 |
report.AppendLine();
|
moel@1
|
126 |
}
|
moel@1
|
127 |
}
|
moel@1
|
128 |
|
moel@1
|
129 |
public IHardware[] Hardware {
|
moel@1
|
130 |
get {
|
moel@1
|
131 |
return hardware.ToArray();
|
moel@1
|
132 |
}
|
moel@1
|
133 |
}
|
moel@1
|
134 |
|
moel@1
|
135 |
public string GetReport() {
|
moel@32
|
136 |
if (report.Length > 0) {
|
moel@32
|
137 |
report.Insert(0, "Serial Port T-Balancer" + Environment.NewLine +
|
moel@45
|
138 |
Environment.NewLine);
|
moel@45
|
139 |
report.AppendLine();
|
moel@32
|
140 |
return report.ToString();
|
moel@32
|
141 |
} else
|
moel@32
|
142 |
return null;
|
moel@1
|
143 |
}
|
moel@1
|
144 |
|
moel@1
|
145 |
public void Close() {
|
moel@1
|
146 |
foreach (TBalancer tbalancer in hardware)
|
moel@1
|
147 |
tbalancer.Close();
|
moel@1
|
148 |
}
|
moel@1
|
149 |
}
|
moel@1
|
150 |
}
|