Hardware/TBalancer/TBalancer.cs
author moel.mich
Sun, 21 Feb 2010 18:10:30 +0000
changeset 57 142907c75be4
parent 45 eadad41d21a6
child 63 1a7c13ac7348
permissions -rw-r--r--
Added support for T-Balancer sensorhub and miniNG. Added a Flow sensor-type.
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@57
    49
    private string portName;
moel@1
    50
    private Image icon;
moel@1
    51
    private SerialPort serialPort;
moel@33
    52
    private byte protocolVersion;
moel@1
    53
    private Sensor[] digitalTemperatures = new Sensor[8];
moel@1
    54
    private Sensor[] analogTemperatures = new Sensor[4];
moel@57
    55
    private Sensor[] sensorhubTemperatures = new Sensor[6];
moel@57
    56
    private Sensor[] sensorhubFlows = new Sensor[2];
moel@1
    57
    private Sensor[] fans = new Sensor[4];
moel@57
    58
    private Sensor[] miniNGTemperatures = new Sensor[4];
moel@57
    59
    private Sensor[] miniNGFans = new Sensor[4];
moel@1
    60
    private List<ISensor> active = new List<ISensor>();
moel@1
    61
    private List<ISensor> deactivating = new List<ISensor>();
moel@57
    62
    private int[] primaryData = new int[0];
moel@57
    63
    private int[] alternativeData = new int[0];
moel@1
    64
moel@1
    65
    public const byte STARTFLAG = 100;
moel@57
    66
    public const byte ENDFLAG = 254;
moel@57
    67
moel@57
    68
    private delegate void MethodDelegate();
moel@57
    69
    private MethodDelegate alternativeRequest;    
moel@1
    70
moel@33
    71
    public TBalancer(string portName, byte protocolVersion) {
moel@57
    72
      this.portName = portName;
moel@57
    73
      this.icon = Utilities.EmbeddedResources.GetImage("bigng.png");
moel@33
    74
      this.protocolVersion = protocolVersion;
moel@1
    75
moel@57
    76
      int offset = 0;
moel@57
    77
      for (int i = 0; i < digitalTemperatures.Length; i++)
moel@57
    78
        digitalTemperatures[i] = new Sensor("Digital Sensor #" + (i + 1),
moel@57
    79
          offset + i, SensorType.Temperature, this);
moel@57
    80
      offset += digitalTemperatures.Length;
moel@57
    81
moel@57
    82
      for (int i = 0; i < analogTemperatures.Length; i++)
moel@57
    83
        analogTemperatures[i] = new Sensor("Analog Sensor #" + (i + 1),
moel@57
    84
          offset + i, SensorType.Temperature, this);
moel@57
    85
      offset += analogTemperatures.Length;
moel@57
    86
moel@57
    87
      for (int i = 0; i < sensorhubTemperatures.Length; i++)
moel@57
    88
        sensorhubTemperatures[i] = new Sensor("Sensorhub Sensor #" + (i + 1),
moel@57
    89
          offset + i, SensorType.Temperature, this);
moel@57
    90
      offset += sensorhubTemperatures.Length;
moel@57
    91
moel@57
    92
      for (int i = 0; i < sensorhubFlows.Length; i++)
moel@57
    93
        sensorhubFlows[i] = new Sensor("Flowmeter #" + (i + 1),
moel@57
    94
          offset + i, SensorType.Flow, this);
moel@57
    95
      offset += sensorhubFlows.Length;
moel@57
    96
moel@57
    97
      for (int i = 0; i < miniNGTemperatures.Length; i++)
moel@57
    98
        miniNGTemperatures[i] = new Sensor("miniNG #" + (i / 2 + 1) + 
moel@57
    99
          " Sensor #" + (i % 2 + 1), offset + i, SensorType.Temperature, this);
moel@57
   100
      offset += miniNGTemperatures.Length;
moel@57
   101
moel@57
   102
      alternativeRequest = new MethodDelegate(DelayedAlternativeRequest);
moel@57
   103
moel@1
   104
      try {
moel@1
   105
        serialPort = new SerialPort(portName, 19200, Parity.None, 8,
moel@1
   106
          StopBits.One);
moel@1
   107
        serialPort.Open();
moel@1
   108
        Update();
moel@57
   109
      } catch (IOException) { }      
moel@1
   110
    }
moel@1
   111
moel@1
   112
    private void ActivateSensor(Sensor sensor) {
moel@1
   113
      deactivating.Remove(sensor);
moel@1
   114
      if (!active.Contains(sensor)) {
moel@1
   115
        active.Add(sensor);
moel@15
   116
        if (SensorAdded != null)
moel@15
   117
          SensorAdded(sensor);
moel@1
   118
      }      
moel@1
   119
    }
moel@1
   120
moel@1
   121
    private void DeactivateSensor(Sensor sensor) {
moel@1
   122
      if (deactivating.Contains(sensor)) {
moel@1
   123
        active.Remove(sensor);
moel@1
   124
        deactivating.Remove(sensor);
moel@15
   125
        if (SensorRemoved != null)
moel@15
   126
          SensorRemoved(sensor);
moel@1
   127
      } else if (active.Contains(sensor)) {
moel@1
   128
        deactivating.Add(sensor);
moel@1
   129
      }     
moel@1
   130
    }
