Home All Groups Group Topic Archive Search About

Is any device connected to COM1 port

Author
19 Oct 2005 12:56 PM
Abdhul Saleem
Hi,

I need to check whether any device connected to COM1 port?

Pls provide me a code snippet to do the above.

Pls reply ASAP.

Regards,
M. Abdhul Saleem.

Author
19 Oct 2005 3:15 PM
Someone
> I need to check whether any device connected to COM1 port?

If you mean by that you want to test if a serial device is connected and
powered on, you could check DSRHolding property. DSR is Data Set Ready,
which means Modem ready. After opening the port, wait for the modem to
initialize and then check DSRHolding. In the sample below I used a delay
routine. You could change that and use a Timer instead and implement some
sort of timeout. Example:

Dim PortOpenTime As Date

PortOpenTime = Now

In Timer1:

Dim SecondsSincePortWasOpened As Long
SecondsSincePortWasOpened = DateDiff("s", PortOpenTime, Now)


Here is the sample code:


Option Explicit

Private Sub Form_Load()
    Debug.Print IsDeviceConnected(1)
End Sub

'
' IsDeviceConnected
'
' RETURNS
'
'   0   No serial device attached or powered off
'   1   Serial device attached and powered on
'   2   Error, serial port may be in use
'
Private Function IsDeviceConnected(ByVal CommPort As Long) As Long
On Error GoTo ErrorHandler

    MSComm1.CommPort = CommPort
    MSComm1.PortOpen = True
    MSComm1.DTREnable = True
    Delay 1 ' Second for the modem to initialize and respond
    If MSComm1.DSRHolding Then
        ' Serial device attached and powered on
        IsDeviceConnected = 1 ' Yes
    Else
        ' No serial device attached or powered off
        IsDeviceConnected = 0 ' No
    End If

    MSComm1.PortOpen = False

ExitMe:
    On Error GoTo 0
    Exit Function

ErrorHandler:

    IsDeviceConnected = 2 ' Error, serial port may be in use
    'MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume ExitMe
End Function

Private Sub Delay(ByVal Seconds As Single)
    Dim t As Single

    t = Timer
    Do While Timer - t < Seconds
        If t > Timer Then ' Adjust for midnight rollover to 0
            t = t - 86400
        End If
    Loop

End Sub



Show quoteHide quote
"Abdhul Saleem" <AbdhulSal***@discussions.microsoft.com> wrote in message
news:74331DBE-35F4-4408-A0DD-1DD3B0C71219@microsoft.com...
> Hi,
>
> I need to check whether any device connected to COM1 port?
>
> Pls provide me a code snippet to do the above.
>
> Pls reply ASAP.
>
> Regards,
> M. Abdhul Saleem.
>
Are all your drivers up to date? click for free checkup

Author
19 Oct 2005 5:34 PM
Someone
Here is an updated routine that doesn't always wait 1 second for each port:

Option Explicit

Private Sub Form_Load()
    Debug.Print IsDeviceConnected(1, 0.5) ' Wait up to 0.5 Seconds
End Sub

'
' IsDeviceConnected
'
' INPUT
'
'   CommPort    CommPort to check. The port must not be already open
'   TimeOut     Time out in seconds to wait for the serial device to come
online
'
' RETURNS
'
'   0   Timed out. No serial device attached or powered off
'   1   Serial device attached and powered on
'   2   Error, serial port may be in use
'
Private Function IsDeviceConnected(ByVal CommPort As Long, ByVal TimeOut As
Single) As Long
On Error GoTo ErrorHandler
    Dim t As Single

    MSComm1.CommPort = CommPort
    MSComm1.PortOpen = True
    MSComm1.DTREnable = True

    t = Timer
    Do While Timer - t < TimeOut
        If t > Timer Then ' Adjust for midnight rollover to 0
            t = t - 86400
        End If
        If MSComm1.DSRHolding Then
            ' Serial device attached and powered on
            IsDeviceConnected = 1 ' Yes
            MSComm1.PortOpen = False
            Exit Function
        End If
    Loop

    ' Timed out. No serial device attached or powered off
    IsDeviceConnected = 0 ' No

ExitMe:
    On Error GoTo 0
    Exit Function
ErrorHandler:

    IsDeviceConnected = 2 ' Error, serial port may be in use
    'MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume ExitMe
End Function




