add some error checking, get a new handle if the old one is invalid

This commit is contained in:
Elijah R 2023-12-04 12:22:23 -05:00
parent f91090a7b0
commit e48ce2f9cf

View file

@ -54,6 +54,20 @@ namespace CollabVMAgent
if (readCancel.IsCancellationRequested) return; if (readCancel.IsCancellationRequested) return;
Thread.Sleep(READ_INTERVAL); Thread.Sleep(READ_INTERVAL);
ReadFile(viohnd, sizebuf, 4, out read, IntPtr.Zero); ReadFile(viohnd, sizebuf, 4, out read, IntPtr.Zero);
var err = Marshal.GetLastWin32Error();
if (err != 0)
{
#if DEBUG
Console.WriteLine($"Got error {err} while trying to read from serial port");
#endif
switch (err)
{
case 6:
CloseHandle(viohnd);
viohnd = CreateFile(DevicePath, FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
break;
}
}
} }
uint size = BitConverter.ToUInt32(sizebuf, 0); uint size = BitConverter.ToUInt32(sizebuf, 0);
#if DEBUG #if DEBUG
@ -131,7 +145,7 @@ namespace CollabVMAgent
} }
[DllImport("setupapi.dll", CharSet = CharSet.Auto)] [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SetupDiGetClassDevs( static extern IntPtr SetupDiGetClassDevs(
ref Guid ClassGuid, ref Guid ClassGuid,
IntPtr Enumerator, IntPtr Enumerator,
@ -174,7 +188,7 @@ namespace CollabVMAgent
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped); static extern bool ReadFile(IntPtr hFile, [Out] byte[] lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll")] [DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, IntPtr lpOverlapped); static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]