moel@1
   131
moel@57
   132
    private void ReadminiNG(int[] data, int number) {
moel@57
   133
      int offset = 1 + number * 65;
moel@57
   134
moel@57
   135
      if (data[offset + 61] != ENDFLAG)
moel@57
   136
        return;
moel@57
   137
moel@57
   138
      for (int i = 0; i < 2; i++) {
moel@57
   139
        Sensor sensor = miniNGTemperatures[number * 2 + i];
moel@57
   140
        if (data[offset + 7 + i] > 0) {
moel@57
   141
          sensor.Value = 0.5f * data[offset + 7 + i];
moel@57
   142
          ActivateSensor(sensor);
moel@57
   143
        } else {
moel@57
   144
          DeactivateSensor(sensor);
moel@57
   145
        }
moel@57
   146
      }
moel@57
   147
moel@57
   148
      for (int i = 0; i < 2; i++) {
moel@57
   149
        float maxRPM = 20.0f * data[offset + 44 + 2 * i];
moel@57
   150
moel@57
   151
        if (miniNGFans[number * 2 + i] == null)
moel@57
   152
          miniNGFans[number * 2 + i] = 
moel@57
   153
            new Sensor("miniNG #" + (number + 1) + " Fan #" + (i + 1),
moel@57
   154
            4 + number * 2 + i, maxRPM, SensorType.Fan, this);
moel@57
   155
        
moel@57
   156
        Sensor sensor = miniNGFans[number * 2 + i];
moel@57
   157
moel@57
   158
        sensor.Value = 20.0f * data[offset + 43 + 2 * i];
moel@57
   159
        ActivateSensor(sensor);
moel@57
   160
      }
moel@57
   161
    }
moel@57
   162
moel@1
   163
    private void ReadData() {
moel@1
   164
      int[] data = new int[285];
moel@1
   165
      for (int i = 0; i < data.Length; i++)
moel@1
   166
        data[i] = serialPort.ReadByte();
moel@1
   167
moel@57
   168
      if (data[0] != STARTFLAG) {
moel@1
   169
        serialPort.DiscardInBuffer();   
moel@1
   170
        return;
moel@1
   171
      }
moel@1
   172
moel@57
   173
      if (data[1] == 255) { // bigNG
moel@57
   174
moel@57
   175
        if (data[274] != protocolVersion) 
moel@57
   176
          return;
moel@57
   177
moel@57
   178
        this.primaryData = data;
moel@57
   179
moel@57
   180
        for (int i = 0; i < digitalTemperatures.Length; i++)
moel@57
   181
          if (data[238 + i] > 0) {
moel@57
   182
            digitalTemperatures[i].Value = 0.5f * data[238 + i];
moel@57
   183
            ActivateSensor(digitalTemperatures[i]);
moel@57
   184
          } else {
moel@57
   185
            DeactivateSensor(digitalTemperatures[i]);
moel@57
   186
          }
moel@57
   187
moel@57
   188
        for (int i = 0; i < analogTemperatures.Length; i++)
moel@57
   189
          if (data[260 + i] > 0) {
moel@57
   190
            analogTemperatures[i].Value = 0.5f * data[260 + i];
moel@57
   191
            ActivateSensor(analogTemperatures[i]);
moel@57
   192
          } else {
moel@57
   193
            DeactivateSensor(analogTemperatures[i]);
moel@57
   194
          }
moel@57
   195
moel@57
   196
        for (int i = 0; i < sensorhubTemperatures.Length; i++)
moel@57
   197
          if (data[246 + i] > 0) {
moel@57
   198
            sensorhubTemperatures[i].Value = 0.5f * data[246 + i];
moel@57
   199
            ActivateSensor(sensorhubTemperatures[i]);
moel@57
   200
          } else {
moel@57
   201
            DeactivateSensor(sensorhubTemperatures[i]);
moel@57
   202
          }
moel@57
   203
moel@57
   204
        for (int i = 0; i < sensorhubFlows.Length; i++)
moel@57
   205
          if (data[231 + i] > 0 && data[234] > 0) {
moel@57
   206
            float pulsesPerSecond = ((float)data[231 + i]) / data[234];
moel@57
   207
            const float pulsesPerLiter = 509;
moel@57
   208
            sensorhubFlows[i].Value = pulsesPerSecond * 3600 / pulsesPerLiter;
moel@57
   209
            ActivateSensor(sensorhubFlows[i]);
moel@57
   210
          } else {
moel@57
   211
            DeactivateSensor(sensorhubFlows[i]);
moel@57
   212
          }
moel@57
   213
moel@57
   214
        for (int i = 0; i < fans.Length; i++) {
moel@57
   215
          float maxRPM = 11.5f * ((data[149 + 2 * i] << 8) | data[148 + 2 * i]);
moel@57
   216
moel@57
   217
          if (fans[i] == null)
moel@57
   218
            fans[i] = new Sensor("Fan #" + (i + 1), i, maxRPM, SensorType.Fan,
moel@57
   219
              this);
moel@57
   220
moel@57
   221
          if ((data[136] & (1 << i)) == 0)
moel@57
   222
            fans[i].Value = maxRPM * 0.01f * data[156 + i]; // pwm mode
moel@57
   223
          else
moel@57
   224
            fans[i].Value = maxRPM * 0.01f * data[141 + i]; // analog mode
moel@57
   225
          ActivateSensor(fans[i]);
moel@1
   226
        }
moel@1
   227
moel@57
   228
      } else if (data[1] == 253) { // miniNG #1
moel@57
   229
        this.alternativeData = data;
moel@1
   230
moel@57
   231
        ReadminiNG(data, 0);        
moel@57
   232
              
moel@57
   233
        if (data[66] == 252)  // miniNG #2
moel@57
   234
          ReadminiNG(data, 1);
moel@57
   235
      } 
moel@1
   236
    }
