dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] Reading string from USB device

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2015
    Posts
    42

    Resolved [RESOLVED] Reading string from USB device

    I am trying to read a character string from a USB connected RFID Reader.
    Currently, I have the form Keypreview set to true, then this code:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Tag_ID = Tag_ID & Chr(KeyCode)
         If Len(Tag_ID) <> 10 Then
            Exit Sub
        End If
    My question(s):
    1) Is there a better way?
    2) What about strings longer, (or shorter), than 10 characters?

    If I use Notepad to read the strings, I seem to get a crlf at the end, but I cannot figure out how to detect this, rather than just counting the characters.

    Any ideas will be warmly received.

    Thanks, in advance.

    Jim

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,706

    Re: Reading string from USB device

    Quote Originally Posted by JimOldguy View Post
    I am trying to read a character string from a USB connected RFID Reader.
    Devices that pretend to be a keyboard are keyboards.

    The only way to do better is when the device can be configured not to pretend it is a keyboard and it supports some other kind of access.


    I doubt it sends a CRLF because that isn't normal keyboard behavior. Remember, this is keystroke emulation not normal data transfer. When the "enter" key arrives a Windows application should trigger the default control, since that's what "enter" means in Windows. It is basically a "submit" or "go" key.

    To suppress this you'd avoid marking any control as the default control.


    A device using such a rudimentary way of communicating doesn't leave you with many options. You almost have to rely on users following a manual ritual such as "give the tag input textbox focus, then scan the tag." There is little magic to be accomplished by trying to grab keystrokes, it doesn't turn a sow's ear into a silk purse.

    Like Robinson Crusoe, it's primitive as can be. Primitive as can be.


    If your Form has no edit controls and no default controls you might resort to keystroke snagging. Then when "enter" arrives the accumulated result is your tag ID. That seems pretty obvious, so ultimately I don't understand the question.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2015
    Posts
    42

    Re: Reading string from USB device

    Hello again, Dilettante,

    Thanks for responding.
    I have proved that the reader sends a linefeed character at the end of the string.
    How can I capture/detect it in VB6?
    I'm now using
    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
        Tag_ID = Tag_ID & Chr(KeyAscii)
        If Chr(KeyAscii) <> "10" Then
            'If Len(Tag_ID) <> 10 Then
            KeyAscii = 0
            Exit Sub
        End If
    But it never goes beyond this as the linefeed is not detected.
    Am I using the right code to detect it?

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,706

    Re: Reading string from USB device

    What makes you think it sends a linefeed? That's not very normal for a device emulating a keyboard.

    And why:

    Code:
    If Chr(KeyAscii) <> "10" Then
    ... instead of just:

    Code:
    If KeyAscii <> 10 Then
    ???


    In any case you can try typing Ctrl-J yourself. Then you should see a linefeed character intercepted.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,998

    Re: Reading string from USB device

    Right now i'm trying to wrap my mind around the question, how this RFID-Reader sends data in first place?

    Especially, when, as dile pointed out, it's basically a keyboard.

    So, just by holding a valid RFID-Tag to the reader it shouldn't do anything.
    It's like having a keyboard in front of you, and just looking at it (not typing!).

    My suspicion: there is another program/process running in a kinda endless loop,
    polling the RFID-reader
    1) "Is there any data?"
    2a) "No. let's continue"
    2b) "Yay. Send it to <wherever> and append a linefeed"
    3) "Goto 1)"
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,706

    Re: Reading string from USB device

    I suspect it goes more like: "Oh, I see an RFID tag has just entered my range. Fetch its ID, tappa tappa tap as a keyboard."

    The "program" is running on a microcontroller.

  7. #7
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    447

    Re: Reading string from USB device

    Try this code to see what's being sent:

    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
        Debug.Print KeyAscii
    End Sub

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2015
    Posts
    42

    Resolved Re: Reading string from USB device

    Well, Ctrl-J certainly is detected, so evidently my search for KeyAscii=10 fails, as the reader must not be sending it!
    If I type CTRL-J, the code works!
    So, I still need to find how to detect the end of the input string.

    Zvoni, yes, the reader is working as a keyboard, but just sending the string of characters read from the fob.
    There are no special drivers. etc., loaded for this, Windows sees it as "SYC ID&IC USB Reader", HID Keyboard Device.
    It is evidently performing as a "keyboard wedge", sending its data to the keyboard buffer.

    UPDATE!

    I can detect KeyAscii=13 (CR instead of LF)
    Problem solved!

    Thanks, guys, for your help, and patience!

    Jim

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,706

    Re: [RESOLVED] Reading string from USB device

    Yes, the "enter" KeyCode (vbKeyEnter) gets mapped to the CR KeyAscii. These even have the same value in this case even though in general they can be different as in shifted/unshifted letters and such.

    If I had understood that this was the question the thread wouldn't have gotten so long. Sorry.


    Of course you'll still have the problem of this device typing garbage at random into other controls. Renaming files can become a risky enterprise. You really want a better-behaved device that communicates as almost anything else but a keyboard.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width