5

I'm starting to work with a pinpad. I need that my program find the port where the pinpad is connected without user interaction.

I tried with:

Dim searcher As New ManagementObjectSearcher("root\cimv2","SELECT * FROM Win32_SerialPort")

        For Each queryObj As ManagementObject In searcher.Get()
          MsgBox(queryObj("Name"))
        Next

but this only give me "COM1" and "COM2" as an answer (My device is connected to COM4)

and with

Dim searcher As New ManagementObjectSearcher("root\cimv2", "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0")

        For Each queryObj As ManagementObject In searcher.Get()
            MsgBox(queryObj("Name"))
        Next

With this one I can see my device friendly name but I don´t know how to get the port (I receive the names like 'HP printer')

Any idea of how can I get the port that I need?

Thanks in advance

DenLun
  • 194
  • 2
  • 16

2 Answers2

3

Based on the comments it sounds like your device is a USB device that has a driver that causes it to appear to be (emulates) a serial port attached device. In that case I would use:

My.Computer.Ports.SerialPortNames

to enumerate and loop over all serial ports. Then, one at a time try to open each one and send a command to the device that you know it responds to. Most devices have some kind of heartbeat or keep alive message that they will respond to. Whichever port you get a response on is the port you need to use.

Shane Wealti
  • 2,252
  • 3
  • 19
  • 33
  • I try that solution but the thing is that there is going to be a lot of pinpads in several places and I don't know if there is going to be an issue when the machine have, for example, 10 different devices connected at the same time. – DenLun Dec 19 '13 at 17:08
  • One machine will have more than one pinpad connected to it? If so, is there some kind of device identifier that you can query for over the serial interface? – Shane Wealti Dec 20 '13 at 18:29
  • There is not going to be more than one pinpad at the same time so I use your solution and it worked. Thanks! I was afraid to create something like an eternal loop or something weird by sending a message port by port but it is working fine. – DenLun Jan 07 '14 at 16:52
0

I want to point 2 matters:

1: here is a solution i used for this problem (efficiancy corrections will be appreciated) I used this soution i used to figure out on which port vx805 verifone pin pad was connected (has a unique device id):

Friend Class pinPadComLocater

Private Shared com As String
Private Const PNPDeviceID = "VID_11CA&PID_0220"
Private Const scope = "root\cimv2"
Public ReadOnly pinPadCom As String = Nothing


Sub New()
    If isVX805PinPadConnected() Then
        pinPadCom = com
        Output.mainLog(Output.pinpadLocationMsg + com)
    Else
        Output.mainLog(Output.pinpadNotFoundMsg)
    End If

End Sub

Private Shared Function queryCom(port As String) As Boolean
    Dim query = "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=""{4d36e978-e325-11ce-bfc1-08002be10318}"" AND DeviceID LIKE  ""%" + PNPDeviceID + "%"" AND Caption LIKE ""%" + port + "%"""
    Dim resp = New ManagementObjectSearcher(scope, query).Get
    If resp.Count = 1 Then Return True  
    For Each queryObj As ManagementObject In resp
        For Each prop In queryObj.Properties 'print all data for development purposes
            Try
                Console.writeline(prop.Name + " : " + queryObj(prop.Name).ToString)
                catch ex As Exception
            End Try
        Next
    Next
    Return False
End Function
Private Shared Function isVX805PinPadConnected() As Boolean
    For Each port In My.Computer.Ports.SerialPortNames
        Try
            If queryCom(port) Then
                com = port
                Return True
            End If
        Catch err As ManagementException
            Throw New ConstraintException("An error occurred while querying for WMI data: " & err.Message)
        End Try
    Next
    Throw New ConstraintException("Pin Pad Com Port could not be located")
    Return False
End Function

End Class

2: would love more clarifiaction on that:

Then, one at a time try to open each one and send a command to the device that you know it responds to. Most devices have some kind of heartbeat or keep alive message that they will respond to

I would love to see a code example of how you send such a heartbeat check to a pinpad

Tal Aruety
  • 23
  • 5