Re: Is any device connected to COM1 port
- From: "Someone" <nobody@xxxxxxx>
- Date: Wed, 19 Oct 2005 13:34:03 -0400
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
"Someone" <nobody@xxxxxxx> wrote in message
news:%23LrLc$L1FHA.2132@xxxxxxxxxxxxxxxxxxxxxxx
>> 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" <AbdhulSaleem@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:74331DBE-35F4-4408-A0DD-1DD3B0C71219@xxxxxxxxxxxxxxxx
>> 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.
>>
>
>
.
- References:
- Re: Is any device connected to COM1 port
- From: Someone
- Re: Is any device connected to COM1 port
- Prev by Date: Re: how to split numeric part from letters in string
- Next by Date: Re: Shrink Wrap
- Previous by thread: Re: Is any device connected to COM1 port
- Next by thread: Re: Is any device connected to COM1 port
- Index(es):
Relevant Pages
|