Error when disconnecting serial port

ejleiss

Active member
Joined
Oct 1, 2012
Messages
37
Programming Experience
1-3
Hello,

I have a program that continuously reads in streaming serial port data and plots this data on a chart. I am getting an error and the program is crashing when I try to disconnect the serial port. I believe it is because there is still data in the receive buffer when I try to disconnect. I tried to use the following to disconnect my port, but it still gives the error in my receive sub routine. Is there another way to cleanly disconnect and close the serial port?

SerialPort1.DiscardInBuffer()SerialPort1.Close()
SerialPort1.Dispose()

Thank you.
 
A crash is simply an unhandled exception. The first order of business is to examine the exception thrown. It will give you a lot of useful information on what actually when wrong. Start by examining that. You can catch it explicitly in code but the IDE will give you all the same information on an unhandled exception thrown while debugging.
 
Back
Top