Hardware/Heatmaster/Heatmaster.cs
author moel.mich
Mon, 23 Aug 2010 20:00:06 +0000
changeset 172 c9d8de472546
parent 171 81ab5e53122e
child 173 fb96c0ca3c2d
permissions -rw-r--r--
Fixed a few problems with the Heatmaster.
moel@171
     1
/*
moel@171
     2
  
moel@171
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@171
     4
moel@171
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@171
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@171
     7
  the License. You may obtain a copy of the License at
moel@171
     8
 
moel@171
     9
  http://www.mozilla.org/MPL/
moel@171
    10
moel@171
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@171
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@171
    13
  for the specific language governing rights and limitations under the License.
moel@171
    14
moel@171
    15
  The Original Code is the Open Hardware Monitor code.
moel@171
    16
moel@171
    17
  The Initial Developer of the Original Code is 
moel@171
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@171
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@171
    20
  the Initial Developer. All Rights Reserved.
moel@171
    21
moel@171
    22
  Contributor(s):
moel@171
    23
moel@171
    24
  Alternatively, the contents of this file may be used under the terms of
moel@171
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@171
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@171
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@171
    28
  of those above. If you wish to allow use of your version of this file only
moel@171
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@171
    30
  use your version of this file under the terms of the MPL, indicate your
moel@171
    31
  decision by deleting the provisions above and replace them with the notice
moel@171
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@171
    33
  the provisions above, a recipient may use your version of this file under
moel@171
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@171
    35
 
moel@171
    36
*/
moel@171
    37
moel@171
    38
using System;
moel@171
    39
using System.Collections.Generic;
moel@171
    40
using System.IO;
moel@171
    41
using System.IO.Ports;
moel@171
    42
using System.Text;
moel@171
    43
using System.Text.RegularExpressions;
moel@171
    44
using System.Threading;
moel@171
    45
moel@171
    46
