Results 1 to 8 of 8

Thread: How to detect when a device/cable is plugged or unplugged when using MSCOMM control.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    9

    Unhappy How to detect when a device/cable is plugged or unplugged when using MSCOMM control.

    Hi,

    I am using VB6 and writing a small application which uses MSComm control. I want to open the com port and communicate to the device. But i want to get notified or detect when the cable is disconnected in between before i close the com port. Similarly, when the cable is connected and communication is restored, i want to get notified. How can i do this.

    I am giving the code which i m using right now. But this is not detecting the error when the cable is disconnected.

    Code:
    Private Sub Command1_Click()
    Dim CommSpeed As String
    Dim CommFormat As String
    Dim ErrorNumber As Integer
    
    Set m_MSComm = New MSComm
    CommSpeed = "9600"
    CommFormat = ",N,8,1"
    On Error GoTo Errtrap
    With m_MSComm
        .CommPort = 1
        .Settings = CommSpeed + CommFormat
        .RThreshold = 1
        .InputLen = 0
        .PortOpen = True
    End With
    
    If m_MSComm.PortOpen = True Then
        MsgBox "comm port opened successfully"
    End If
    
    Exit Sub
    Errtrap:
      ErrorNumber = Err.Number
      If Err.Number = 8002 Then
      MsgBox "Com port is disconnected"
      End If
    
    End Sub
    
    Private Sub Command2_Click()
    
    If m_MSComm.PortOpen = True Then
         m_MSComm.PortOpen = False
    End If
    
    If Err Then
    MsgBox Error$, 48
    Else
    MsgBox "Comm port closed successfully"
    End If
    
    End Sub
    Can you please help me how to detect when some disconnection/ connection happens using this com port. May be just to show a message box when this happens is enough.

    Thanks in advance,
    Satyanvesh.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to detect when a device/cable is plugged or unplugged when using MSCOMM contr

    Well you can not detect a cable being plugged in or unplugged unless it is a special cable that has CD or some other signal tied high which is not normally the case.

    If there is a device attached to the cable then you can usually detect it via the CD or DSR signal both of which are supported by MSComm control.

    note the cases in the following example from the online help
    Code:
    Private Sub MSComm_OnComm ()
       Select Case MSComm1.CommEvent
       ' Handle each event or error by placing 
       ' code below each case statement
    
       ' Errors
          Case comEventBreak   ' A Break was received.
          Case comEventFrame   ' Framing Error
          Case comEventOverrun   ' Data Lost.
          Case comEventRxOver   ' Receive buffer overflow.
          Case comEventRxParity   ' Parity Error.
          Case comEventTxFull   ' Transmit buffer full.
          Case comEventDCB   ' Unexpected error retrieving DCB]
    
       ' Events
          Case comEvCD   ' Change in the CD line.
          Case comEvCTS   ' Change in the CTS line.
          Case comEvDSR   ' Change in the DSR line.
          Case comEvRing   ' Change in the Ring Indicator.
          Case comEvReceive   ' Received RThreshold # of 
                            ' chars.
          Case comEvSend   ' There are SThreshold number of 
                         ' characters in the transmit
                         ' buffer.
          Case comEvEof   ' An EOF charater was found in 
                         ' the input stream
       End Select
    End Sub
    I generally use CD to detect whether the device is connected and turned on or not

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    9

    Re: How to detect when a device/cable is plugged or unplugged when using MSCOMM contr

    @DataMiser: Thank you for your reply. I have tried with that comEvCD and i was able to get an event using that. But for both disconnection as well as connection of the cable, comEvCD event is thrown. Is there anyway to differentiate exactly its a connection or disconnection happened?

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to detect when a device/cable is plugged or unplugged when using MSCOMM contr

    Yes when that event happens you would check the status of the cdholding
    Code:
    If MSComm1.CDHolding then DeviceIsConnected Else DeviceIsNotConnected
    This should work assuming you have the right type of cable and/or null modem adapter. Some adapters and some cables tie the CD high which results in the CD signal always being present even when no device is connected to the other end of the cable.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How to detect when a device/cable is plugged or unplugged when using MSCOMM contr

    Actually true/on/1 is a low (negative) voltage, high (positive) voltages are false/off/0.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    9

    Resolved Re: How to detect when a device/cable is plugged or unplugged when using MSCOMM contr

    Quote Originally Posted by DataMiser View Post
    Yes when that event happens you would check the status of the cdholding
    Code:
    If MSComm1.CDHolding then DeviceIsConnected Else DeviceIsNotConnected
    This should work assuming you have the right type of cable and/or null modem adapter. Some adapters and some cables tie the CD high which results in the CD signal always being present even when no device is connected to the other end of the cable.
    Thank you very much @DataMiser. CDHolding worked for me. After so much of search i got this answer.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    9

    Re: How to detect when a device/cable is plugged or unplugged when using MSCOMM contr

    @dilettante: Thank you for your information.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2014
    Posts
    9

    Re: How to detect when a device/cable is plugged or unplugged when using MSCOMM contr

    I have a similar problem with USB as well. I need to notify when a USB cable gets disconnected/connected. I have tried to use RegisterForUSBEventNotification using the VendorID and ProductID, but i m not able to detect it properly. Here is what my code in VB6 looks like.

    Code:
    Private Declare Function RegisterForUSBEventNotification Lib "USBNotify" (ByVal v_id As Long, ByVal p_id As Long, ByVal pFun As Long) As Long
    Private Declare Function UnregisterForUSBEventNotification Lib "USBNotify" (ByVal stopDll As Long) As Long
    
    Private Sub Class_Initialize()
    {
        Dim USBVID As Long
        Dim USBPID As Long
        Dim X as Integer
    
     X = Registry.GetValue(RG_SERVICEINFO_FORMS, "USBVID", RegVal)
            USBVID = Val("&H" & RegVal)
            X = Registry.GetValue(RG_SERVICEINFO_FORMS, "USBPID", RegVal)
            USBPID = Val("&H" & RegVal)
             If USBPID <> 0 & USBVID <> 0 Then
                 X = RegisterForUSBEventNotification(USBVID, USBPID, AddressOf USBDisconnectCallback)
             End If
    
    }
    
    Private Sub Class_Terminate()
    {
      UnregisterForUSBEventNotification 1
    }
    
    Private Sub USBDisconnectCallback(ByVal usbChange As Long)
        
        If usbChange <> 1 Then
            MsgBox "Disconnected"
        Else
            MsgBox "Connected"
        End If
        
    End Sub
    I am not able to enter into the USBDisconnectCallback function and show the message box that its disconnected/connected.

    Can somebody please help me?

    Thanks,
    Satyanvesh.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width