Migrated to CsWin32 #1

Merged
nefarius merged 14 commits from nefarius/bugfix/refactoring into master 2024-07-13 15:32:14 +02:00
3 changed files with 11 additions and 12 deletions
Showing only changes of commit 3394e70767 - Show all commits

View File

@ -16,3 +16,4 @@ GetOverlappedResult
ClearCommError
GetCommProperties
WIN32_ERROR
GetHandleInformation

View File

@ -35,5 +35,6 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="61.0.15-preview" />
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
</Project>

View File

@ -257,11 +257,10 @@ public partial class SerialPort : IDisposable
CheckResult();
_writeCount = toSend.GetLength(0);
fixed (byte* ptr = toSend)
fixed (NativeOverlapped* ptrOl = &_ptrUwo)
{
uint sent;
if (PInvoke.WriteFile(_hPort, ptr, (uint)_writeCount, &sent, ptrOl))
if (PInvoke.WriteFile(_hPort, toSend.AsSpan(), &sent, ptrOl))
{
_writeCount -= (int)sent;
}
@ -460,17 +459,16 @@ public partial class SerialPort : IDisposable
private unsafe void ReceiveThread()
{
byte* buf = stackalloc byte[1];
byte[] buffer = new byte[1];
AutoResetEvent sg = new(false);
NativeOverlapped ov = new() { EventHandle = sg.SafeWaitHandle.DangerousGetHandle() };
COMM_EVENT_MASK eventMask = 0;
try
{
while (true)
{
COMM_EVENT_MASK eventMask = 0;
if (!PInvoke.SetCommMask(_hPort,
COMM_EVENT_MASK.EV_RXCHAR | COMM_EVENT_MASK.EV_TXEMPTY | COMM_EVENT_MASK.EV_CTS |
COMM_EVENT_MASK.EV_DSR
@ -540,7 +538,7 @@ public partial class SerialPort : IDisposable
uint gotBytes;
do
{
if (!PInvoke.ReadFile(_hPort, buf, 1, &gotBytes, &ov))
if (!PInvoke.ReadFile(_hPort, buffer, &gotBytes, &ov))
{
if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_IO_PENDING)
{
@ -555,7 +553,7 @@ public partial class SerialPort : IDisposable
if (gotBytes == 1)
{
OnRxChar(buf[0]);
OnRxChar(buffer[0]);
}
} while (gotBytes > 0);
}
@ -593,8 +591,7 @@ public partial class SerialPort : IDisposable
if (i != 0)
{
uint f;
if (!Win32Com.GetCommModemStatus(_hPort.DangerousGetHandle(), out f))
if (!Win32Com.GetCommModemStatus(_hPort.DangerousGetHandle(), out uint f))
{
throw new CommPortException("IO Error [005]");
}