More migration

This commit is contained in:
Benjamin Höglinger-Stelzer 2024-07-13 15:07:34 +02:00
parent c171f2a316
commit 3394e70767
3 changed files with 11 additions and 12 deletions

View File

@ -15,4 +15,5 @@ GetCommModemStatus
GetOverlappedResult
ClearCommError
GetCommProperties
WIN32_ERROR
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]");
}
@ -643,4 +640,4 @@ public partial class SerialPort : IDisposable
ThrowException("Offline");
return false;
}
}
}