private void GPSTestApp_FormClosing(object sender, FormClosingEventArgs e) { //close the ports before closing the app //unsubscribe from event handlers controlPort.SerialDataReadyEvent -= controlPort_SerialDataReadyEvent; testPort.SerialDataReadyEvent -= testPort_SerialDataReadyEvent; //check if controlPort is closed if (controlPort.CloseSerialPort()) { if (controlPort.PortName != "") { tbControlDebugOut.AppendText(controlPort.PortName + " is closed\n"); } } else { tbControlDebugOut.AppendText("Error: " + controlPort.ReadSerialData() + "\n"); } //testPort if (testPort.CloseSerialPort()) { if (testPort.PortName != "") { tbTestDebugOut.AppendText(testPort.PortName + " is closed\n"); } } else { tbTestDebugOut.AppendText("Error: " + testPort.ReadSerialData() + "\n"); } }