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.Configuration;
|
moel@1
|
41 |
using System.Drawing;
|
moel@1
|
42 |
using System.IO;
|
moel@1
|
43 |
using System.IO.Ports;
|
moel@1
|
44 |
using System.Text;
|
moel@1
|
45 |
|
moel@1
|
46 |
namespace OpenHardwareMonitor.Hardware.TBalancer {
|
moel@1
|
47 |
public class TBalancer : IHardware {
|
moel@1
|
48 |
|
moel@1
|
49 |
private Image icon;
|
moel@1
|
50 |
private SerialPort serialPort;
|
moel@1
|
51 |
private Sensor[] digitalTemperatures = new Sensor[8];
|
moel@1
|
52 |
private Sensor[] analogTemperatures = new Sensor[4];
|
moel@1
|
53 |
private Sensor[] fans = new Sensor[4];
|
moel@1
|
54 |
private List<ISensor> active = new List<ISensor>();
|
moel@1
|
55 |
private List<ISensor> deactivating = new List<ISensor>();
|
moel@1
|
56 |
private int[] data;
|
moel@1
|
57 |
|
moel@1
|
58 |
public const byte STARTFLAG = 100;
|
moel@1
|
59 |
public const byte PROTOCOL_VERSION = 0x2A;
|
moel@1
|
60 |
|
moel@1
|
61 |
public TBalancer(string portName) {
|
moel@1
|
62 |
icon = Utilities.EmbeddedResources.GetImage("bigng.png");
|
moel@1
|
63 |
|
moel@1
|
64 |
try {
|
moel@1
|
65 |
serialPort = new SerialPort(portName, 19200, Parity.None, 8,
|
moel@1
|
66 |
StopBits.One);
|
moel@1
|
67 |
serialPort.Open();
|
moel@1
|
68 |
Update();
|
moel@1
|
69 |
} catch (IOException) { }
|
moel@1
|
70 |
|
moel@1
|
71 |
for (int i = 0; i < digitalTemperatures.Length; i++)
|
moel@1
|
72 |
digitalTemperatures[i] = new Sensor("Digital Sensor #" + (i + 1), i,
|
moel@1
|
73 |
SensorType.Temperature, this);
|
moel@1
|
74 |
|
moel@1
|
75 |
for (int i = 0; i < analogTemperatures.Length; i++)
|
moel@1
|
76 |
analogTemperatures[i] = new Sensor("Analog Sensor #" + (i + 1),
|
moel@1
|
77 |
i + digitalTemperatures.Length, SensorType.Temperature, this);
|
moel@1
|
78 |
}
|
moel@1
|
79 |
|
moel@1
|
80 |
private void ActivateSensor(Sensor sensor) {
|
moel@1
|
81 |
deactivating.Remove(sensor);
|
moel@1
|
82 |
if (!active.Contains(sensor)) {
|
moel@1
|
83 |
active.Add(sensor);
|
moel@15
|
84 |
if (SensorAdded != null)
|
moel@15
|
85 |
SensorAdded(sensor);
|
moel@1
|
86 |
}
|
moel@1
|
87 |
}
|
moel@1
|
88 |
|
moel@1
|
89 |
private void DeactivateSensor(Sensor sensor) {
|
moel@1
|
90 |
if (deactivating.Contains(sensor)) {
|
moel@1
|
91 |
active.Remove(sensor);
|
moel@1
|
92 |
deactivating.Remove(sensor);
|
moel@15
|
93 |
if (SensorRemoved != null)
|
moel@15
|
94 |
SensorRemoved(sensor);
|
moel@1
|
95 |
} else if (active.Contains(sensor)) {
|
moel@1
|
96 |
deactivating.Add(sensor);
|
moel@1
|
97 |
}
|
moel@1
|
98 |
}
|
moel@1
|
99 |
|
moel@1
|
100 |
private void ReadData() {
|
moel@1
|
101 |
int[] data = new int[285];
|
moel@1
|
102 |
for (int i = 0; i < data.Length; i++)
|
moel@1
|
103 |
data[i] = serialPort.ReadByte();
|
moel@1
|
104 |
|
moel@1
|
105 |
if (data[0] != STARTFLAG || data[274] != PROTOCOL_VERSION) {
|
moel@1
|
106 |
serialPort.DiscardInBuffer();
|
moel@1
|
107 |
return;
|
moel@1
|
108 |
}
|
moel@1
|
109 |
|
moel@1
|
110 |
for (int i = 0; i < digitalTemperatures.Length; i++)
|
moel@1
|
111 |
if (data[238 + i] > 0) {
|
moel@1
|
112 |
digitalTemperatures[i].Value = 0.5f * data[238 + i];
|
moel@1
|
113 |
ActivateSensor(digitalTemperatures[i]);
|
moel@1
|
114 |
} else {
|
moel@1
|
115 |
DeactivateSensor(digitalTemperatures[i]);
|
moel@1
|
116 |
}
|
moel@1
|
117 |
|
moel@1
|
118 |
for (int i = 0; i < analogTemperatures.Length; i++)
|
moel@1
|
119 |
if (data[260 + i] > 0) {
|
moel@1
|
120 |
analogTemperatures[i].Value = 0.5f * data[260 + i];
|
moel@1
|
121 |
ActivateSensor(analogTemperatures[i]);
|
moel@1
|
122 |
} else {
|
moel@1
|
123 |
DeactivateSensor(analogTemperatures[i]);
|
moel@1
|
124 |
}
|
moel@1
|
125 |
|
moel@1
|
126 |
for (int i = 0; i < fans.Length; i++) {
|
moel@1
|
127 |
float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
|
moel@1
|
128 |
|
moel@1
|
129 |
if (fans[i] == null)
|
moel@1
|
130 |
fans[i] = new Sensor("Fan #" + (i + 1), i, maxRPM, SensorType.Fan,
|
moel@1
|
131 |
this);
|
moel@1
|
132 |
|
moel@1
|
133 |
if ((data[136] & (1 << i)) == 0)
|
moel@1
|
134 |
fans[i].Value = maxRPM * 0.01f * data[156 + i]; // pwm mode
|
moel@1
|
135 |
else
|
moel@1
|
136 |
fans[i].Value = maxRPM * 0.01f * data[141 + i]; // analog mode
|
moel@1
|
137 |
ActivateSensor(fans[i]);
|
moel@1
|
138 |
}
|
moel@1
|
139 |
this.data = data;
|
moel@1
|
140 |
}
|
moel@1
|
141 |
|
moel@1
|
142 |
public Image Icon {
|
moel@1
|
143 |
get { return icon; }
|
moel@1
|
144 |
}
|
moel@1
|
145 |
|
moel@1
|
146 |
public string Name {
|
moel@1
|
147 |
get { return "T-Balancer bigNG"; }
|
moel@1
|
148 |
}
|
moel@1
|
149 |
|
moel@1
|
150 |
public string Identifier {
|
moel@1
|
151 |
get { return "/bigng/" +
|
moel@1
|
152 |
serialPort.PortName.TrimStart(new char[]{'/'}).ToLower(); }
|
moel@1
|
153 |
}
|
moel@1
|
154 |
|
moel@1
|
155 |
public ISensor[] Sensors {
|
moel@1
|
156 |
get { return active.ToArray(); }
|
moel@1
|
157 |
}
|
moel@1
|
158 |
|
moel@1
|
159 |
public string GetReport() {
|
moel@1
|
160 |
StringBuilder r = new StringBuilder();
|
moel@1
|
161 |
|
moel@1
|
162 |
r.AppendLine("T-Balancer bigNG");
|
moel@1
|
163 |
r.AppendLine();
|
moel@1
|
164 |
r.Append("Port Name: "); r.AppendLine(serialPort.PortName);
|
moel@1
|
165 |
r.AppendLine();
|
moel@1
|
166 |
r.AppendLine("System Information Answer");
|
moel@1
|
167 |
r.AppendLine();
|
moel@1
|
168 |
|
moel@1
|
169 |
r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
|
moel@1
|
170 |
r.AppendLine();
|
moel@1
|
171 |
for (int i = 0; i <= 0x11; i++) {
|
moel@1
|
172 |
r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append(" ");
|
moel@1
|
173 |
for (int j = 0; j <= 0xF; j++) {
|
moel@1
|
174 |
int index = ((i << 4) | j);
|
moel@1
|
175 |
if (index < data.Length) {
|
moel@1
|
176 |
r.Append(" ");
|
moel@1
|
177 |
r.Append(data[index].ToString("X2"));
|
moel@1
|
178 |
}
|
moel@1
|
179 |
}
|
moel@1
|
180 |
r.AppendLine();
|
moel@1
|
181 |
}
|
moel@1
|
182 |
r.AppendLine();
|
moel@1
|
183 |
|
moel@1
|
184 |
return r.ToString();
|
moel@1
|
185 |
}
|
moel@1
|
186 |
|
moel@1
|
187 |
public void Update() {
|
moel@1
|
188 |
while (serialPort.BytesToRead >= 285)
|
moel@1
|
189 |
ReadData();
|
moel@1
|
190 |
serialPort.Write(new byte[] { 0x38 }, 0, 1);
|
moel@1
|
191 |
}
|
moel@1
|
192 |
|
moel@1
|
193 |
public void Close() {
|
moel@1
|
194 |
serialPort.Close();
|
moel@1
|
195 |
}
|
moel@1
|
196 |
|
moel@1
|
197 |
public event SensorEventHandler SensorAdded;
|
moel@1
|
198 |
public event SensorEventHandler SensorRemoved;
|
moel@1
|
199 |
}
|
moel@1
|
200 |
}
|