Migrated more types

This commit is contained in:
Benjamin Höglinger-Stelzer 2022-10-03 19:20:01 +02:00
parent e48d103c63
commit 6f9ec4d2a4
2 changed files with 7 additions and 9 deletions

View File

@ -43,7 +43,7 @@ public class SerialPort : IDisposable
if (_hPort.IsInvalid) if (_hPort.IsInvalid)
{ {
if (Marshal.GetLastWin32Error() == Win32Com.ERROR_ACCESS_DENIED) return false; if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_ACCESS_DENIED) return false;
throw new CommPortException("Port Open Failure"); throw new CommPortException("Port Open Failure");
} }
@ -210,7 +210,8 @@ public class SerialPort : IDisposable
} }
else else
{ {
if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) ThrowException("Unexpected failure"); if (Marshal.GetLastWin32Error() != (int)WIN32_ERROR.ERROR_IO_PENDING)
ThrowException("Unexpected failure");
} }
} }
} }
@ -263,7 +264,7 @@ public class SerialPort : IDisposable
} }
else else
{ {
if (Marshal.GetLastWin32Error() != Win32Com.ERROR_IO_PENDING) ThrowException("Unexpected failure"); if (Marshal.GetLastWin32Error() != (int)WIN32_ERROR.ERROR_IO_PENDING) ThrowException("Unexpected failure");
} }
} }
@ -406,7 +407,7 @@ public class SerialPort : IDisposable
Marshal.WriteInt32(uMask, 0); Marshal.WriteInt32(uMask, 0);
if (!Win32Com.WaitCommEvent(_hPort.DangerousGetHandle(), uMask, unmanagedOv)) if (!Win32Com.WaitCommEvent(_hPort.DangerousGetHandle(), uMask, unmanagedOv))
{ {
if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_IO_PENDING)
sg.WaitOne(); sg.WaitOne();
else else
throw new CommPortException("IO Error [002]"); throw new CommPortException("IO Error [002]");
@ -425,7 +426,7 @@ public class SerialPort : IDisposable
if (((uint)errs & Win32Com.CE_RXOVER) != 0) s = s.Append("Receive Overflow,"); if (((uint)errs & Win32Com.CE_RXOVER) != 0) s = s.Append("Receive Overflow,");
if (((uint)errs & Win32Com.CE_RXPARITY) != 0) s = s.Append("Parity,"); if (((uint)errs & Win32Com.CE_RXPARITY) != 0) s = s.Append("Parity,");
if (((uint)errs & Win32Com.CE_TXFULL) != 0) s = s.Append("Transmit Overflow,"); if (((uint)errs & Win32Com.CE_TXFULL) != 0) s = s.Append("Transmit Overflow,");
s.Length = s.Length - 1; s.Length -= 1;
throw new CommPortException(s.ToString()); throw new CommPortException(s.ToString());
} }
@ -439,7 +440,7 @@ public class SerialPort : IDisposable
{ {
if (!Win32Com.ReadFile(_hPort.DangerousGetHandle(), buf, 1, out gotbytes, unmanagedOv)) if (!Win32Com.ReadFile(_hPort.DangerousGetHandle(), buf, 1, out gotbytes, unmanagedOv))
{ {
if (Marshal.GetLastWin32Error() == Win32Com.ERROR_IO_PENDING) if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_IO_PENDING)
{ {
Win32Com.CancelIo(_hPort.DangerousGetHandle()); Win32Com.CancelIo(_hPort.DangerousGetHandle());
gotbytes = 0; gotbytes = 0;

View File

@ -5,9 +5,6 @@ namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{ {
internal class Win32Com internal class Win32Com
{ {
//Constants for errors:
internal const UInt32 ERROR_ACCESS_DENIED = 5;
internal const UInt32 ERROR_IO_PENDING = 997;
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
internal static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags); internal static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags);