I have a very simple vb.net app part of which opens a comm port and then reads/writes etc..
That all works well except if the device is not plugged into the pc and all that appears in the com ports list are virtual ports.. I then get an argument error.. "The given port name does not start with COM/com or does not resolve to a valid serial port.Parameter name: portName"
Code is summarised below :-
Routines-------->
<DllImport("kernel32.dll", SetlastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetDefaultCommConfig(ByVal lpszName As String, ByRef lpCC As COMMCONFIG, ByRef lpdwSize As Integer) As Boolean
End Function
Public Shared Function IsPortAvailable(ByVal portNumber As Int32) As Boolean
If portNumber <= 0 Then
Return False
Else
Dim cfg As COMMCONFIG
Dim cfgsize As Int32 = Marshal.SizeOf(cfg)
cfg.dwSize = cfgsize
Dim ret As Boolean = GetDefaultCommConfig("COM" + portNumber.ToString, cfg, cfgsize)
Return ret
End If
End Function
<---------
Application----------->
dim CommPortAvailable as boolean = IsPortAvailable(pnumber)
If CommPortAvailable = False Then
...........COM Port Error
Else
..........COM Port Found
End If
........The above routine detects the virtual ports ok..
Try
With comPort
.PortName = "COM27" -------( one of the virtual ports listed from SerialPort.GetPortNames )
.....................etc etc
End With
If comPort.IsOpen Then
comPort.Close()
End If
comPort.Open()
............ This is where the error occurs
I have seen lots of pages on google mentioning this but have not found any simple solutions like how to determine if the port is virtual/physical, get the physical from virtual etc..
Thanks a lot
That all works well except if the device is not plugged into the pc and all that appears in the com ports list are virtual ports.. I then get an argument error.. "The given port name does not start with COM/com or does not resolve to a valid serial port.Parameter name: portName"
Code is summarised below :-
Routines-------->
<DllImport("kernel32.dll", SetlastError:=True, CharSet:=CharSet.Auto)> Private Shared Function GetDefaultCommConfig(ByVal lpszName As String, ByRef lpCC As COMMCONFIG, ByRef lpdwSize As Integer) As Boolean
End Function
Public Shared Function IsPortAvailable(ByVal portNumber As Int32) As Boolean
If portNumber <= 0 Then
Return False
Else
Dim cfg As COMMCONFIG
Dim cfgsize As Int32 = Marshal.SizeOf(cfg)
cfg.dwSize = cfgsize
Dim ret As Boolean = GetDefaultCommConfig("COM" + portNumber.ToString, cfg, cfgsize)
Return ret
End If
End Function
<---------
Application----------->
dim CommPortAvailable as boolean = IsPortAvailable(pnumber)
If CommPortAvailable = False Then
...........COM Port Error
Else
..........COM Port Found
End If
........The above routine detects the virtual ports ok..
Try
With comPort
.PortName = "COM27" -------( one of the virtual ports listed from SerialPort.GetPortNames )
.....................etc etc
End With
If comPort.IsOpen Then
comPort.Close()
End If
comPort.Open()
............ This is where the error occurs
I have seen lots of pages on google mentioning this but have not found any simple solutions like how to determine if the port is virtual/physical, get the physical from virtual etc..
Thanks a lot