How to control USB port in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mustafa828
    New Member
    • Nov 2006
    • 2

    How to control USB port in C#

    Hello everybody

    I'm developing application using visual C#, and also using Barcode reader connected to USB port, some time i need to stop the barcode reader from reading when the cursor will be in another TextBox, for example when it is inside the price TextBox i don't want it to work.
    Please if you have any information it will be helpful to me.
    thanks in advance to all of you.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The manufacturers of the Barcode Reader should provide you with the drivers necessary to control it.

    To connect to a usb you should use the System.IO class...

    For example, the following code snippet will connect to a USB key (thumb drive), read it's information and create a text file on the USB key containing the USB key's information.
    [code=vbnet]
    Try
    'Making sure to demand permission to access USB
    Dim myPerm As New FileIOPermissio n(PermissionSta te.Unrestricted )
    myPerm.Demand()

    Dim ValidDriveLette rs As String = "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
    Dim info As System.IO.Drive Info
    Dim sb As New StringBuilder
    Dim i As Integer
    Dim len As Integer = ValidDriveLette rs.Length - 1
    For i = 0 To len
    info = New System.IO.Drive Info(ValidDrive Letters(i))
    If info.IsReady Then
    If String.Compare( info.VolumeLabe l, "NameOfMyUsbKey ", True) = 0 Then
    sb.Append(info. DriveFormat)
    Dim wr As IO.StreamWriter = IO.File.AppendT ext(info.Name + "test.txt")
    wr.WriteLine("A vailableFreeSpa ce: " + info.AvailableF reeSpace.ToStri ng)
    wr.WriteLine("D riveFormat: " + info.DriveForma t)
    wr.WriteLine("D riveType: " + info.DriveType. ToString)
    wr.WriteLine("N ame: " + info.Name)
    wr.WriteLine("T otalFreeSpace: " + info.TotalFreeS pace.ToString)
    wr.WriteLine("T otalSize: " + info.TotalSize. ToString)
    wr.WriteLine("V olumeLabel: " + info.VolumeLabe l)
    wr.Flush()
    wr.Close()
    End If
    End If
    Next
    Catch ex As Exception
    LBL_Error.Text = "Failed to get permissions: " + ex.Message + " " + ex.StackTrace
    End Try[/code]

    I doubt you are going to access your reader in this manner.
    You're going to have to use the drivers to control your Barcode reader...contac t the manufacturer for any software development kits available.

    -Frinny

    Comment

    • mustafa828
      New Member
      • Nov 2006
      • 2

      #3
      Thank you Frinavale for your replay.

      The barcode reader is working properly like a keyboard without any driver software, immediatly after i connected to the computer, i need only to stop it from working through my application using VC#.

      The manufactory didn't give me any details about this.
      from this website you can see the barcode reader which i purchased.

      http://www.svbinternat ional.com/bar_code_scanne rs/bar_code_scanne rusb.htm

      model: SVB-0006

      Comment

      • nmsreddi
        Contributor
        • Jul 2006
        • 366

        #4
        Hello

        I dint get your question properly,

        when ever you are reading a barcode then the details related to that will be retrived from the existing database, when you are using the barcode for your purpose why you need to enter text again .can you give some more clarification about your application.


        Originally posted by mustafa828
        Thank you Frinavale for your replay.

        The barcode reader is working properly like a keyboard without any driver software, immediatly after i connected to the computer, i need only to stop it from working through my application using VC#.

        The manufactory didn't give me any details about this.
        from this website you can see the barcode reader which i purchased.

        http://www.svbinternat ional.com/bar_code_scanne rs/bar_code_scanne rusb.htm

        model: SVB-0006

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by mustafa828
          Hello everybody

          I'm developing application using visual C#, and also using Barcode reader connected to USB port, some time i need to stop the barcode reader from reading when the cursor will be in another TextBox, for example when it is inside the price TextBox i don't want it to work.
          Please if you have any information it will be helpful to me.
          thanks in advance to all of you.
          A barcode reader is an external device that will do it's own thing when certain events trigger it to process things (like when it is presented with a barcode).

          I don't think that you'll be able to stop this from happening.

          -Frinny

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            If the barcode acts like a keyboard, you need to research ways to disable keyboards(perfe rably a desired keyboard and not all keyboard) or look for ways to disable USB-DEVICES in general.
            I would guess that it sends windows messages with the key strokes just the way the real keyboard does, so it would be hard to tell the difference.


            On the DriveInfo class, I was unaware of that (DriveInfo.GetD rives() might be better then polling through a letter array). I would be interested to see if this recognizes drives that are mounted to a directory and not a drive letter.

            Comment

            Working...