-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialPortActionEventArgs.cs
More file actions
123 lines (108 loc) · 4.44 KB
/
Copy pathSerialPortActionEventArgs.cs
File metadata and controls
123 lines (108 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System.Text.RegularExpressions;
namespace WinSerialMon;
public sealed class SerialPortActionEventArgs : EventArgs
{
private static readonly Regex ComPortPattern =
new(@"\b(COM\d{1,3})\b", RegexOptions.IgnoreCase | RegexOptions.Compiled);
public SerialPortActionEventArgs(
SerialPortAction action,
SerialIrpEvent irpEvent,
int? requestedLength,
int? completedLength,
byte[]? readBuffer,
byte[]? writeBuffer,
byte[]? ioctlInputBuffer,
byte[]? ioctlOutputBuffer)
{
Action = action;
IrpEvent = irpEvent ?? throw new ArgumentNullException(nameof(irpEvent));
RequestedLength = requestedLength;
CompletedLength = completedLength;
ReadBuffer = readBuffer;
WriteBuffer = writeBuffer;
IoctlInputBuffer = ioctlInputBuffer;
IoctlOutputBuffer = ioctlOutputBuffer;
}
public SerialPortAction Action { get; }
public SerialIrpEvent IrpEvent { get; }
public string? DevicePath => IrpEvent.DevicePath;
public string? ComPortName
{
get
{
var path = IrpEvent.DevicePath;
if (string.IsNullOrWhiteSpace(path)) return null;
var match = ComPortPattern.Match(path);
return match.Success ? match.Value.ToUpperInvariant() : null;
}
}
public int ProcessId => IrpEvent.ProcessId;
public int ThreadId => IrpEvent.ThreadId;
public ulong? IoControlCode => IrpEvent.IoControlCode;
public uint? NtStatus => IrpEvent.NtStatus;
public SerialIoctlCode KnownIoctlCode
{
get
{
if (IrpEvent.IoControlCode is not { } code) return SerialIoctlCode.Unknown;
return Enum.IsDefined(typeof(SerialIoctlCode), code)
? (SerialIoctlCode)code
: SerialIoctlCode.Unknown;
}
}
public int? RequestedLength { get; }
public int? CompletedLength { get; }
public byte[]? ReadBuffer { get; }
public byte[]? WriteBuffer { get; }
public byte[]? IoctlInputBuffer { get; }
public byte[]? IoctlOutputBuffer { get; }
// ── Decoded IOCTL parameters ──────────────────────────────────────────────
/// <summary>New baud rate from SetBaudRate or SetCommConfig IOCTL.</summary>
public uint? NewBaudRate
{
get
{
if (KnownIoctlCode == SerialIoctlCode.SetBaudRate
&& IoctlInputBuffer is { Length: >= 4 } buf)
{
return BitConverter.ToUInt32(buf, 0);
}
return NewCommConfig?.BaudRate;
}
}
/// <summary>Decoded data bits, stop bits, parity from SetLineControl or SetCommConfig IOCTL.</summary>
public SerialLineControlSettings? NewLineControl
{
get
{
if (KnownIoctlCode == SerialIoctlCode.SetLineControl)
return SerialLineControlSettings.TryDecode(IoctlInputBuffer);
if (NewCommConfig is { } cfg)
return new SerialLineControlSettings(cfg.DataBits, cfg.StopBits, cfg.Parity);
return null;
}
}
/// <summary>Decoded handshake/flow-control settings from SetHandFlow IOCTL.</summary>
public SerialHandFlowSettings? NewHandFlow =>
KnownIoctlCode == SerialIoctlCode.SetHandFlow
? SerialHandFlowSettings.TryDecode(IoctlInputBuffer)
: null;
/// <summary>Decoded read/write timeout settings from SetTimeouts or GetTimeouts IOCTL.</summary>
public SerialTimeoutSettings? NewTimeouts =>
KnownIoctlCode switch
{
SerialIoctlCode.SetTimeouts => SerialTimeoutSettings.TryDecode(IoctlInputBuffer),
SerialIoctlCode.GetTimeouts => SerialTimeoutSettings.TryDecode(IoctlOutputBuffer),
_ => null
};
/// <summary>Decoded modem status register bits from GetModemStatus IOCTL.</summary>
public SerialModemStatus? ModemStatus =>
KnownIoctlCode == SerialIoctlCode.GetModemStatus
? SerialModemStatus.TryDecode(IoctlOutputBuffer)
: null;
/// <summary>Fully decoded COMMCONFIG/DCB from SetCommConfig IOCTL (baud, data bits, stop bits, parity, flow control).</summary>
public SerialCommConfigSettings? NewCommConfig =>
KnownIoctlCode == SerialIoctlCode.SetCommConfig
? SerialCommConfigSettings.TryDecode(IoctlInputBuffer)
: null;
}