V
V
Vladislav2015-09-24 12:02:16
C++ / C#
Vladislav, 2015-09-24 12:02:16

How to close SerialPort if device is disabled?

An IOException is thrown when trying to send any data to a disconnected device. Further, with cport.close(), another IOException is triggered (The device does not recognize the command). Using Port.Dispose() throws the same exception.

/*Грубо вырезанный кусок кода*/
...
SerialPort cport;
...
            try
            {
                ...
                cport.Write(a, 0, a.Length);
            }
            catch (Exception) 
            {
                cport.close();
            }

How to close SerialPort and continue program execution further?
Z.Y. In other words, I need to handle device disconnection. SerialPinChange.Break cannot be used due to some peculiarities.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2015-09-24
@gloomkolomna

put cport.close() in the finally block;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question