first commit
This commit is contained in:
@@ -0,0 +1,290 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// Copyright (C) LibreHardwareMonitor and Contributors.
|
||||
// Partial Copyright (C) Michael Möller <mmoeller@openhardwaremonitor.org> and Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.IO.Ports;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
|
||||
namespace LibreHardwareMonitor.Hardware.Controller.Heatmaster;
|
||||
|
||||
internal sealed class Heatmaster : Hardware, IDisposable
|
||||
{
|
||||
private readonly bool _available;
|
||||
private readonly StringBuilder _buffer = new();
|
||||
private readonly Sensor[] _controls;
|
||||
private readonly Sensor[] _fans;
|
||||
private readonly int _firmwareCrc;
|
||||
private readonly int _firmwareRevision;
|
||||
private readonly Sensor[] _flows;
|
||||
private readonly int _hardwareRevision;
|
||||
private readonly string _portName;
|
||||
private readonly Sensor[] _relays;
|
||||
private readonly Sensor[] _temperatures;
|
||||
private SerialPort _serialPort;
|
||||
|
||||
public Heatmaster(string portName, ISettings settings) : base("Heatmaster", new Identifier("heatmaster", portName.TrimStart('/').ToLowerInvariant()), settings)
|
||||
{
|
||||
_portName = portName;
|
||||
try
|
||||
{
|
||||
_serialPort = new SerialPort(portName, 38400, Parity.None, 8, StopBits.One);
|
||||
_serialPort.Open();
|
||||
_serialPort.NewLine = ((char)0x0D).ToString();
|
||||
|
||||
_hardwareRevision = ReadInteger(0, 'H');
|
||||
_firmwareRevision = ReadInteger(0, 'V');
|
||||
_firmwareCrc = ReadInteger(0, 'C');
|
||||
|
||||
int fanCount = Math.Min(ReadInteger(32, '?'), 4);
|
||||
int temperatureCount = Math.Min(ReadInteger(48, '?'), 6);
|
||||
int flowCount = Math.Min(ReadInteger(64, '?'), 1);
|
||||
int relayCount = Math.Min(ReadInteger(80, '?'), 1);
|
||||
|
||||
_fans = new Sensor[fanCount];
|
||||
_controls = new Sensor[fanCount];
|
||||
for (int i = 0; i < fanCount; i++)
|
||||
{
|
||||
int device = 33 + i;
|
||||
string name = ReadString(device, 'C');
|
||||
_fans[i] = new Sensor(name, device, SensorType.Fan, this, settings) { Value = ReadInteger(device, 'R') };
|
||||
ActivateSensor(_fans[i]);
|
||||
_controls[i] = new Sensor(name, device, SensorType.Control, this, settings) { Value = (100 / 255.0f) * ReadInteger(device, 'P') };
|
||||
ActivateSensor(_controls[i]);
|
||||
}
|
||||
|
||||
_temperatures = new Sensor[temperatureCount];
|
||||
for (int i = 0; i < temperatureCount; i++)
|
||||
{
|
||||
int device = 49 + i;
|
||||
string name = ReadString(device, 'C');
|
||||
_temperatures[i] = new Sensor(name, device, SensorType.Temperature, this, settings);
|
||||
int value = ReadInteger(device, 'T');
|
||||
_temperatures[i].Value = 0.1f * value;
|
||||
if (value != -32768)
|
||||
ActivateSensor(_temperatures[i]);
|
||||
}
|
||||
|
||||
_flows = new Sensor[flowCount];
|
||||
for (int i = 0; i < flowCount; i++)
|
||||
{
|
||||
int device = 65 + i;
|
||||
string name = ReadString(device, 'C');
|
||||
_flows[i] = new Sensor(name, device, SensorType.Flow, this, settings) { Value = 0.1f * ReadInteger(device, 'L') };
|
||||
ActivateSensor(_flows[i]);
|
||||
}
|
||||
|
||||
_relays = new Sensor[relayCount];
|
||||
for (int i = 0; i < relayCount; i++)
|
||||
{
|
||||
int device = 81 + i;
|
||||
string name = ReadString(device, 'C');
|
||||
_relays[i] = new Sensor(name, device, SensorType.Control, this, settings)
|
||||
{
|
||||
Value = 100 * ReadInteger(device, 'S')
|
||||
};
|
||||
ActivateSensor(_relays[i]);
|
||||
}
|
||||
|
||||
// set the update rate to 2 Hz
|
||||
WriteInteger(0, 'L', 2);
|
||||
_available = true;
|
||||
}
|
||||
catch (IOException)
|
||||
{ }
|
||||
catch (TimeoutException)
|
||||
{ }
|
||||
}
|
||||
|
||||
public override HardwareType HardwareType
|
||||
{
|
||||
get { return HardwareType.Cooler; }
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_serialPort != null)
|
||||
{
|
||||
_serialPort.Dispose();
|
||||
_serialPort = null;
|
||||
}
|
||||
}
|
||||
|
||||
private string ReadLine(int timeout)
|
||||
{
|
||||
int i = 0;
|
||||
StringBuilder builder = new();
|
||||
while (i <= timeout)
|
||||
{
|
||||
while (_serialPort.BytesToRead > 0)
|
||||
{
|
||||
byte b = (byte)_serialPort.ReadByte();
|
||||
switch (b)
|
||||
{
|
||||
case 0xAA: return ((char)b).ToString();
|
||||
case 0x0D: return builder.ToString();
|
||||
default:
|
||||
builder.Append((char)b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
|
||||
throw new TimeoutException();
|
||||
}
|
||||
|
||||
private string ReadField(int device, char field)
|
||||
{
|
||||
_serialPort.WriteLine("[0:" + device + "]R" + field);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
string s = ReadLine(200);
|
||||
Match match = Regex.Match(s, @"-\[0:" + device.ToString(CultureInfo.InvariantCulture) + @"\]R" + Regex.Escape(field.ToString(CultureInfo.InvariantCulture)) + ":(.*)");
|
||||
if (match.Success)
|
||||
return match.Groups[1].Value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private string ReadString(int device, char field)
|
||||
{
|
||||
string s = ReadField(device, field);
|
||||
if (s?[0] == '"' && s[s.Length - 1] == '"')
|
||||
return s.Substring(1, s.Length - 2);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private int ReadInteger(int device, char field)
|
||||
{
|
||||
string s = ReadField(device, field);
|
||||
if (int.TryParse(s, out int i))
|
||||
return i;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void WriteField(int device, char field, string value)
|
||||
{
|
||||
_serialPort.WriteLine("[0:" + device + "]W" + field + ":" + value);
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
string s = ReadLine(200);
|
||||
Match match = Regex.Match(s, @"-\[0:" + device.ToString(CultureInfo.InvariantCulture) + @"\]W" + Regex.Escape(field.ToString(CultureInfo.InvariantCulture)) + ":" + value);
|
||||
if (match.Success)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteInteger(int device, char field, int value)
|
||||
{
|
||||
WriteField(device, field, value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
private void ProcessUpdateLine(string line)
|
||||
{
|
||||
Match match = Regex.Match(line, @">\[0:(\d+)\]([0-9:\|-]+)");
|
||||
if (match.Success && int.TryParse(match.Groups[1].Value, out int device))
|
||||
{
|
||||
foreach (string s in match.Groups[2].Value.Split('|'))
|
||||
{
|
||||
string[] strings = s.Split(':');
|
||||
int[] ints = new int[strings.Length];
|
||||
bool valid = true;
|
||||
for (int i = 0; i < ints.Length; i++)
|
||||
{
|
||||
if (!int.TryParse(strings[i], out ints[i]))
|
||||
{
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!valid)
|
||||
continue;
|
||||
|
||||
switch (device)
|
||||
{
|
||||
case 32:
|
||||
if (ints.Length == 3 && ints[0] <= _fans.Length)
|
||||
{
|
||||
_fans[ints[0] - 1].Value = ints[1];
|
||||
_controls[ints[0] - 1].Value = (100 / 255.0f) * ints[2];
|
||||
}
|
||||
|
||||
break;
|
||||
case 48:
|
||||
if (ints.Length == 2 && ints[0] <= _temperatures.Length)
|
||||
_temperatures[ints[0] - 1].Value = 0.1f * ints[1];
|
||||
|
||||
break;
|
||||
case 64:
|
||||
if (ints.Length == 3 && ints[0] <= _flows.Length)
|
||||
_flows[ints[0] - 1].Value = 0.1f * ints[1];
|
||||
|
||||
break;
|
||||
case 80:
|
||||
if (ints.Length == 2 && ints[0] <= _relays.Length)
|
||||
_relays[ints[0] - 1].Value = 100 * ints[1];
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (!_available)
|
||||
return;
|
||||
|
||||
while (_serialPort.IsOpen && _serialPort.BytesToRead > 0)
|
||||
{
|
||||
byte b = (byte)_serialPort.ReadByte();
|
||||
if (b == 0x0D)
|
||||
{
|
||||
ProcessUpdateLine(_buffer.ToString());
|
||||
_buffer.Length = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_buffer.Append((char)b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string GetReport()
|
||||
{
|
||||
StringBuilder r = new();
|
||||
r.AppendLine("Heatmaster");
|
||||
r.AppendLine();
|
||||
r.Append("Port: ");
|
||||
r.AppendLine(_portName);
|
||||
r.Append("Hardware Revision: ");
|
||||
r.AppendLine(_hardwareRevision.ToString(CultureInfo.InvariantCulture));
|
||||
r.Append("Firmware Revision: ");
|
||||
r.AppendLine(_firmwareRevision.ToString(CultureInfo.InvariantCulture));
|
||||
r.Append("Firmware CRC: ");
|
||||
r.AppendLine(_firmwareCrc.ToString(CultureInfo.InvariantCulture));
|
||||
r.AppendLine();
|
||||
return r.ToString();
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
_serialPort.Close();
|
||||
_serialPort.Dispose();
|
||||
_serialPort = null;
|
||||
base.Close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
// Copyright (C) LibreHardwareMonitor and Contributors.
|
||||
// Partial Copyright (C) Michael Möller <mmoeller@openhardwaremonitor.org> and Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO.Ports;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace LibreHardwareMonitor.Hardware.Controller.Heatmaster;
|
||||
|
||||
internal class HeatmasterGroup : IGroup
|
||||
{
|
||||
private readonly List<Heatmaster> _hardware = new();
|
||||
private readonly StringBuilder _report = new();
|
||||
|
||||
public HeatmasterGroup(ISettings settings)
|
||||
{
|
||||
// No implementation for Heatmaster on Unix systems
|
||||
if (Software.OperatingSystem.IsUnix)
|
||||
return;
|
||||
|
||||
string[] portNames = GetRegistryPortNames();
|
||||
for (int i = 0; i < portNames.Length; i++)
|
||||
{
|
||||
bool isValid = false;
|
||||
try
|
||||
{
|
||||
using SerialPort serialPort = new(portNames[i], 38400, Parity.None, 8, StopBits.One);
|
||||
serialPort.NewLine = ((char)0x0D).ToString();
|
||||
_report.Append("Port Name: ");
|
||||
_report.AppendLine(portNames[i]);
|
||||
try
|
||||
{
|
||||
serialPort.Open();
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
_report.AppendLine("Exception: Access Denied");
|
||||
}
|
||||
|
||||
if (serialPort.IsOpen)
|
||||
{
|
||||
serialPort.DiscardInBuffer();
|
||||
serialPort.DiscardOutBuffer();
|
||||
serialPort.Write(new byte[] { 0xAA }, 0, 1);
|
||||
|
||||
int j = 0;
|
||||
while (serialPort.BytesToRead == 0 && j < 10)
|
||||
{
|
||||
Thread.Sleep(20);
|
||||
j++;
|
||||
}
|
||||
|
||||
if (serialPort.BytesToRead > 0)
|
||||
{
|
||||
bool flag = false;
|
||||
while (serialPort.BytesToRead > 0 && !flag)
|
||||
{
|
||||
flag |= serialPort.ReadByte() == 0xAA;
|
||||
}
|
||||
|
||||
if (flag)
|
||||
{
|
||||
serialPort.WriteLine("[0:0]RH");
|
||||
try
|
||||
{
|
||||
int k = 0;
|
||||
int revision = 0;
|
||||
while (k < 5)
|
||||
{
|
||||
string line = ReadLine(serialPort, 100);
|
||||
if (line.StartsWith("-[0:0]RH:", StringComparison.Ordinal))
|
||||
{
|
||||
revision = int.Parse(line.Substring(9), CultureInfo.InvariantCulture);
|
||||
break;
|
||||
}
|
||||
|
||||
k++;
|
||||
}
|
||||
|
||||
isValid = revision == 770;
|
||||
if (!isValid)
|
||||
{
|
||||
_report.Append("Status: Wrong Hardware Revision " + revision.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
_report.AppendLine("Status: Timeout Reading Revision");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_report.AppendLine("Status: Wrong Startflag");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_report.AppendLine("Status: No Response");
|
||||
}
|
||||
|
||||
serialPort.DiscardInBuffer();
|
||||
}
|
||||
else
|
||||
{
|
||||
_report.AppendLine("Status: Port not Open");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_report.AppendLine(e.ToString());
|
||||
}
|
||||
|
||||
if (isValid)
|
||||
{
|
||||
_report.AppendLine("Status: OK");
|
||||
_hardware.Add(new Heatmaster(portNames[i], settings));
|
||||
}
|
||||
|
||||
_report.AppendLine();
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<IHardware> Hardware => _hardware;
|
||||
|
||||
public string GetReport()
|
||||
{
|
||||
if (_report.Length > 0)
|
||||
{
|
||||
StringBuilder r = new();
|
||||
r.AppendLine("Serial Port Heatmaster");
|
||||
r.AppendLine();
|
||||
r.Append(_report);
|
||||
r.AppendLine();
|
||||
return r.ToString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
foreach (Heatmaster heatmaster in _hardware)
|
||||
heatmaster.Close();
|
||||
}
|
||||
|
||||
private static string ReadLine(SerialPort port, int timeout)
|
||||
{
|
||||
int i = 0;
|
||||
StringBuilder builder = new();
|
||||
while (i < timeout)
|
||||
{
|
||||
while (port.BytesToRead > 0)
|
||||
{
|
||||
byte b = (byte)port.ReadByte();
|
||||
switch (b)
|
||||
{
|
||||
case 0xAA: return ((char)b).ToString();
|
||||
case 0x0D: return builder.ToString();
|
||||
default:
|
||||
builder.Append((char)b);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
|
||||
throw new TimeoutException();
|
||||
}
|
||||
|
||||
private static string[] GetRegistryPortNames()
|
||||
{
|
||||
List<string> result = new();
|
||||
string[] paths = { string.Empty, "&MI_00" };
|
||||
try
|
||||
{
|
||||
foreach (string path in paths)
|
||||
{
|
||||
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\USB\VID_10C4&PID_EA60" + path);
|
||||
if (key != null)
|
||||
{
|
||||
foreach (string subKeyName in key.GetSubKeyNames())
|
||||
{
|
||||
RegistryKey subKey = key.OpenSubKey(subKeyName + "\\" + "Device Parameters");
|
||||
if (subKey?.GetValue("PortName") is string name && !result.Contains(name))
|
||||
result.Add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SecurityException)
|
||||
{ }
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user