SerialPort.open() - error message: COM does not exist
Code:
#Region "Manager Variables"
'property variables
Private _baudRate As String = String.Empty
Private _parity As String = String.Empty
Private _stopBits As String = String.Empty
Private _dataBits As String = String.Empty
Private _portName As String = String.Empty
Private _transType As TransmissionType
Private _msg As String
Private _type As MessageType
'global manager variables
Private comPort As New SerialPort()
Private write As Boolean = True
#End Region
Code:
Public Function OpenPort() As Boolean
Try
'first check if the port is already open
'if its open then close it
If comPort.IsOpen = True Then
comPort.Close()
End If
'set the properties of our SerialPort Object
comPort.BaudRate = Integer.Parse(_baudRate)
'BaudRate
comPort.DataBits = Integer.Parse(_dataBits)
'DataBits
comPort.StopBits = DirectCast([Enum].Parse(GetType(StopBits), _stopBits), StopBits)
'StopBits
comPort.Parity = DirectCast([Enum].Parse(GetType(Parity), _parity), Parity)
'Parity
comPort.PortName = _portName
'PortName
'now open the port
comPort.Open()
'display message
_type = MessageType.Normal
_msg = "Port opened at " + DateTime.Now + "" + Environment.NewLine + ""
MsgBox(_type, _msg)
'return true
Return True
Catch ex As Exception
MsgBox(MessageType.[Error], ex.Message)
Return False
End Try
End Function
I have a catch ex As Exception at comport.Open() with error message saying "COM18 does not exist". I checked with serialport.getportnames() that it is listed. It is also listed on device manager.
The port settings I checked were correct:
comPort.BaudRate = 9600
comPort.DataBits = 8
comPort.StopBits = 1
comPort.Parity = None {0}
comPort.PortName = "COM18"
Am I missing something?
Thanks