Re: Is any device connected to COM1 port

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.
>>
>
>


.



Relevant Pages

  • Re: COMM control refresh
    ... line which was supposed to close the port so it can be opened again. ... > ' CommPort CommPort to check. ... No serial device attached or powered off ... > Private Sub Timer1_Timer ...
    (microsoft.public.vb.general.discussion)
  • Re: Is any device connected to COM1 port
    ... > I need to check whether any device connected to COM1 port? ... ' 0 No serial device attached or powered off ... On Error GoTo ErrorHandler ... Private Sub Delay ...
    (microsoft.public.vb.general.discussion)
  • Re: Serial Port Timer
    ... Private Sub ListeningSerialPort_DataReceived(ByVal sender As System.Object, ... ocp.read is on a timer and reads char by char. ... ' Write an user specified Command to the Port. ... Catch exc As Exception ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Data sticks around on serial port between program runs
    ... >data from the previous run (The serial device sends its data all at ... >the port in my program and in a cleanup function when the program ... >exits, before closing the port. ... Typically tcflush() would do the trick, ...
    (comp.unix.programmer)
  • Re: Serial Port Timer
    ... I did not use a timer. ... Private Sub ListeningSerialPort_DataReceived(ByVal sender As System.Object, ... ocp.read is on a timer and reads char by char. ... ' Write an user specified Command to the Port. ...
    (microsoft.public.dotnet.languages.vb)