-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a class for obtaining data about connected devices
- Loading branch information
Showing
2 changed files
with
527 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO.Ports; | ||
using System.Linq; | ||
using HSMonitor.Models; | ||
using HSMonitor.Services; | ||
|
||
namespace HSMonitor.Utils.Usb.Serial; | ||
|
||
public class Serial : IDisposable | ||
{ | ||
private readonly SerialPort _serialPort; | ||
private readonly SettingsService _settingsService; | ||
|
||
public Serial(SettingsService settingsService) | ||
{ | ||
_settingsService = settingsService; | ||
if (_settingsService is {Settings: not null}) | ||
_serialPort = new SerialPort( | ||
string.IsNullOrEmpty(_settingsService.Settings.LastSelectedPort) | ||
? "COM1" | ||
: _settingsService.Settings.LastSelectedPort, | ||
_settingsService.Settings.LastSelectedBaudRate, | ||
Parity.None, | ||
8, | ||
StopBits.One); | ||
else | ||
_serialPort = new SerialPort(); | ||
} | ||
|
||
public static IEnumerable<DeviceInfo> GetPorts() | ||
{ | ||
var devices = Win32DeviceMgmt | ||
.GetAllCOMPorts(); | ||
|
||
devices | ||
.Where(d => d.BusDescription?.Contains("HSMonitor") == true) | ||
.ToList() | ||
.ForEach(d => d.IsHsMonitorDevice = true); | ||
|
||
return devices; | ||
} | ||
|
||
public bool CheckAccess() | ||
{ | ||
try | ||
{ | ||
return Open(); | ||
} | ||
catch | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
private bool Open() | ||
{ | ||
if (_serialPort.IsOpen) | ||
return true; | ||
|
||
try | ||
{ | ||
_serialPort.PortName = _settingsService.Settings.LastSelectedPort ?? "COM1"; | ||
_serialPort.Open(); | ||
} | ||
catch | ||
{ | ||
_serialPort.Close(); | ||
throw; | ||
} | ||
|
||
return _serialPort.IsOpen; | ||
} | ||
|
||
public void Close() | ||
{ | ||
if (!_serialPort.IsOpen) return; | ||
_serialPort.Close(); | ||
} | ||
|
||
public void Write(byte[] data) | ||
{ | ||
if (!_serialPort.IsOpen) return; | ||
try | ||
{ | ||
_serialPort.Write(data, 0, data.Length); | ||
} | ||
catch | ||
{ | ||
_serialPort.Close(); | ||
throw; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_serialPort.Close(); | ||
_serialPort.Dispose(); | ||
} | ||
|
||
} |
Oops, something went wrong.