Sub CloseComPort() Using myComPort If (Not (myComPort Is Nothing)) Then ' The COM port exists. If myComPort.IsOpen Then ' Wait for the transmit buffer to empty. Do While (myComPort.BytesToWrite > 0) Loop End If End If End Using End Sub Listing 3. The End Using statement closes the port and disposes of its resources.