Home > Archive > Visual Basic > April 2004 > API To check if Serial port 4 is available
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
API To check if Serial port 4 is available
|
|
| mashraf 2004-04-20, 3:30 pm |
| Hi,
Does any one know any API I can use to check if Com Port 1 or Com port 4 is available or being used by anothere application. I do not want to use Comm Ctrl OCX.
Thanks,
Mashraf
| |
| Mathias Pihl 2004-04-20, 4:30 pm |
| Try this:
Private Declare Function ConfigurePort _
Lib "winspool.drv" Alias "ConfigurePortA" _
(ByVal pName As String, ByVal hwnd As Long, _
ByVal pPortName As String) As Long
MsgBox ConfigurePort("", Me.hwnd, "COM4")
If the MSGBOX returns 0 there is an error (=not active)
HTH
-------------
It's a long way to the top (if you wanna be a developer)
-Mathias->
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
| |
| Randy Birch 2004-04-20, 7:31 pm |
| But ... if that port exists on the machine the configure port dialog will
appear before the function returns. And if the dialog appears and the user
presses OK, it returns 1, otherwise it returns 0.
MsgBox ConfigurePort(vbNullString, Me.hwnd, "COM2:")
MsgBox ConfigurePort(vbNullString, Me.hwnd, "COM4:")
If the port is not present 0 is returned immediately, but this API may not
be the best choice if the user expects COM4 to exist and merely wants to
check if it's valid.
--
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Mathias Pihl" <mathiaspihl@hotmail.com> wrote in message
news:e1Fh0wwJEHA.952@TK2MSFTNGP12.phx.gbl...
: Try this:
:
: Private Declare Function ConfigurePort _
: Lib "winspool.drv" Alias "ConfigurePortA" _
: (ByVal pName As String, ByVal hwnd As Long, _
: ByVal pPortName As String) As Long
:
: MsgBox ConfigurePort("", Me.hwnd, "COM4")
:
: If the MSGBOX returns 0 there is an error (=not active)
:
: HTH
:
: -------------
: It's a long way to the top (if you wanna be a developer)
:
: -Mathias->
:
: *** Sent via Developersdex http://www.developersdex.com ***
: Don't just participate in USENET...get rewarded for it!
| |
| Randy Birch 2004-04-20, 7:31 pm |
| This will return the available ports on a given system ...
http://vbnet.mvps.org/code/enums/enumports.htm
But, as far as I know the only way to tell if a specific port is in use
(once you've established the port exists on the machine) is to attempt to
open it and if it returns an error, it is in use. This requires using, at a
minimum, the mscomm control.
I believe the same can be achieved using either QueryDosDevice or CreateFile
API calls ... Richard Grier - the COMM expert in these parts - may have
precise information this or be able to provide a better alternative that
will work across all Windows versions (QueryDosDevice is for Win98 and
later) .
--
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"mashraf" <anonymous@discussions.microsoft.com> wrote in message
news:D31352DE-8309-49D1-B3FA-3E9C30C2DCD8@microsoft.com...
: Hi,
: Does any one know any API I can use to check if Com Port 1 or Com port 4
is available or being used by anothere application. I do not want to use
Comm Ctrl OCX.
:
: Thanks,
: Mashraf
:
| |
| Dick Grier 2004-04-21, 2:33 pm |
| Hi,
Why don't you want to use the comm control? This is easiest. The
application NEVER needs to actually display any forms or to be visible in
any other way.
However, certainly, you can use various APIs. If you attempt to open the
port using CreateFile, an error will be returned if the port is in use or
not available (remember to use CloseHandle to close the port if the
CreateFile call returns a valid handle). There are other possible APIs,
too. However, MSComm or CreateFile are straight forward.
Dick
--
Richard Grier (Microsoft Visual Basic MVP)
See www.hardandsoftware.net for contact information.
Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
| |
| Thomas Lutz 2004-04-28, 10:36 am |
| The most reliable way to determine what COM ports are available would
be to actually attempt to open each port in a loop. If you can open
the port then it is available and you can immediately close it.
You can even test if the port exists but is being used by another
application using this approach.
The easiest way to do this would be to us the MSComm.OCX however you
can also use the Windows API.
There are some keys in the registry that you can look for in the
HKLM\Hardware\DeviceMap\SerialComm section
however this is not completely reliable because many newer add on COM
ports that connect to a USB port do not list themselves in this
section of the registry.
Here is some simple code that may do the job:
Type DCB
DCBlength As Long
BaudRate As Long
fBitFields As Long
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer
End Type
Type COMMCONFIG
dwSize As Long
wVersion As Integer
wReserved As Integer
dcbx As DCB
dwProviderSubType As Long
dwProviderOffset As Long
dwProviderSize As Long
wcProviderData As Byte
End Type
'
Declare Function GetDefaultCommConfig Lib "kernel32" _
Alias "GetDefaultCommConfigA" (ByVal lpszName As String, _
lpCC As COMMCONFIG, lpdwSize As Long) As Long
'
Public Function PortFoundInSystem(port As Integer) As Long
' call from a loop and test for COM1 to COM32
'this function returns non-zero value if the port exists
Dim cc As COMMCONFIG, ccsize As Long
'
ccsize = LenB(cc) 'gets the size of COMMCONFIG structure
'
PortFoundInSystem = GetDefaultCommConfig("COM" + Trim(Str(port)) +
Chr(0), cc, ccsize)
'
End Function
Here is a simple function to actually open the port to test if if is
available:
Function OpenPort(TPort As Long) As Long
Dim ComPort As String
ComPort = "COM" & Format$(TPort)
If TPort > 9 Then ComPort = "\\.\" + ComPort
ComId = CreateFile(ComPort, GENERIC_READ Or GENERIC_WRITE, 0, ByVal
vbNullString, OPEN_EXISTING, 0, ByVal vbNullString)
'Debug.Print TPort, ComId
If ComId < 0 Then
Select Case ComId
Case IE_MEMORY ' "Unable to allocate queues. Try reducing buffer
size."
OpenPort = -2
Case IE_HARDWARE ' "Hardware Not Present"
OpenPort = -1
Case Else ' "Device " + ComPort + " Not Available."
OpenPort = -3
End Select
Exit Function
End If
success = CloseHandle(ComId)
OpenPort = 1 ' port found and it is available! - yippeee
End Function
For more serial I/O tips, tricks and free utilities, visit
www.taltech.com
On Tue, 20 Apr 2004 11:21:08 -0700, "mashraf"
<anonymous@discussions.microsoft.com> wrote:
>Hi,
>Does any one know any API I can use to check if Com Port 1 or Com port 4 is available or being used by anothere application. I do not want to use Comm Ctrl OCX.
>
>Thanks,
>Mashraf
|
|
|
|
|