Exception: Safe handle has been closed while communicating with the SerialPort.
This exception occurs during the communication with the SerialPort. Let’s explain with the example. There is one Device connected with the system over SerialPort which is non-permanent SerialPort (something like a device connected with the system using USB Port). When the port “goes away” unexpectedly the underlying Stream associated with it gets disposed. If there is an active read or write operation on the port at the time a subsequent call to SerialPort.Close() can lead to the exception “Safe handle has been closed”.
How to resolved “Safe handle has been closed” Exception?
1. Make sure the SerialPort is open while performing Read/ Write operation with the SerialPort.
2. Close the SerialPort after reading/ Writing Operation.
3. Implement try, catch and finally block for the Serial Communication and always Close the SerialPort before exiting from the Application.
4. Implement Application Level exception handling and display a proper message to the user. You can suggest user to disconnect the device and again connect.
Hope this will help.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?