namespace OpenHardwareMonitor.Hardware.Heatmaster {
moel@171
    47
  internal class Heatmaster : Hardware {
moel@171
    48
moel@171
    49
    private string portName;
moel@171
    50
    private SerialPort serialPort;
moel@171
    51
moel@171
    52
    private int hardwareRevision;
moel@171
    53
    private int firmwareRevision;
moel@171
    54
    private int firmwareCRC;
moel@171
    55
moel@171
    56
    private Sensor[] fans;
moel@171
    57
    private Sensor[] controls;
moel@171
    58
    private Sensor[] temperatures;
moel@171
    59
    private Sensor[] flows;
moel@171
    60
    private Sensor[] relays;
moel@171
    61
    
moel@171
    62
    private bool available = false;
moel@171
    63
moel@172
    64
    private StringBuilder buffer = new StringBuilder();
moel@172
    65
moel@171
    66
    private string ReadLine(int timeout) {
moel@171
    67
      int i = 0;
moel@171
    68
      StringBuilder builder = new StringBuilder();
moel@171
    69
      while (i <= timeout) {
moel@171
    70
        while (serialPort.BytesToRead > 0) {
moel@171
    71
          byte b = (byte)serialPort.ReadByte();
moel@171
    72
          switch (b) {
moel@171
    73
            case 0xAA: return ((char)b).ToString();
moel@171
    74
            case 0x0D: return builder.ToString();
moel@171
    75
            default: builder.Append((char)b); break;
moel@171
    76
          }
moel@171
    77
        }
moel@171
    78
        i++;
moel@171
    79
        Thread.Sleep(1);
moel@171
    80
      }
moel@171
    81
      throw new TimeoutException();
moel@171
    82
    }
moel@171
    83
moel@171
    84
    private string ReadField(int device, char field) {
moel@171
    85
      serialPort.WriteLine("[0:" + device + "]R" + field);
moel@171
    86
      for (int i = 0; i < 5; i++) {
moel@172
    87
        string s = ReadLine(200);
moel@171
    88
        Match match = Regex.Match(s, @"-\[0:" + device.ToString() + @"\]R" +
moel@171
    89
          Regex.Escape(field.ToString()) + ":(.*)");
moel@171
    90
        if (match.Success) 
moel@171
    91
          return match.Groups[1].Value;
moel@171
    92
      }
moel@171
    93
      return null;
moel@171
    94
    }
moel@171
    95
moel@171
    96
    private string ReadString(int device, char field) {
moel@171
    97
      string s = ReadField(device, field);
moel@171
    98
      if (s != null && s[0] == '"' && s[s.Length - 1] == '"')
moel@171
    99
        return s.Substring(1, s.Length - 2);
moel@171
   100
      else
moel@171
   101
        return null;
moel@171
   102
    }
moel@171
   103
moel@171
   104
    private int ReadInteger(int device, char field) {
moel@171
   105
      string s = ReadField(device, field);      
moel@171
   106
      int i;
moel@171
   107
      if (int.TryParse(s, out i))
moel@171
   108
        return i;
moel@171
   109
      else
moel@171
   110
        return 0;
moel@171
   111
    }
moel@171
   112
moel@171
   113
    private bool WriteField(int device, char field, string value) {
moel@171
   114
      serialPort.WriteLine("[0:" + device + "]W" + field + ":" + value);
moel@171
   115
      for (int i = 0; i < 5; i++) {
moel@172
   116
        string s = ReadLine(200);
moel@171
   117
        Match match = Regex.Match(s, @"-\[0:" + device.ToString() + @"\]W" + 
moel@171
   118
          Regex.Escape(field.ToString()) + ":" + value);
moel@171
   119
        if (match.Success)
moel@171
   120
          return true;
moel@171
   121
      }
moel@171
   122
      return false;
moel@171
   123
    }
moel@171
   124
moel@171
   125
    private bool WriteInteger(int device, char field, int value) {
moel@171
   126
      return WriteField(device, field, value.ToString());
moel@171
   127
    }
moel@171
   128
moel@171
   129
    private bool WriteString(int device, char field, string value) {
moel@171
   130
      return WriteField(device, field, '"' + value + '"');
moel@171
   131
    }
moel@171
   132
moel@171
   133
    public Heatmaster(string portName, ISettings settings) {
moel@171
   134
moel@171
   135
      this.portName = portName;
moel@171
   136
      try {
moel@171
   137
        serialPort = new SerialPort(portName, 38400, Parity.None, 8,
moel@171
   138
          StopBits.One);
moel@171
   139
        serialPort.Open();
moel@171
   140
        serialPort.NewLine = ((char)0x0D).ToString();
moel@171
   141
        
moel@171
   142
        hardwareRevision = ReadInteger(0, 'H');
moel@171
   143
        firmwareRevision = ReadInteger(0, 'V');
moel@171
   144
        firmwareCRC = ReadInteger(0, 'C');
moel@171
   145
moel@171
   146
        int fanCount = ReadInteger(32, '?');
moel@171
   147
        int temperatureCount = ReadInteger(48, '?');
moel@171
   148
        int flowCount = ReadInteger(64, '?');
moel@171
   149
        int relayCount =  ReadInteger(80, '?');
moel@171
   150
moel@171
   151
        fans = new Sensor[fanCount];
moel@171
   152
        controls = new Sensor[fanCount];
moel@171
   153
        for (int i = 0; i < fanCount; i++) {
moel@171
   154
          int device = 33 + i;
moel@171
   155
          string name = ReadString(device, 'C');
moel@171
   156
          fans[i] = new Sensor(name, device, SensorType.Fan, this, settings);          
moel@171
   157
          fans[i].Value = ReadInteger(device, 'R');
moel@171
   158
          ActivateSensor(fans[i]);
moel@171
   159
          controls[i] =
moel@171
   160
            new Sensor(name, device, SensorType.Control, this, settings);
moel@171
   161
          controls[i].Value = (100 / 255.0f) * ReadInteger(device, 'P');
moel@171
   162
          ActivateSensor(controls[i]);
moel@171
   163
        }
moel@171
   164
        
moel@171
   165
        for (int i = 0; i < fanCount; i++) {
moel@171
   166
          int device = 33 + i;
moel@171
   167
          string name = ReadString(device, 'C');
moel@171
   168
          
moel@171
   169
          fans[i].Value = ReadInteger(device, 'R');
moel@171
   170
          ActivateSensor(fans[i]);
moel@171
   171
        }
moel@171
   172
moel@171
   173
        temperatures = new Sensor[temperatureCount];
moel@171
   174
        for (int i = 0; i < temperatureCount; i++) {
moel@171
   175
          int device = 49 + i;
moel@171
   176
          string name = ReadString(device, 'C');
moel@171
   177
          temperatures[i] =
moel@171
   178
            new Sensor(name, device, SensorType.Temperature, this, settings);
moel@171
   179
          int value = ReadInteger(device, 'T');
moel@171
   180
          temperatures[i].Value = 0.1f * value;
moel@171
   181
          if (value != -32768)
moel@171
   182
            ActivateSensor(temperatures[i]);
moel@171
   183
        }
moel@171
   184
moel@171
   185
        flows = new Sensor[flowCount];
moel@171
   186
        for (int i = 0; i < flowCount; i++) {
moel@171
   187
          int device = 65 + i;
moel@171
   188
          string name = ReadString(device, 'C');
moel@171
   189
          flows[i] = new Sensor(name, device, SensorType.Flow, this, settings);
moel@171
   190
          flows[i].Value = 0.1f * ReadInteger(device, 'L');
moel@171
   191
          ActivateSensor(flows[i]);
moel@171
   192
        }
moel@171
   193
moel@171
   194
        relays = new Sensor[relayCount];
moel@171
   195
        for (int i = 0; i < relayCount; i++) {
moel@171
   196
          int device = 81 + i;
moel@171
   197
          string name = ReadString(device, 'C');
moel@171
   198
          relays[i] = 
moel@171
   199
            new Sensor(name, device, SensorType.Control, this, settings);
moel@171
   200
          relays[i].Value = 100 * ReadInteger(device, 'S');
moel@171
   201
          ActivateSensor(relays[i]);
moel@171
   202
        }
moel@171
   203
moel@171
   204
        // set the update rate to 2 Hz
moel@171
   205
        WriteInteger(0, 'L', 2);
moel@171
   206
        
moel@171
   207
        available = true;
moel@171
   208
moel@171
   209
      } catch (IOException) { } catch (TimeoutException) { }      
moel@171
   210
    }
moel@171
   211
moel@171
   212
    public override HardwareType HardwareType {
moel@171
   213
      get { return HardwareType.Heatmaster; }
moel@171
   214
    }
moel@171
   215
moel@171
   216
    public override Identifier Identifier {
moel@171
   217
      get {
moel@171
   218
        return new Identifier("heatmaster",
moel@171
   219
          serialPort.PortName.TrimStart(new char[]{'/'}).ToLowerInvariant());
moel@171
   220
      }
moel@171
   221
    }
moel@171
   222
moel@171
   223
    public override string Name {
moel@171
   224
      get { return "Heatmaster"; }
moel@171
   225
    }
moel@171
   226
moel@172
   227
    private void ProcessUpdateLine(string line) {
moel@172
   228
      Match match = Regex.Match(line, @">\[0:(\d+)\]([0-9:\|-]+)");
moel@172
   229
      if (match.Success) {
moel@172
   230
        int device;
moel@172
   231
        if (int.TryParse(match.Groups[1].Value, out device)) {
moel@172
   232
          foreach (string s in match.Groups[2].Value.Split('|')) {
moel@172
   233
            string[] strings = s.Split(':');
moel@172
   234
            int[] ints = new int[strings.Length];
moel@172
   235
            for (int i = 0; i < ints.Length; i++)
moel@172
   236
              ints[i] = int.Parse(strings[i]);
moel@172
   237
            switch (device) {
moel@172
   238
              case 32:
moel@172
   239
                if (ints.Length == 3 && ints[0] <= fans.Length) {
moel@172
   240
                  fans[ints[0] - 1].Value = ints[1];
moel@172
   241
                  controls[ints[0] - 1].Value = (100 / 255.0f) * ints[2];
moel@172
   242
                }
moel@172
   243
                break;
moel@172
   244
              case 48:
moel@172
   245
                if (ints.Length == 2 && ints[0] <= temperatures.Length)
moel@172
   246
                  temperatures[ints[0] - 1].Value = 0.1f * ints[1];
moel@172
   247
                break;
moel@172
   248
              case 64:
moel@172
   249
                if (ints.Length == 3 && ints[0] <= flows.Length)
moel@172
   250
                  flows[ints[0] - 1].Value = 0.1f * ints[1];
moel@172
   251
                break;
moel@172
   252
              case 80:
moel@172
   253
                if (ints.Length == 2 && ints[0] <= relays.Length)
moel@172
   254
                  relays[ints[0] - 1].Value = 100 * ints[1];
moel@172
   255
                break;
moel@172
   256
            }
moel@172
   257
          }
moel@172
   258
        }
moel@172
   259
      }
moel@172
   260
    }
moel@172
   261
moel@171
   262
    public override void Update() {
moel@171
   263
      if (!available)
moel@171
   264
        return;
moel@171
   265
moel@171
   266
      while (serialPort.BytesToRead > 0) {
moel@172
   267
        byte b = (byte)serialPort.ReadByte();
moel@172
   268
        if (b == 0x0D) {
moel@172
   269
          ProcessUpdateLine(buffer.ToString());
moel@172
   270
          buffer.Length = 0;
moel@172
   271
        } else {
moel@172
   272
          buffer.Append((char)b);
moel@171
   273
        }
moel@171
   274
      }
moel@171
   275
    }
moel@171
   276
moel@171
   277
    public override string GetReport() {
moel@171
   278
      StringBuilder r = new StringBuilder();
moel@171
   279
moel@171
   280
      r.AppendLine("Heatmaster");
moel@171
   281
      r.AppendLine();
moel@171
   282
      r.Append("Port: ");
moel@171
   283
      r.AppendLine(portName);
moel@171
   284
      r.Append("Hardware Revision: ");
moel@171
   285
      r.AppendLine(hardwareRevision.ToString());
moel@171
   286
      r.Append("Firmware Revision: ");
moel@171
   287
      r.AppendLine(firmwareRevision.ToString());
moel@171
   288
      r.Append("Firmware CRC: ");
moel@171
   289
      r.AppendLine(firmwareCRC.ToString());
moel@171
   290
      r.AppendLine();
moel@171
   291
moel@171
   292
      return r.ToString();
moel@171
   293
    }
moel@171
   294
moel@171
   295
    public void Close() {
moel@171
   296
      serialPort.Close();
moel@171
   297
    }
moel@171
   298
  }
moel@171
   299
}