01-11-2002, 02:55 PM
i'm currently writing a program to access information through a serial port. i have a menu option to select which com port to use, but it's giving me problems. I'd like to know if there's any way to check if a com port either exists or is in use, so that i can just display an error message instead of having the computer lock up like it does when i try to access a port in use. i'm using the mscomm controls.
01-14-2002, 07:20 PM
VB generates an error code when it tries to open a serial port already in use. You can use this code to determine if the port is opened.
Try the following code:
Function OpenSerialPort() As Integer
On Error GoTo SerialPortOpenErrorHandler
'----Open Communication Port----
Form1.MSComm1.PortOpen = False
OpenSerialPort = 0
If Err.Number = 8005 Then
'Serial Port Already Opened!
MsgBox "Serial Port is already opened by another application:" & vbCrLf & "Please Close Serial Port - " & intPort, vbOKOnly + vbCritical, "Error"
OpenSerialPort = -1
01-14-2002, 07:21 PM
I saw one error in the code I sent:
Change the following line from:
Form1.MSComm1.PortOpen = False
Form1.MSComm1.PortOpen = True
01-18-2002, 10:25 AM
i tried this code and it failed to catch the error. When i select com1 and try to open it, the mouse locks up for a second, then moves again, but then the whole program stays locked up and i have to kill it. i know that this computer has the serial port set to use com2, and nothing's using com1 that i know of, but since this program will be running on multiple computers, i'd like it to catch any port problems on any computer.
01-18-2002, 01:47 PM
Try something like this...
Private Function IsCommPortAvailable(lCom as Long) As Boolean
Dim lOldPort As Long
Dim lOldOpen As Boolean
lOldPort = MSComm1.CommPort
lOldOpen = MSComm1.PortOpen
On Error Resume Next
If lOldOpen Then MSComm1.PortOpen = False
MSComm1.CommPort = lCom
MSComm1.PortOpen = True
If Err Then
IsCommPortAvailable = False
IsCommPortAvailable = True
On Error Goto 0
If MSComm1.PortOpen Then MSComm1.PortOpen = False
MSComm1.CommPort = lOldPort
MSComm1.PortOpen = lOldOpen
Hope this helps...
01-18-2002, 03:03 PM
That solution didn't work either.
Just for more info, on this computer, the dialup modem uses com1, and the device i'm communicating with through a serial port (rs-232) uses com2. Now, in my program i need to be able to choose the serial port because some computers use com1 as the serial port, so i need to give an option for that to the user. However, from looking at it, when VB tries to open the com1 on this computer, no error is generated, because none of the error code is doing anything, and the program still works. any other suggestions?
01-19-2002, 02:47 AM
Go To : http://www.vbweb.co.uk/show/21/ for an article, "
Com Ports and the MSComm Control - Introduction
by James Crowley" which in a series of articles, specifically addresses this topic.
01-21-2002, 09:27 AM
ya, i tried that one too..... but when i test it, no com ports show up in the list. it says every one of them is unavailable, which isn't true, because com2 works fine.
any other possibilities, or maybe a reason the thing on vbweb.co.uk didn't work?