 |
|
 |
Thanks see you later by experience about it!
|
|
|
|
 |
|
 |
If i have no idea for VID & PID.
Can I use software to find the VID & the PID?
Thanks!
|
|
|
|
 |
|
 |
Hello
It has been sometime I wrote this software, it is not designed for this originally but if I remember well you have everything you need to be able to scan for devices and get VIDs and PIDs.
Stephane.
|
|
|
|
 |
|
|
 |
|
 |
I see that this is based on a WinForm public bool RegisterForDeviceChange(bool Register, System.Windows.Forms.Form f)
I also see Win32Wrapper.DEVICE_NOTIFY has two other values for the flags. Any chance of an overload for use within a Windows Service?
Great piece of code!!
|
|
|
|
 |
|
 |
Hello,
It has been a while now I posted this code I may not be really helpful to answer you.
I think you should be able to use it in a Windows service but have to rewrite a new function RegisterForDeviceChange to hook to some other object than a Windows form.
If you do it, you may want to let us know how you did it.
Thanks, Stephane.
|
|
|
|
 |
|
 |
I've been trying to do the same thing but the only way I could find was to use unmanaged code or WMI. Please let me know if you were successful in creating a similar class without having to hook up to an object.
Thanks!
Mart
|
|
|
|
 |
|
 |
I was looking for methods to detect connection and disconnect of USB CDC device, and here I got perfect solution. Thanks slelong for this perfect article.
|
|
|
|
 |
|
 |
Hello,
Thank you. Stephane.
|
|
|
|
 |
|
 |
Is there also a possibility to use this class in WPF?
|
|
|
|
 |
|
 |
WPF, to me, impact the way you build your application, mainly the user interface.
It should not prevent you using this C# class.
Stephane.
|
|
|
|
 |
|
 |
WPF Windows don't expose the WndProc method by default, you have to set that up on a initialisation/loaded event as explained in this link. Once you've done that, you need to alter the RegisterForDeviceChange method slightly to take a handle instead of a System.Windows.Forms.Form and use the handle you get from the
new WindowInteropHelper(this).Handle
line. The change to the method should be obvious.
Really nice library BTW.
Complete code for a (minimal) WPF window attached, you need to add a couple of references (System.Windows.Forms and USBClassLibrary):
using System;
using System.Windows;
using System.Windows.Interop;
using USBClassLibrary;
namespace WpfApplication
{
public partial class MainWindow : Window
{
private USBClassLibrary.USBClass.DeviceProperties USBDeviceProperties;
private bool MyUSBDeviceConnected { get; set; }
private USBClass USBPort { get; set; }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
IntPtr handle = new WindowInteropHelper(this).Handle;
HwndSource source = HwndSource.FromHwnd(handle);
source.AddHook(new HwndSourceHook(WndProc));
USBPort.RegisterForDeviceChange(true, handle);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
System.Windows.Forms.Message m = new System.Windows.Forms.Message();
m.HWnd = hwnd;
m.Msg = msg;
m.WParam = wParam;
m.LParam = lParam;
USBPort.ProcessWindowsMessage(ref m);
return IntPtr.Zero;
}
public MainWindow()
{
InitializeComponent();
USBPort = new USBClass();
USBDeviceProperties = new USBClass.DeviceProperties();
USBPort.USBDeviceAttached += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceAttached);
USBPort.USBDeviceRemoved += new USBClass.USBDeviceEventHandler(USBPort_USBDeviceRemoved);
USBTryMyDeviceConnection();
MyUSBDeviceConnected = false;
}
private bool USBTryMyDeviceConnection()
{
if (USBClass.GetUSBDevice(0x0525, 0xa4a2, ref USBDeviceProperties, false))
{
Connect();
return true;
}
else
{
Disconnect();
return false;
}
}
private void USBPort_USBDeviceAttached(object sender, USBClass.USBDeviceEventArgs e)
{
if (!MyUSBDeviceConnected)
{
if (USBTryMyDeviceConnection())
{
MyUSBDeviceConnected = true;
}
}
}
private void USBPort_USBDeviceRemoved(object sender, USBClass.USBDeviceEventArgs e)
{
if (!USBClass.GetUSBDevice(0x0525, 0xa4a2, ref USBDeviceProperties, false))
{
MyUSBDeviceConnected = false;
Disconnect();
}
}
private void Connect()
{
MessageBox.Show("Connected!");
}
private void Disconnect()
{
MessageBox.Show("Disconnected!");
}
}
}
|
|
|
|
 |
|
 |
Hi Thanks for an excellent piece of work. I found that the COM PORT part of the class does not work with the FTDI chip. I got around this by using SerialPort.GetPortNames() on the device connected event and comparing the list of ports to those before the event. On gotcha here is that the port does not show up for about 5 to 15 seconds. I just poll the GetPortNames() until I get my port or time out. Maybe the big delay between the USB enumeration and the COM port being created is what is stopping your class working?
|
|
|
|
 |
|
 |
Hello
I am not sure I can help you because I do not have the FTDI chip to investigate / debug what may be happening.
Please apologize.
Stéphane.
|
|
|
|
 |
|
 |
Hi All,
I'm trying to use the USBClassLibrary with a barcode scanner.
The Attach and Detach handlers is working fine !
Now I would catch the scan event to do something just before the code arrival.
Because I would do something before the code is read
I find 3 events in the class :
USBDeviceAttached
USBDeviceRemoved
USBDeviceQueryRemove
How to detect the activity (a new scan) on the device... ?
Should I use an event to do this or is it possible to detect the activity without an event ?
Many thanks in advance
Nicolas
modified on Monday, November 8, 2010 1:47 AM
|
|
|
|
 |
|
 |
Hey man, you´re amazing to write this code. I finally understand how I can to connect a USB devide and manage it but i have a little question: This library can send and receive data (bytes) from the USB device attached with this library?
|
|
|
|
 |
|
|
 |
|
 |
If it is possible
Please tell me the way
Thanks
Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim
So Smile Please
|
|
|
|
 |
|
 |
Hello
Can you please look at the section "updating the code" ? If you cannot find it, you may need to look for other ways.
Stéphane
|
|
|
|
 |
|
 |
As per my knowledge every device, whether it is HDD, MB, DVD or USBKit.. all, have unique serial number set by manufacturer
Anyway thanks for your response
Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim
So Smile Please
|
|
|
|
 |
|
 |
Hello
Ok but did you check the update section?
Steph.
|
|
|
|
 |
|
 |
Hi there!
Since I need to read the SN from my usb device, I went through this topic, but can't get it to work.
Any chance to have an help from you?
bye
|
|
|
|
 |
|
 |
Hi
All usb devices have a serial (part of the standard), but if it's filled is a completely different matter (I seriously doubt it).
wvd_vegt
|
|
|
|
 |
|
 |
hey guys
I found this code via google and I've added the code into a C# 2010 Express test project. But now I got 12 Failures in the program.. I hope you can help me
Here are the failures:
The Name "WIN32Wrapper" is currently not in the context. // NOTE: Just translate it from german into english.. I hope it is correct
The Name "h" is currently not in the context.
The Name "DevInfoData" ...
The Name "RegType" ...
The Name "IntPtrBuffer" ...
The Name "Buffer_Size" ...
The Name "RequiredSize" ...
The Name "Marshal" ...
Are these special functions from older c# version or why I got these? I hope you can help me
greetz
|
|
|
|
 |
|
 |
Hello, I just do no know what you did. If you open my project, I guess it should work but you may have to twick some project settings.
Steph.
|
|
|
|
 |