moel@1
   237
moel@1
   238
    public Image Icon {
moel@1
   239
      get { return icon; }
moel@1
   240
    }
moel@1
   241
moel@1
   242
    public string Name {
moel@1
   243
      get { return "T-Balancer bigNG"; }
moel@1
   244
    }
moel@1
   245
moel@1
   246
    public string Identifier {
moel@1
   247
      get { return "/bigng/" + 
moel@57
   248
        this.portName.TrimStart(new char[]{'/'}).ToLower(); }
moel@1
   249
    }
moel@1
   250
moel@1
   251
    public ISensor[] Sensors {
moel@1
   252
      get { return active.ToArray(); }
moel@1
   253
    }
moel@1
   254
moel@1
   255
    public string GetReport() {
moel@1
   256
      StringBuilder r = new StringBuilder();
moel@1
   257
moel@1
   258
      r.AppendLine("T-Balancer bigNG");
moel@1
   259
      r.AppendLine();
moel@1
   260
      r.Append("Port Name: "); r.AppendLine(serialPort.PortName);
moel@1
   261
      r.AppendLine();
moel@57
   262
moel@57
   263
      r.AppendLine("Primary System Information Answer");
moel@1
   264
      r.AppendLine();
moel@1
   265
      r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@1
   266
      r.AppendLine();
moel@1
   267
      for (int i = 0; i <= 0x11; i++) {
moel@1
   268
        r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
moel@1
   269
        for (int j = 0; j <= 0xF; j++) {
moel@1
   270
          int index = ((i << 4) | j);
moel@57
   271
          if (index < primaryData.Length) {
moel@1
   272
            r.Append(" ");
moel@57
   273
            r.Append(primaryData[index].ToString("X2"));
moel@1
   274
          }          
moel@1
   275
        }
moel@1
   276
        r.AppendLine();
moel@1
   277
      }
moel@1
   278
      r.AppendLine();
moel@1
   279
moel@57
   280
      if (alternativeData.Length > 0) {
moel@57
   281
        r.AppendLine("Alternative System Information Answer");
moel@57
   282
        r.AppendLine();
moel@57
   283
        r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@57
   284
        r.AppendLine();
moel@57
   285
        for (int i = 0; i <= 0x11; i++) {
moel@57
   286
          r.Append(" "); r.Append((i << 4).ToString("X3")); r.Append("  ");
moel@57
   287
          for (int j = 0; j <= 0xF; j++) {
moel@57
   288
            int index = ((i << 4) | j);
moel@57
   289
            if (index < alternativeData.Length) {
moel@57
   290
              r.Append(" ");
moel@57
   291
              r.Append(alternativeData[index].ToString("X2"));
moel@57
   292
            }
moel@57
   293
          }
moel@57
   294
          r.AppendLine();
moel@57
   295
        }
moel@57
   296
        r.AppendLine();
moel@57
   297
      }
moel@57
   298
moel@1
   299
      return r.ToString();
moel@1
   300
    }
moel@1
   301
moel@57
   302
    private void DelayedAlternativeRequest() {
moel@57
   303
      System.Threading.Thread.Sleep(500);
moel@57
   304
      try {
moel@57
   305
        if (serialPort.IsOpen)
moel@57
   306
          serialPort.Write(new byte[] { 0x37 }, 0, 1);
moel@57
   307
      } catch (Exception) { }
moel@57
   308
    }
moel@57
   309
moel@1
   310
    public void Update() {
moel@1
   311
      while (serialPort.BytesToRead >= 285)
moel@1
   312
        ReadData();
moel@57
   313
      if (serialPort.BytesToRead == 1)
moel@57
   314
        serialPort.ReadByte();
moel@57
   315
moel@1
   316
      serialPort.Write(new byte[] { 0x38 }, 0, 1);
moel@57
   317
      alternativeRequest.BeginInvoke(null, null);
moel@1
   318
    }
moel@1
   319
moel@1
   320
    public void Close() {
moel@1
   321
      serialPort.Close();
moel@1
   322
    }
moel@1
   323
moel@1
   324
    public event SensorEventHandler SensorAdded;
moel@1
   325
    public event SensorEventHandler SensorRemoved;
moel@1
   326
  }
moel@1
   327
}