How to access USB port via VB.NET?
-
Monday, January 29, 2007 6:35 AM
Hi,
I am currently doing a program to control some device via usb port but i don't know how to interface the usb port using vb.net. anybody can help me with this problem as which namespace I should use? thanks a lot
All Replies
-
Monday, January 29, 2007 12:49 PM
Hi
It depends on the device that you are trying to communicate with.
Many USB devices simply emulate COM ports (S = Serial) and can therefore be communicated with using the System.Net.Sockets.Socket class. For those that don't, it is usually best to get hold of a generic driver that can handle the conversion for you (http://www.thesycon.com/eng/usbio.shtml)
Hope this helps.
RIchard
-
Monday, February 12, 2007 7:29 AM
According to a Microsoft MVP, VB.NET doesn't have any generic support for USB. You will have to use the Windows API or the API that comes with the USB device. See http://www.thescripts.com/forum/thread336454.html
Ezani
-
Tuesday, February 13, 2007 12:37 AMthanks a lot for your info
-
Sunday, October 11, 2009 1:50 PMYou can use the wmi to do some of your USB coding
Example to detect if a device is plugged in you would use this code
Imports
System.Management
Public
Class Form1
Dim w As ManagementEventWatcher
Private Sub AddUSBHandler()
Dim q As WqlEventQuery
Dim scope As ManagementScope = New ManagementScope("root\CIMV2")
scope.Options.EnablePrivileges =
True
Try
q =
New WqlEventQuery()
q.EventClassName =
"__InstanceOperationEvent"
q.WithinInterval =
New TimeSpan(0, 0, 1)
q.Condition =
"TargetInstance ISA 'Win32_USBControllerdevice'"
w =
New ManagementEventWatcher(scope, q)
AddHandler w.EventArrived, AddressOf USBEvent
w.Start()
Catch ex As Exception
MessageBox.Show(ex.Message)
If Not w Is Nothing Then
w.Stop()
End If
End Try
End Sub
Public Sub USBEvent(ByVal sender As System.Object, ByVal e As EventArrivedEventArgs)
If e.NewEvent.ClassPath.ClassName = "__InstanceCreationEvent" Then
Dim s As String
s += e.NewEvent.Properties(
"TargetInstance").Value.Properties("Antecedent").Value.ToString() + ControlChars.CrLf
s += e.NewEvent.Properties(
"TargetInstance").Value.Properties("Dependent").Value.ToString() + ControlChars.CrLf
MsgBox(
"USB inserted, the information is " + ControlChars.CrLf + s)
ElseIf e.NewEvent.ClassPath.ClassName = "__InstanceDeletionEvent" Then
Dim s As String
s += e.NewEvent.Properties(
"TargetInstance").Value.Properties("Antecedent").Value.ToString() + ControlChars.CrLf
s += e.NewEvent.Properties(
"TargetInstance").Value.Properties("Dependent").Value.ToString() + ControlChars.CrLf
MsgBox(
"USB removed, the information is " + s)
Else
MsgBox(e.NewEvent.ClassPath.ClassName)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddUSBHandler()
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
If Not w Is Nothing Then
w.Stop()
End If
End Sub
End
Class
It may take me a few days but I will be posting code on Http://ess-image.com someplace showing how to talk to usb video control devices such as pan tilt units and controling the camera, so that may help you.
You can also find code sample on codeproject but the ones I found were in C++
I hope this helps you out some -
Friday, November 20, 2009 7:40 PMExcalibursoftware thanks for the example. I tried this and I get "ManagementEventWatcher" is not defined, plus others. Is there something missing that I haven't put in? I used the code exactly as you've shown here.
-
Friday, November 20, 2009 8:09 PM
Youmay need to add some Imports statemets at the top of the code. Fo instance, for ManagementEventWatcher you would need
Imports System.Management
or you can fully specifiy the class in your code - System.Management.ManagementEventWatcher. -
Tuesday, August 31, 2010 12:16 AMThank you for posting that!! It is a great help.
-
Tuesday, August 31, 2010 12:16 AMMake sure you add the .NET reference for system.management to your project.
-
Thursday, November 17, 2011 1:18 AM
can i use this code to controll a device that will act like parallel port.?
i want to use usb port to send data on the device that i will make for it to act as a parallelport.
-
Thursday, November 17, 2011 2:06 AM
The device that you will use to act like a parallel port make will provide an interface for the USB port. This interface might be a virtual serial port, or it might be a custom USB interface that uses a device driver. It depends on the device that you are using for this task, the software that comes with that device, or the software that you write for the USB connection on that device.
The fact that you are using this device to act like a parallel port affects the code that runs on the device to manage that parallel port, and it affects the data that you will transfer between the device and the PC. It does not affect the way that the device's USB port communicates with the PC.

