Files
ReLibreHardwareMonitor/LibreHardwareMonitorLib/Hardware/Gpu/PowerMonitor/SharedSerialPort.cs
T
BlacktempelandPhyxionNL df9e0a5131 Add support for Thermal Grizzly WireView Pro 2. (#2171)
* Add support for Thermal Grizzly WireView Pro 2.

* Process PR comments.

* PR comments.

* Add private keyword.

* PR comments.

* Replace WMI with SetupAPI.

Some first updates according to Thermal Grizzly code.

* Add report.

* Further improve implementation according to code of Thermal Grizzly (thanks !).

* No connection check for welcome message.

* Rename class + change SerialPort to SharedSerialPort for guard - fixing mutex issues.

* PR comments.

* Remove SP_DEVINFO_DATA from NativeMethods.txt

* Update WireViewPro2.cs

* Rename hasMutex to _hasMutex for consistency

* Formatting

* Fix build

---------

Co-authored-by: PhyxionNL <7643972+PhyxionNL@users.noreply.github.com>
2026-01-27 13:20:07 +01:00

89 lines
1.9 KiB
C#

using System.IO.Ports;
namespace LibreHardwareMonitor.Hardware.Gpu.PowerMonitor;
internal sealed class SharedSerialPort : SerialPort
{
private bool _hasMutex;
public SharedSerialPort()
{ }
public SharedSerialPort(string portName) : base(portName)
{ }
public SharedSerialPort(string portName, int baudRate) : base(portName, baudRate)
{ }
public SharedSerialPort(string portName, int baudRate, Parity parity) : base(portName, baudRate, parity)
{ }
public SharedSerialPort(string portName, int baudRate, Parity parity, int dataBits)
: base(portName, baudRate, parity, dataBits)
{ }
public SharedSerialPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
: base(portName, baudRate, parity, dataBits, stopBits)
{ }
private int MutexTimeout { get; set; } = 500;
public new void Open()
{
_hasMutex = Mutexes.WaitUsbSensors(MutexTimeout);
if (_hasMutex)
{
base.Open();
}
}
public new void Close()
{
if (_hasMutex)
{
if (IsOpen)
{
BaseStream.Flush();
BaseStream.Close();
}
_hasMutex = false;
Mutexes.ReleaseUsbSensors();
}
}
public new void Write(byte[] buffer, int offset, int count)
{
if (_hasMutex)
{
base.Write(buffer, offset, count);
}
}
public new int Read(byte[] buffer, int offset, int count)
{
if (_hasMutex)
{
return base.Read(buffer, offset, count);
}
return 0;
}
public new void DiscardInBuffer()
{
if (_hasMutex)
{
base.DiscardInBuffer();
}
}
public new void DiscardOutBuffer()
{
if (_hasMutex)
{
base.DiscardOutBuffer();
}
}
}