364,088 Members | 5493 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How to control USB port in C#

mustafa828
P: 2
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.
Jan 15 '08 #1
Share this Question
Share on Google+
5 Replies


Frinavale
Expert Mod 5K+
P: 7,034
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.
Expand|Select|Wrap|Line Numbers
  1. Try
  2.             'Making sure to demand permission to access USB
  3.             Dim myPerm As New FileIOPermission(PermissionState.Unrestricted)
  4.             myPerm.Demand()
  5.  
  6.             Dim ValidDriveLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  7.             Dim info As System.IO.DriveInfo
  8.             Dim sb As New StringBuilder
  9.             Dim i As Integer
  10.             Dim len As Integer = ValidDriveLetters.Length - 1
  11.             For i = 0 To len
  12.                 info = New System.IO.DriveInfo(ValidDriveLetters(i))
  13.                 If info.IsReady Then
  14.                     If String.Compare(info.VolumeLabel, "NameOfMyUsbKey", True) = 0 Then
  15.                         sb.Append(info.DriveFormat)
  16.                         Dim wr As IO.StreamWriter = IO.File.AppendText(info.Name + "test.txt")
  17.                         wr.WriteLine("AvailableFreeSpace: " + info.AvailableFreeSpace.ToString)
  18.                         wr.WriteLine("DriveFormat: " + info.DriveFormat)
  19.                         wr.WriteLine("DriveType: " + info.DriveType.ToString)
  20.                         wr.WriteLine("Name: " + info.Name)
  21.                         wr.WriteLine("TotalFreeSpace: " + info.TotalFreeSpace.ToString)
  22.                         wr.WriteLine("TotalSize: " + info.TotalSize.ToString)
  23.                         wr.WriteLine("VolumeLabel: " + info.VolumeLabel)
  24.                         wr.Flush()
  25.                         wr.Close()
  26.                     End If
  27.                 End If
  28.             Next
  29.         Catch ex As Exception
  30.             LBL_Error.Text = "Failed to get permissions: " + ex.Message + " " + ex.StackTrace
  31.         End Try
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...contact the manufacturer for any software development kits available.

-Frinny
Jan 15 '08 #2

mustafa828
P: 2
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.svbinternational.com/bar_code_scanners/bar_code_scannerusb.htm

model: SVB-0006
Jan 15 '08 #3

nmsreddi
100+
P: 366
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.


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.svbinternational.com/bar_code_scanners/bar_code_scannerusb.htm

model: SVB-0006
Jan 16 '08 #4

Frinavale
Expert Mod 5K+
P: 7,034
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
Jan 16 '08 #5

Plater
Expert Mod 5K+
P: 7,575
If the barcode acts like a keyboard, you need to research ways to disable keyboards(perferably 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.GetDrives() 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.
Jan 16 '08 #6

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework