Added support for Fintek F71858 chips. Corrected the number of fans for Fintek.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
40 using System.Configuration;
43 using System.IO.Ports;
46 namespace OpenHardwareMonitor.Hardware.TBalancer {
47 public class TBalancer : IHardware {
49 private string portName;
51 private SerialPort serialPort;
52 private byte protocolVersion;
53 private Sensor[] digitalTemperatures = new Sensor[8];
54 private Sensor[] analogTemperatures = new Sensor[4];
55 private Sensor[] sensorhubTemperatures = new Sensor[6];
56 private Sensor[] sensorhubFlows = new Sensor[2];
57 private Sensor[] fans = new Sensor[4];
58 private Sensor[] miniNGTemperatures = new Sensor[4];
59 private Sensor[] miniNGFans = new Sensor[4];
60 private List<ISensor> active = new List<ISensor>();
61 private List<ISensor> deactivating = new List<ISensor>();
62 private int[] primaryData = new int[0];
63 private int[] alternativeData = new int[0];
65 public const byte STARTFLAG = 100;
66 public const byte ENDFLAG = 254;
68 private delegate void MethodDelegate();
69 private MethodDelegate alternativeRequest;
71 public TBalancer(string portName, byte protocolVersion) {
72 this.portName = portName;
73 this.icon = Utilities.EmbeddedResources.GetImage("bigng.png");
74 this.protocolVersion = protocolVersion;
76 ParameterDescription[] parameter = new ParameterDescription[] {
77 new ParameterDescription("Offset", "Temperature offset.", 0)
80 for (int i = 0; i < digitalTemperatures.Length; i++)
81 digitalTemperatures[i] = new Sensor("Digital Sensor #" + (i + 1),
82 offset + i, null, SensorType.Temperature, this, parameter);
83 offset += digitalTemperatures.Length;
85 for (int i = 0; i < analogTemperatures.Length; i++)
86 analogTemperatures[i] = new Sensor("Analog Sensor #" + (i + 1),
87 offset + i, null, SensorType.Temperature, this, parameter);
88 offset += analogTemperatures.Length;
90 for (int i = 0; i < sensorhubTemperatures.Length; i++)
91 sensorhubTemperatures[i] = new Sensor("Sensorhub Sensor #" + (i + 1),
92 offset + i, null, SensorType.Temperature, this, parameter);
93 offset += sensorhubTemperatures.Length;
95 for (int i = 0; i < sensorhubFlows.Length; i++)
96 sensorhubFlows[i] = new Sensor("Flowmeter #" + (i + 1),
97 offset + i, null, SensorType.Flow, this, new ParameterDescription[] {
98 new ParameterDescription("Impulse Rate",
99 "The impulse rate of the flowmeter in pulses/L", 509)
101 offset += sensorhubFlows.Length;
103 for (int i = 0; i < miniNGTemperatures.Length; i++)
104 miniNGTemperatures[i] = new Sensor("miniNG #" + (i / 2 + 1) +
105 " Sensor #" + (i % 2 + 1), offset + i, null, SensorType.Temperature,
107 offset += miniNGTemperatures.Length;
109 alternativeRequest = new MethodDelegate(DelayedAlternativeRequest);
112 serialPort = new SerialPort(portName, 19200, Parity.None, 8,
116 } catch (IOException) { }
119 private void ActivateSensor(Sensor sensor) {
120 deactivating.Remove(sensor);
121 if (!active.Contains(sensor)) {
123 if (SensorAdded != null)
128 private void DeactivateSensor(Sensor sensor) {
129 if (deactivating.Contains(sensor)) {
130 active.Remove(sensor);
131 deactivating.Remove(sensor);
132 if (SensorRemoved != null)
133 SensorRemoved(sensor);
134 } else if (active.Contains(sensor)) {
135 deactivating.Add(sensor);
139 private void ReadminiNG(int[] data, int number) {
140 int offset = 1 + number * 65;
142 if (data[offset + 61] != ENDFLAG)
145 for (int i = 0; i < 2; i++) {
146 Sensor sensor = miniNGTemperatures[number * 2 + i];
147 if (data[offset + 7 + i] > 0) {
148 sensor.Value = 0.5f * data[offset + 7 + i] +
149 sensor.Parameters[0].Value;
150 ActivateSensor(sensor);
152 DeactivateSensor(sensor);
156 for (int i = 0; i < 2; i++) {
157 float maxRPM = 20.0f * data[offset + 44 + 2 * i];
159 if (miniNGFans[number * 2 + i] == null)
160 miniNGFans[number * 2 + i] =
161 new Sensor("miniNG #" + (number + 1) + " Fan #" + (i + 1),
162 4 + number * 2 + i, maxRPM, SensorType.Fan, this);
164 Sensor sensor = miniNGFans[number * 2 + i];
166 sensor.Value = 20.0f * data[offset + 43 + 2 * i];
167 ActivateSensor(sensor);
171 private void ReadData() {
172 int[] data = new int[285];
173 for (int i = 0; i < data.Length; i++)
174 data[i] = serialPort.ReadByte();
176 if (data[0] != STARTFLAG) {
177 serialPort.DiscardInBuffer();
181 if (data[1] == 255) { // bigNG
183 if (data[274] != protocolVersion)
186 this.primaryData = data;
188 for (int i = 0; i < digitalTemperatures.Length; i++)
189 if (data[238 + i] > 0) {
190 digitalTemperatures[i].Value = 0.5f * data[238 + i] +
191 digitalTemperatures[i].Parameters[0].Value;
192 ActivateSensor(digitalTemperatures[i]);
194 DeactivateSensor(digitalTemperatures[i]);
197 for (int i = 0; i < analogTemperatures.Length; i++)
198 if (data[260 + i] > 0) {
199 analogTemperatures[i].Value = 0.5f * data[260 + i] +
200 analogTemperatures[i].Parameters[0].Value;
201 ActivateSensor(analogTemperatures[i]);
203 DeactivateSensor(analogTemperatures[i]);
206 for (int i = 0; i < sensorhubTemperatures.Length; i++)
207 if (data[246 + i] > 0) {
208 sensorhubTemperatures[i].Value = 0.5f * data[246 + i] +
209 sensorhubTemperatures[i].Parameters[0].Value;
210 ActivateSensor(sensorhubTemperatures[i]);
212 DeactivateSensor(sensorhubTemperatures[i]);
215 for (int i = 0; i < sensorhubFlows.Length; i++)
216 if (data[231 + i] > 0 && data[234] > 0) {
217 float pulsesPerSecond = (data[231 + i] * 4.0f) / data[234];
218 float pulsesPerLiter = sensorhubFlows[i].Parameters[0].Value;
219 sensorhubFlows[i].Value = pulsesPerSecond * 3600 / pulsesPerLiter;
220 ActivateSensor(sensorhubFlows[i]);
222 DeactivateSensor(sensorhubFlows[i]);
225 for (int i = 0; i < fans.Length; i++) {
226 float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
229 fans[i] = new Sensor("Fan #" + (i + 1), i, maxRPM, SensorType.Fan,
230 this, new ParameterDescription[] {
231 new ParameterDescription("MaxRPM",
232 "Maximum revolutions per minute (RPM) of the fan.", maxRPM)
235 if ((data[136] & (1 << i)) == 0) // pwm mode
236 fans[i].Value = fans[i].Parameters[0].Value * 0.01f * data[156 + i];
238 fans[i].Value = fans[i].Parameters[0].Value * 0.01f * data[141 + i];
239 ActivateSensor(fans[i]);
242 } else if (data[1] == 253) { // miniNG #1
243 this.alternativeData = data;
247 if (data[66] == 252) // miniNG #2
257 get { return "T-Balancer bigNG"; }
260 public string Identifier {
261 get { return "/bigng/" +
262 this.portName.TrimStart(new char[]{'/'}).ToLower(); }
265 public IHardware[] SubHardware {
266 get { return new IHardware[0]; }
269 public ISensor[] Sensors {
270 get { return active.ToArray(); }
273 public string GetReport() {
274 StringBuilder r = new StringBuilder();
276 r.AppendLine("T-Balancer bigNG");
278 r.Append("Port Name: "); r.AppendLine(serialPort.PortName);
281 r.AppendLine("Primary System Information Answer");
283 r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
285 for (int i = 0; i <= 0x11; i++) {
286 r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append(" ");
287 for (int j = 0; j <= 0xF; j++) {
288 int index = ((i << 4) | j);
289 if (index < primaryData.Length) {
291 r.Append(primaryData[index].ToString("X2"));
298 if (alternativeData.Length > 0) {
299 r.AppendLine("Alternative System Information Answer");
301 r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
303 for (int i = 0; i <= 0x11; i++) {
304 r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append(" ");
305 for (int j = 0; j <= 0xF; j++) {
306 int index = ((i << 4) | j);
307 if (index < alternativeData.Length) {
309 r.Append(alternativeData[index].ToString("X2"));
320 private void DelayedAlternativeRequest() {
321 System.Threading.Thread.Sleep(500);
323 if (serialPort.IsOpen)
324 serialPort.Write(new byte[] { 0x37 }, 0, 1);
325 } catch (Exception) { }
328 public void Update() {
329 while (serialPort.BytesToRead >= 285)
331 if (serialPort.BytesToRead == 1)
332 serialPort.ReadByte();
334 serialPort.Write(new byte[] { 0x38 }, 0, 1);
335 alternativeRequest.BeginInvoke(null, null);
338 public void Close() {
342 public event SensorEventHandler SensorAdded;
343 public event SensorEventHandler SensorRemoved;