Detecting When Your USB Pacemaker Is Plugged In and Out

This is where we can share code to do things with our Pacemakers
Post Reply
Sox
Admin Sox
Admin Sox
Posts: 347
Joined: Fri Sep 14, 2012 9:25 pm
Location: Bath, England
Contact:

Detecting When Your USB Pacemaker Is Plugged In and Out

Post by Sox » Sat Sep 29, 2012 7:10 pm

To autodetect Pacemaker insertion and removal you need to hook your form into the Windows messaging to detect the ones appropriate for USB device changes, then check if you can find the Pacemaker drive. This is a code snipped for VB .Net

Code: Select all

Imports Microsoft.Win32

Private WM_DEVICECHANGE As Integer = &H219

Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
        DBT_CONFIGCHANGECANCELED = &H19
        DBT_CONFIGCHANGED = &H18
        DBT_CUSTOMEVENT = &H8006
        DBT_DEVICEARRIVAL = &H8000
        DBT_DEVICEQUERYREMOVE = &H8001
        DBT_DEVICEQUERYREMOVEFAILED = &H8002
        DBT_DEVICEREMOVECOMPLETE = &H8004
        DBT_DEVICEREMOVEPENDING = &H8003
        DBT_DEVICETYPESPECIFIC = &H8005
        DBT_DEVNODES_CHANGED = &H7
        DBT_QUERYCHANGECONFIG = &H17
        DBT_USERDEFINED = &HFFFF
End Enum

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_DEVICECHANGE Then
            Select Case m.WParam
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
                    'A USB device arrived
                    If MyPMDUtils.PacemakerInfo.PacemakerDrive = "" Then
                        'Pacemaker was not previously connected, so let's check if it is now
                        MyPMDUtils.FindPacemakerDrive()
                        if (MyPMDUtils.PacemakerInfo.PacemakerPresent) then
                             'Pacemaker has just been plugged in!
                        end if
                    End If
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
                    'A USB device was removed
                    If MyPMDUtils.PacemakerInfo.PacemakerDrive <> "" Then
                        'Pacemaker was previously connected, so let's check if it still is
                        MyPMDUtils.CheckPacemakerDriveStillPresent()
                        if (not MyPMDUtils.PacemakerInfo.PacemakerPresent) then
                             'Pacemaker has just been unplugged!
                        end if
                    End If
            End Select
        End If
        MyBase.WndProc(m)
End Sub
Never experiment with drugs.... you might waste them

fazz
Admin Fazz
Admin Fazz
Posts: 24
Joined: Wed Sep 19, 2012 6:06 am

Re: Detecting When Your USB Pacemaker Is Plugged In and Out

Post by fazz » Wed Oct 17, 2012 1:24 am

I'm a little confused. Does this mean you don't have to eject it properly?

EDIT: Just realised I'm in the developer forum. Ignore me.

Post Reply