Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Error 8002: Invalid port number

Hi Guys,

I get 'Error 8002: Invalid port number' error when trying to use MSCOMM. When i debugged, the CommPort property is set to '1' which is a common port number. When i press F5, the program continues itrs execution as normal.:(
Please explain me this illogical behaviour.

Regards,
Swapna
[338 byte] By [swapnaoe] at [2007-11-11 10:06:22]
# 1 Re: Error 8002: Invalid port number
first check that there is any commnd that change .portnumber , and there is any other device currently using this port ...
you may send here where u have a problem exactly and we will try to help u in it.
Amahdy at 2007-11-11 17:23:16 >
# 2 Re: Error 8002: Invalid port number
Call GetInstalledCOMPorts(port_array)
For i = 0 To UBound(port_array) - 1
If Me.DSPComm.PortOpen = True Then
Me.DSPComm.PortOpen = False
End If
Me.DSPComm.Settings = "9600,n,8,1"
Me.DSPComm.RThreshold = 1
Me.DSPComm.CommPort = Val(Mid$(port_array(i), 4, Len(port_array(i)) -3))
Me.DSPComm.PortOpen = True
Me.DSPComm.InBufferCount = 0
Me.DSPComm.Output = Chr$("&H" & "41")

data_received = Initial_Receive_Data
If Hex$(Asc(data_received)) = "41" Then
serial_port = Val(Mid$(port_array(i), 4, Len(port_array(i)) - 3))
exit_flag = True
Exit For
ElseIf data_received = "Not Responding" Then
End If
Next

'-----------------------When i click the debug button on the error window, the line it shows is ' Me.DSPComm.PortOpen = True '. And when i click either F5 or F8, the execution proceeds further successfully. This is the strange thing i have noticed. I have tried hardcoding the port number with 1 as this is the only working COM port on my PC. Strange thing is this program was working fine until today morning. I have not changed any part of the code.. :(
swapnaoe at 2007-11-11 17:24:16 >
# 3 Re: Error 8002: Invalid port number
u may try : GetFirstAvailableCOMPort() and COMCheckPort() here :
http://vbnet.mvps.org/index.html?code/system/comtestmscomm.htm

I think your problem is here : Val(Mid$(port_array(i), 4, Len(port_array(i)) -3))
as u haven't told me what u use exactly for GetInstalledCOMPorts() but u may check around this , put a break point in this line and look what is the return value and so on ... u r lucky to get this error now however it works later but who know maybe it wont work in other pcs .
Amahdy at 2007-11-11 17:25:14 >
# 4 Re: Error 8002: Invalid port number
another useful example from this forum , he explain all I think :
http://forums.dev-archive.com/showpost.php?p=445843&postcount=3
Amahdy at 2007-11-11 17:26:21 >
# 5 Re: Error 8002: Invalid port number
Call GetInstalledCOMPorts(port_array)
For i = 0 To UBound(port_array) - 1
If Me.DSPComm.PortOpen = True Then
Me.DSPComm.PortOpen = False
End If
Me.DSPComm.Settings = "9600,n,8,1"
Me.DSPComm.RThreshold = 1
Me.DSPComm.CommPort = Val(Mid$(port_array(i), 4, Len(port_array(i)) -3))
'----------------------
The method GetInstalledCOMPorts(port_array) returns the available COM ports in a string array 'port_array'.The format is COM1,COM2, COM3 etc.

This method Val(Mid$(port_array(i), 4, Len(port_array(i)) -3)) takes the port number from the string. eg 1 from COM1.

The GetInstalledCOMPorts() method internally makes use of COMCheckPort() method.
swapnaoe at 2007-11-11 17:27:20 >
# 6 Re: Error 8002: Invalid port number
Hi
The problem is solved.Morning i had installed a software that would share a single COM port between many a applications by creating virtual ports. Thats causing the problem.. :).. I have uninstalled it and it works fine now..
Thank u so much for ya time

Regards,
swapna
swapnaoe at 2007-11-11 17:28:18 >
# 7 Re: Error 8002: Invalid port number
Great , only one hint for u if u don't know it , the third argument in mid$() is optional ,
to get the rest of the string from a starting point u don't need to put the third arg ..

eg : Mid(port_array(i), 4) 'return 1 as the rest of the string "com1" starting from 4 is "1"
but if the array return any thing like com1xxx , you must use your method , that's why if u use the "GetFirstAvailableCOMPort()" u will get the number directly and I think it's already in your "GetInstalledCOMPorts()" function and this last one add the string "com" at the beginning [I mean don't add it then remove it again ] .
Amahdy at 2007-11-11 17:29:22 >
# 8 Re: Error 8002: Invalid port number
Thank u.. :)
My application actually displays, the available COM ports so that user can select the required port. Thats y, have prefixed COM to every available port Number.
swapnaoe at 2007-11-11 17:30:22 >