Announcement

Collapse
No announcement yet.

Get data from barcode scanner

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Get data from barcode scanner

    I've got a hand held barcode scanner that I would like to connect to my HomeSeer server and have Homeseer grab the incoming data from the unit and just add to a list.

    The barcode scanner has a PS2 connection which is plugged into a USB adaptor and shows up as a second HID Keyboard Device in Device Manager.

    I can successfully scan a barcode and have it appear in notepad. But is it possible to have HomeSeer monitor just this 'Keyboard' for incoming data?

    Thanks for your help :-)

    #2
    Originally posted by Simonk View Post
    I've got a hand held barcode scanner that I would like to connect to my HomeSeer server and have Homeseer grab the incoming data from the unit and just add to a list.

    The barcode scanner has a PS2 connection which is plugged into a USB adaptor and shows up as a second HID Keyboard Device in Device Manager.

    I can successfully scan a barcode and have it appear in notepad. But is it possible to have HomeSeer monitor just this 'Keyboard' for incoming data?

    Thanks for your help :-)
    Oh man, this brings back some nightmares about the many many hours I lost to a similar project in the past.

    You can implement some low level system hooks to differentiate between what comes from the keyboard and what comes from the HID device (or if it was just key inputs full stop I can't remember). I wrote an app to do this and then this app set a device string in HS and triggered an event IIRC (which was in HS2). I really had a time of trying to find a decent solution because that relied on me running the application off of a Joggler I had in the kitchen which I never really liked running on XP (my HS PC not being close to the point of reading). Not ideal.

    I then I had a older serial reader from eBay because I thought serial can be transmitted no problem and I can take the PC out of the picture. I got used but had to get an additional cable (thought it was RJ45 and I can build a cable no problem but turned out it was RJ50 - I'd never even heard of it before) and I then ran a wireless solution by building a bluetooth serial adaptor. It went on for months really.

    From there in HS I used a web service to check the UPC against a database, then add it to a list (which I think may have been a database) once I specified a quantity, then it produced a PDF report and using an ancient version of acrobat you could print documents without launching the reader by passing in a command line argument. Problem solved I thought but in reality I did'nt use it often enough to justify it so tore it out and put it is in the nightmare project box. I may still have the code for the app but I think it would be largely useless as it is for HS2. The web service was not great because whilst it was free there were relatively few UK products that were in it and of those I think it was something odd like Heinz being the main contributor.

    If I was in your position I think I would try and find if it was a true PS/2 keyboard output (as opposed to just using a 6 pin mini din connector) and read the data in an arduino and send the data straight into HS on a serial port.

    Comment


      #3
      Thanks mrhappy for your detailed response.

      I didn't think it would be easy to capture the data if it was just connecting as a second keyboard! I've had a look on Amazon and found a cheap USB hand held scanner that says that it can connect as a Virtual Com Port as well as other options, so I've just ordered one of those to play with.

      I've had a play around with capturing data from com port before so I should be able to figure that bit out!

      I've got cat5 network points available where the scanner would be located which goes to where the server is so was hoping to be able to extend the USB cable using this, I've seen usb to cat5 adaptors available.

      Comment


        #4
        Originally posted by Simonk View Post
        Thanks mrhappy for your detailed response.

        I didn't think it would be easy to capture the data if it was just connecting as a second keyboard! I've had a look on Amazon and found a cheap USB hand held scanner that says that it can connect as a Virtual Com Port as well as other options, so I've just ordered one of those to play with.

        I've had a play around with capturing data from com port before so I should be able to figure that bit out!

        I've got cat5 network points available where the scanner would be located which goes to where the server is so was hoping to be able to extend the USB cable using this, I've seen usb to cat5 adaptors available.

        If you get this to work and make a plugin for it, I'd happily buy it! Totally unnecessary but what a blast!
        Originally posted by rprade
        There is no rhyme or reason to the anarchy a defective Z-Wave device can cause

        Comment


          #5
          Originally posted by Simonk View Post
          Thanks mrhappy for your detailed response.

          I didn't think it would be easy to capture the data if it was just connecting as a second keyboard! I've had a look on Amazon and found a cheap USB hand held scanner that says that it can connect as a Virtual Com Port as well as other options, so I've just ordered one of those to play with.

          I've had a play around with capturing data from com port before so I should be able to figure that bit out!

          I've got cat5 network points available where the scanner would be located which goes to where the server is so was hoping to be able to extend the USB cable using this, I've seen usb to cat5 adaptors available.
          Strangely I still have the source code for the application I wrote. It was a little forms based application that had a class I have clearly obtained from somewhere to watch the keystrokes. From what I can see and remember I think the barcode scanner I had put a D character at the start of every scanned code and pressed return at the end which is what I appear to have waited for. Looking at it I clearly didn't spend a lot of time on it...

          Code:
          Imports System.Runtime.InteropServices
          
          Public Class KeyboardHook
          
              <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
              Private Overloads Shared Function SetWindowsHookEx(ByVal idHook As Integer, ByVal HookProc As KBDLLHookProc, ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
              End Function
              <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
              Private Overloads Shared Function CallNextHookEx(ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
              End Function
              <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
              Private Overloads Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Boolean
              End Function
          
              <StructLayout(LayoutKind.Sequential)> _
              Private Structure KBDLLHOOKSTRUCT
                  Public vkCode As UInt32
                  Public scanCode As UInt32
                  Public flags As KBDLLHOOKSTRUCTFlags
                  Public time As UInt32
                  Public dwExtraInfo As UIntPtr
              End Structure
          
              <Flags()> _
              Private Enum KBDLLHOOKSTRUCTFlags As UInt32
                  LLKHF_EXTENDED = &H1
                  LLKHF_INJECTED = &H10
                  LLKHF_ALTDOWN = &H20
                  LLKHF_UP = &H80
              End Enum
          
              Public Shared Event KeyDown(ByVal Key As Keys)
              Public Shared Event KeyUp(ByVal Key As Keys)
          
              Private Const WH_KEYBOARD_LL As Integer = 13
              Private Const HC_ACTION As Integer = 0
              Private Const WM_KEYDOWN = &H100
              Private Const WM_KEYUP = &H101
              Private Const WM_SYSKEYDOWN = &H104
              Private Const WM_SYSKEYUP = &H105
          
              Private Delegate Function KBDLLHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
          
              Private KBDLLHookProcDelegate As KBDLLHookProc = New KBDLLHookProc(AddressOf KeyboardProc)
              Private HHookID As IntPtr = IntPtr.Zero
          
              Private Function KeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
                  If (nCode = HC_ACTION) Then
                      Dim struct As KBDLLHOOKSTRUCT
                      Select Case wParam
                          Case WM_KEYDOWN, WM_SYSKEYDOWN
                              RaiseEvent KeyDown(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
                          Case WM_KEYUP, WM_SYSKEYUP
                              RaiseEvent KeyUp(CType(CType(Marshal.PtrToStructure(lParam, struct.GetType()), KBDLLHOOKSTRUCT).vkCode, Keys))
                      End Select
                  End If
                  Return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam)
              End Function
          
              Public Sub New()
                  HHookID = SetWindowsHookEx(WH_KEYBOARD_LL, KBDLLHookProcDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
                  If HHookID = IntPtr.Zero Then
                      Throw New Exception("Could not set keyboard hook")
                  End If
              End Sub
          
              Protected Overrides Sub Finalize()
                  If Not HHookID = IntPtr.Zero Then
                      UnhookWindowsHookEx(HHookID)
                  End If
                  MyBase.Finalize()
              End Sub
          
          End Class
          Code:
          Imports HomeSeer2
          Imports Scheduler
          
          Public Class frmBarcode
          
              Public hsinterface As HomeSeer2.application
              Public hsapp As Scheduler.hsapplication
              Private WithEvents kbHook As New KeyboardHook
              Public connectStatus As Boolean = False
              Const LT As String = "Barcode"
              Dim LastIsBarcode As Boolean = False
              Dim BC(1) As Integer
              Dim i As Integer = 0
              Dim FinalCode As String = ""
          
          
              Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
                  If connectStatus Then
                      If Key.ToString.Substring(0, 1) = "D" And Key.ToString.Length = 2 Then
                          BuildCode(Key.ToString.Replace("D", ""))
                          LastIsBarcode = True
                      ElseIf Key.ToString = "Return" And LastIsBarcode = True Then
                          FinishCode()
                      Else
                          LastIsBarcode = False
                      End If
                  End If
              End Sub
          
              Private Sub BuildCode(ByVal KeyString As String)
          
                  BC(i) = Convert.ToInt16(KeyString)
                  i = i + 1
                  ReDim Preserve BC(i)
          
              End Sub
          
              Private Sub FinishCode()
          
                  For j As Integer = 0 To (BC.GetUpperBound(0) - 1)
                      FinalCode = FinalCode & BC(j)
                  Next
          
                  hsapp.WriteLog(LT, FinalCode)
                  hsapp.RunEx("barcode.vb", "Main", FinalCode)
          
                  FinalCode = ""
                  Array.Clear(BC, 0, (BC.GetUpperBound(0) - 1))
                  i = 0
                  ReDim BC(1)
              End Sub
          
          
              Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  Try
                      hsinterface = New HomeSeer2.application
                      hsinterface.SetHost("192.168.0.102")
          
                      Dim rval As String = hsinterface.Connect("hslogin", "hspassword")
          
                      If rval <> "" Then
                          connectStatus = False
                          Exit Sub
                      Else
                          hsapp = hsinterface.GetHSRef
                          hsapp.WriteLog(LT, "Application Connected")
                          connectStatus = True
                      End If
                  Catch ex As Exception
                      connectStatus = False
                  End Try
          
                  Me.WindowState = FormWindowState.Minimized
                  Me.Opacity = 0
              End Sub
          End Class

          Comment


            #6
            And if you manage to work a plugin out of this We should all buy mrhappy beers.
            Originally posted by rprade
            There is no rhyme or reason to the anarchy a defective Z-Wave device can cause

            Comment


              #7
              Barcodes usually start and end with an * (asterisk). As long as you didn't use the * key on the regular keyboard, you could simply write an app to monitor all keystrokes, and when a first * is detected start collecting the keystrokes into a data string until a second * is seen. Then stop collecting, and pass the collected keystroke data string to HS.

              You could also get away with a simple timer that if an * was entered from the keyboard and a second * isn't detected in say 1-3 seconds, then stop collecting keystrokes and clear the data string.

              You could also add prefixes (like Mr Happy's "D") to signify different command entries.

              Comment

              Working...
              X