diff --git a/CollabVMAgent/VirtIOSerial.cs b/CollabVMAgent/VirtIOSerial.cs index 162ed63..85025a5 100644 --- a/CollabVMAgent/VirtIOSerial.cs +++ b/CollabVMAgent/VirtIOSerial.cs @@ -54,6 +54,20 @@ namespace CollabVMAgent if (readCancel.IsCancellationRequested) return; Thread.Sleep(READ_INTERVAL); 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); #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( ref Guid ClassGuid, IntPtr Enumerator, @@ -174,7 +188,7 @@ namespace CollabVMAgent [DllImport("kernel32.dll", SetLastError = true)] 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); [DllImport("kernel32.dll", SetLastError = true)]