I delete and create the corresponding closed device variable and try to connect this device via the COM port again using the Open() function when the device itself is turned off, respectively (attempts are made many times until the device is connected).
And I get an error with this trace:
in System.Runtime.InteropServices.SafeHandle.DangerousAddRef (Boolean & success)
in System.StubHelpers.StubHelpers.SafeHandleAddRef (SafeHandle pHandle, Boolean & success)
at Microsoft.Win32.UnsafeNativeMethods.GetOverlappedResult (SafeFileHandle hFile, NativeOverlapped * lpOverlapped, Int32 & lpNumberOfBytesTransferred, Boolean bWait)
in System.IO.Ports.SerialStream.EventLoopRunner.WaitForCommEvent ()
in System.Threading.ThreadHelper.ThreadStart_Context (Object state)
in System.Threading.ExecutionContext.RunInternal (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state)
in System.Threading.ThreadHelper.ThreadStart ()
The fact is that I have already tried to run it and run it using Task.Factory.StartNew(() => ... and nothing happens anyway.
The exception crashes as systematically every other time.
Here, the port name is specified from the program's default settings, the COM port itself does not close, but the variable that works with it is re-created, the device itself is not connected to the port and every time (and there are many) the Open () function does not work, but the program is all flies equally, directly reaching only the Open () function. I could not send the library of work with the device here:
private void billAcceptorConnect() { try { billAcceptor = new CashCodeSm(); billAcceptor.PortName = Properties.Settings.Default.ComPort; billAcceptor.Open(); billAcceptor.IsAcceptanceEnabled = false; billAcceptor.BillsOnEscrow += billAcceptor_BillsOnEscrow; billAcceptor.BillsStacked += billAcceptor_BillsStacked; billAcceptor.CashBoxRemoved += billAcceptor_CashBoxRemoved; billAcceptor.BillsRejected += billAcceptor_BillsRejected; billAcceptor.BillsReturned += billAcceptor_BillsReturned; billAcceptor.CashBoxAttached += billAcceptor_CashBoxAttached; billAcceptor.CashBoxIsAlmostFull += billAcceptor_CashBoxIsAlmostFull; billAcceptor.CashBoxIsFull += billAcceptor_CashBoxIsFull; billAcceptor.Error += billAceptor_Error; } catch { billAcceptor = null; } }