Show quoteHide quote
"Someone" <nob***@cox.net> wrote in message
news:%23LrLc$L1FHA.2132@TK2MSFTNGP15.phx.gbl...
>> I need to check whether any device connected to COM1 port?
>
> If you mean by that you want to test if a serial device is connected and
> powered on, you could check DSRHolding property. DSR is Data Set Ready,
> which means Modem ready. After opening the port, wait for the modem to
> initialize and then check DSRHolding. In the sample below I used a delay
> routine. You could change that and use a Timer instead and implement some
> sort of timeout. Example:
>
> Dim PortOpenTime As Date
>
> PortOpenTime = Now
>
> In Timer1:
>
> Dim SecondsSincePortWasOpened As Long
> SecondsSincePortWasOpened = DateDiff("s", PortOpenTime, Now)
>
>
> Here is the sample code:
>
>
> Option Explicit
>
> Private Sub Form_Load()
>    Debug.Print IsDeviceConnected(1)
> End Sub
>
> '
> ' IsDeviceConnected
> '
> ' RETURNS
> '
> '   0   No serial device attached or powered off
> '   1   Serial device attached and powered on
> '   2   Error, serial port may be in use
> '
> Private Function IsDeviceConnected(ByVal CommPort As Long) As Long
> On Error GoTo ErrorHandler
>
>    MSComm1.CommPort = CommPort
>    MSComm1.PortOpen = True
>    MSComm1.DTREnable = True
>    Delay 1 ' Second for the modem to initialize and respond
>    If MSComm1.DSRHolding Then
>        ' Serial device attached and powered on
>        IsDeviceConnected = 1 ' Yes
>    Else
>        ' No serial device attached or powered off
>        IsDeviceConnected = 0 ' No
>    End If
>
>    MSComm1.PortOpen = False
>
> ExitMe:
>    On Error GoTo 0
>    Exit Function
>
> ErrorHandler:
>
>    IsDeviceConnected = 2 ' Error, serial port may be in use
>    'MsgBox "Error " & Err.Number & ": " & Err.Description
>    Resume ExitMe
> End Function
>
> Private Sub Delay(ByVal Seconds As Single)
>    Dim t As Single
>
>    t = Timer
>    Do While Timer - t < Seconds
>        If t > Timer Then ' Adjust for midnight rollover to 0
>            t = t - 86400
>        End If
>    Loop
>
> End Sub
>
>
>
> "Abdhul Saleem" <AbdhulSal***@discussions.microsoft.com> wrote in message
> news:74331DBE-35F4-4408-A0DD-1DD3B0C71219@microsoft.com...
>> Hi,
>>
>> I need to check whether any device connected to COM1 port?
>>
>> Pls provide me a code snippet to do the above.
>>
>> Pls reply ASAP.
>>
>> Regards,
>> M. Abdhul Saleem.
>>
>
>
Author
21 Oct 2005 12:46 PM
Abdhul Saleem
Hi,

The DSRHolding property works fine and got the solution.
Thanks for your timely advice.

Regards,
M. Abdhul Saleem
Author
19 Oct 2005 4:29 PM
Dick Grier
Hi,

There is no way to know if there is a device connected to a serial port...
Unless that device is installed such that Windows is aware of it (for
example, a printer or modem; and only if actually installed).  If simply
connected, there is no way to determine for certain.  There are ways that
may or may not work, but nothing reliable.  If the connected device is an
installed device, the WMI APIs (recent versions of Windows) will provide
information.

The problems is that no two devices work the same way.  Some will do
something with the handshaking lines (DSR and CTS), others will not.  You
can examine the DSR and CTS line status (DSRHolding and CTSHolding if using
MSComm), and if either of these are True, there "probably" is a device
connected.  However, this is not 100% because some devices do not use these
handshaking lines at all, and others may be programmed to NOT assert these
lines, even if they do support them, when powered up.

Dick

--
Richard Grier  (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2  (391 pages) published July 2004.  See
www.mabry.com/vbpgser4 to order.
Author
19 Oct 2005 5:17 PM
Someone
> There is no way to know if there is a device connected to a serial port...
> Unless that device is installed such that Windows is aware of it (for
> example, a printer or modem; and only if actually installed).

This might be true for devices that emulate the serial port, but not for
external serial devices. For external serial devices, you don't need to
install the drivers for the external device in order to tell if it's powered
on or not, unless it's not using DSR pin or keeps it off for some reason. I
agree with you about the second part of your post that it may not be
possible to detect all devices, but the OP was not clear about which type of
device, he or she could tell easily with few tests.


Show quoteHide quote
"Dick Grier" <dick_grierNOSPAM@msn.com> wrote in message
news:egdiKpM1FHA.664@tk2msftngp13.phx.gbl...
> Hi,
>
> There is no way to know if there is a device connected to a serial port...
> Unless that device is installed such that Windows is aware of it (for
> example, a printer or modem; and only if actually installed).  If simply
> connected, there is no way to determine for certain.  There are ways that
> may or may not work, but nothing reliable.  If the connected device is an
> installed device, the WMI APIs (recent versions of Windows) will provide
> information.
>
> The problems is that no two devices work the same way.  Some will do
> something with the handshaking lines (DSR and CTS), others will not.  You
> can examine the DSR and CTS line status (DSRHolding and CTSHolding if
> using MSComm), and if either of these are True, there "probably" is a
> device connected.  However, this is not 100% because some devices do not
> use these handshaking lines at all, and others may be programmed to NOT
> assert these lines, even if they do support them, when powered up.
>
> Dick
>
> --
> Richard Grier  (Microsoft Visual Basic MVP)
>
> See www.hardandsoftware.net for contact information.
>
> Author of Visual Basic Programmer's Guide to Serial Communications, 4th
> Edition ISBN 1-890422-28-2  (391 pages) published July 2004.  See
> www.mabry.com/vbpgser4 to order.
>
>
Author
20 Oct 2005 5:15 PM
Dick Grier
Hi,

>>
For external serial devices, you don't need to
install the drivers for the external device in order to tell if it's powered
on or not, unless it's not using DSR pin or keeps it off for some reason.
<<

This scenario is VERY common.  DSR is asserted ONLY if the device
manufacturer decides to do so, and isn't if not.  Three wire serial devices
are a dime a dozen.  The only device that you can be "fairly" sure will
assert DSR is a modem -- and even then you will see a percentage that do
not.  And... even modems that do assert DSR provide a way to disable this
feature (and to save that as a default startup condition).  So, assuming
that DSR will be present is sure to fail sooner or later.  If you "know"
that you device asserts DSR, then you are fine.  But... I covered that.

Dick

--
Richard Grier  (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2  (391 pages) published July 2004.  See
www.mabry.com/vbpgser4 to order.

Bookmark and Share