This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSerialPort.cs
233 lines (209 loc) · 8.33 KB
/
SerialPort.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System.Runtime.InteropServices;
using System.Text;
using static Vanara.PInvoke.SetupAPI;
using static Vanara.PInvoke.AdvApi32;
using System.Security.AccessControl;
using Vanara.InteropServices;
using Vanara.PInvoke;
using Vanara.Extensions;
namespace PS5CodeReader
{
internal class SerialPort : System.IO.Ports.SerialPort
{
private readonly static int _buadRate = 115200;
internal SerialPort(string portName)
{
PortName = portName;
BaudRate = _buadRate;
}
private static IEnumerable<int> BaudRates => new[]
{
268435450,
921600,
460800,
256000,
230400,
153600,
128000,
115200,
57600,
56000,
38400,
28800,
19200,
14400,
9600,
4800,
2400,
1200,
600,
300,
110
};
internal new void Open()
{
base.Open();
return;
///Playstation 5 only supports 115200;
foreach (var b in BaudRates)
{
BaudRate = b;
try
{
base.Open();
}
catch (ArgumentOutOfRangeException ex) when (ex != null && ex.ParamName != null && ex.ParamName.ToLowerInvariant().Contains("baudrate"))
{
var value = new string(ex.Message.Where(char.IsDigit).ToArray());
if (int.TryParse(value, out var i))
{
BaudRate = i;
try
{
base.Open();
}
catch
{
continue;
}
}
}
catch (IOException ex) when (ex.Message.ToLowerInvariant().Contains("the parameter is incorrect"))
{
if (IsOpen)
Close();
continue;
break;
continue;
}
if (IsOpen)
break;
}
}
internal static IEnumerable<Device> SelectSerial(bool isSorted = true, Func<Device, bool>? filter = null)
{
var guid = GetGuidFromClassName(@"Ports");
var autoDevice = new Device(@"Auto", @"Detect Device Automatically (Auto)");
var deviceList = new List<Device>();
deviceList.AddRange(GetDeviceByGuid(guid, filter));
if (isSorted)
{
deviceList = deviceList.OrderBy(x => x.Port.Length).ThenBy(x => x.Port).ToList();
}
deviceList.Insert(0, autoDevice);
return deviceList;
}
private static IEnumerable<Device> GetDeviceByGuid(Guid guid, Func<Device, bool>? filter = null)
{
var hDevInfo = SetupDiGetClassDevs(guid, Flags: DIGCF.DIGCF_PRESENT);
if (hDevInfo == IntPtr.Zero)
{
throw new Exception(@"Failed to get device information set for the Modem ports");
}
try
{
var devices = new List<Device>();
SetupDiEnumDeviceInfo(hDevInfo).ToList()?.ForEach(hDevInfoData =>
{
var name = GetDeviceName(hDevInfo, hDevInfoData);
if (string.IsNullOrEmpty(name)) return;
var description = GetDeviceDescription(hDevInfo, hDevInfoData);
var friendlyName = GetDeviceFriendlyName(hDevInfo, hDevInfoData);
var instancePath = GetDeviceInstanceId(hDevInfo, hDevInfoData);
devices.Add(new Device(name, friendlyName));
});
return devices;
}
finally
{
SetupDiDestroyDeviceInfoList(hDevInfo);
}
}
internal static Guid GetGuidFromClassName(string name)
{
var guidArray = Array.Empty<Guid>();
var flag = SetupDiClassGuidsFromName(name, guidArray, 0, out var requiredSize);
if (!flag && requiredSize <= 0) return guidArray.FirstOrDefault();
Array.Resize(ref guidArray, (int)requiredSize);
if (guidArray.Length < 1) return guidArray.FirstOrDefault();
SetupDiClassGuidsFromName(name, guidArray, (uint)guidArray.Length, out _);
return guidArray.FirstOrDefault();
}
private static string? GetDeviceName(SafeHDEVINFO hDevInfo, SP_DEVINFO_DATA hDevInfoData)
{
const string name = @"PortName";
var ptrRegistryKey = SetupDiOpenDevRegKey(hDevInfo, hDevInfoData, DICS_FLAG.DICS_FLAG_GLOBAL, 0u, DIREG.DIREG_DEV, RegistryRights.QueryValues);
if (ptrRegistryKey.IsInvalid)
return null;
try
{
var size = 0u;
RegQueryValueEx(ptrRegistryKey, name, nint.Zero, out _, nint.Zero, ref size);
using var mem = new SafeHGlobalHandle(size);
var flag = RegQueryValueEx(ptrRegistryKey, name, nint.Zero, out _, mem, ref size);
return flag == Win32Error.ERROR_SUCCESS ? mem.ToString(-1, CharSet.Auto) : null;
}
catch
{
return null;
}
finally
{
RegCloseKey(ptrRegistryKey);
}
}
private static string? GetDeviceDescription(SafeHDEVINFO hDevInfo, SP_DEVINFO_DATA hDevInfoData)
{
SetupDiGetDeviceRegistryProperty(hDevInfo, hDevInfoData, SPDRP.SPDRP_DEVICEDESC, out _, nint.Zero, 0u, out var size);
using var mem = new SafeHGlobalHandle(size);
var flag = SetupDiGetDeviceRegistryProperty(hDevInfo, hDevInfoData, SPDRP.SPDRP_DEVICEDESC, out _, mem, mem.Size, out _);
return flag ? mem.ToString(-1, CharSet.Auto) : string.Empty;
}
private static string? GetDeviceFriendlyName(SafeHDEVINFO ptr, SP_DEVINFO_DATA ptrDevInfo)
{
SetupDiGetDeviceRegistryProperty(ptr, ptrDevInfo, SPDRP.SPDRP_FRIENDLYNAME, out _, nint.Zero, 0u, out var size);
using var mem = new SafeHGlobalHandle(size);
var flag = SetupDiGetDeviceRegistryProperty(ptr, ptrDevInfo, SPDRP.SPDRP_FRIENDLYNAME, out _, mem, mem.Size, out _);
return flag ? mem.ToString(-1, CharSet.Auto) : string.Empty;
}
private static string GetDeviceInstanceId(SafeHDEVINFO ptr, SP_DEVINFO_DATA ptrDevInfo)
{
var sb = new StringBuilder();
SetupDiGetDeviceInstanceId(ptr, ptrDevInfo, sb, 0u, out var size);
sb = new StringBuilder((int)size);
var flag = SetupDiGetDeviceInstanceId(ptr, ptrDevInfo, sb, size, out _);
return flag ? sb.ToString() : string.Empty;
}
internal static byte CalculateChecksum(string data)
{
var checksum = 0;
checksum = Encoding.ASCII.GetBytes(data).Sum(x => x);
return (byte)((checksum + 256) % 256);
}
internal new void Write(string command)
{
var checkSum = CalculateChecksum(command);
var commandBytes = Encoding.ASCII.GetBytes($"{command}:{checkSum:X2}\r\n");
Write(commandBytes, 0 , commandBytes.Length);
}
internal Task WriteLineAsync(string command, CancellationToken cancellationToken = default)
{
var checkSum = CalculateChecksum(command);
return SerialPortExtensions.WriteLineAsync(this, $"{command}:{checkSum:X2}", cancellationToken);;
}
internal void SendBreak(double timeout = 0.25)
{
if (!IsOpen) return;
BreakState = true;
Thread.Sleep(TimeSpan.FromMilliseconds(timeout));
BreakState = false;
}
internal async Task SendBreakAsync(double timeout = 0.25, CancellationToken cancellationToken = default)
{
if (!IsOpen) return;
BreakState = true;
await Task.Delay(TimeSpan.FromMicroseconds(timeout), cancellationToken);
BreakState = false;
}
}
}