1

Are there any USB developers out there who use .NET?

I know one way of communicating with USB HID device is creating wrappers to the Windows API's like CreateFile, WriteFile and ReadFile. But I am wondering does anyone know if you can use any of the .NET classes to do the same thing. I am certain that I will still have to use some of the API's for operations like detecting which devices are on the bus and requesting the dectriptors but I am looking for a .NET method of sending/receiving reports? I am thinking, for example, that I could use the Stream Class or StreamWriter and StreamReader but I don't know.

Brian Tompsett - 汤莱恩
5,92772 gold badges63 silver badges135 bronze badges
asked Feb 19, 2010 at 17:42
2
  • Have done some USB with the Win32 API and overlapped IO, but never in .NET. However I would guess that if you got the file handle to the device you can instantiate a stream using that (SafeFileHandle and what not)? Commented Feb 19, 2010 at 18:02
  • That is what I was thinking but I am not sure... I am hoping someone has tried it Commented Feb 19, 2010 at 18:06

3 Answers 3

2

The System.IO.FileStream class is a highly suitable replacement. Its constructor calls CreateFile(), its Read method calls ReadFile(), its Write method calls WriteFile(). You should have little trouble matching the FileAcces, FileMode and FileShare enums to the corresponding CreateFile() arguments. You will however have to P/Invoke the SetupAPI functions.

answered Feb 19, 2010 at 18:21
Sign up to request clarification or add additional context in comments.

3 Comments

The path to the device returned by the SetupAPI functions looks like this "\\\\?\\hid#vid_0461&pid_4d15#6&3535d8ec&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}" If I try FileStream myStream = System.IO.File.OpenRead( above path ); I get an error saying illegal characters in path.
Ouch, yes, the ? character doesn't pass a check on the path by the FileIOPermission class. Bit of a bug there, although it would be hard to do otherwise. P/Invoking CreateFile is your lot, you can still use FileStream through the FileStream(SafeFileHandle, FileAccess) constructor.
I tried this for a bit but didn't have much luck. There are still a lot of HID API's that I needed to make wrappers for anyways so I just bit the bullet and gave up on FileStream...
1

Probably have more luck using a library that wraps all the P/Invoke calls for you. One I've looked at is HidLibrary.

answered Sep 9, 2010 at 19:23

1 Comment

HidLibrary is working wonders for me. Thanks for the pointer!
0

FileStream is working well enough for me, though mine is a very simple USB implementation.

I use CreateFile to initially set up the device,but then do all the reading and writing to the device through the FileStream object.

answered Feb 16, 2011 at 18:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.