usb c++

can anyone have some ideas about writing and reading on usb ports using visual c++ or C#
Who is Participating?
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
in C# is quiet easy:

using System.Management;

class SomeClass
        private static ManagementEventWatcher MySerialMonitor;

        public void SomeInitializerMethod()
            MySerialMonitor= new ManagementEventWatcher(@"root\CIMV2", "SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice'");
            MySerialMonitor.EventArrived += new EventArrivedEventHandler(MySerialMonitor_EventArrived);
             // etcetera

        private static void MySerialMonitor_EventArrived(object sender, EventArrivedEventArgs e)
            if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
            {   // this event name can be different
                 // try to discriminate your specific hardware here, there can be other usb devices
                 // Check the 'e' object members carefully

What kind of USB devices are you thinking of? Storage devices can be accessed transparently liek regular hard drives through their drive letter, and other USB devices usually are available via a virtual COM port, see ("Serial library for C++") for more on the latter.
Jaime OlivaresSoftware ArchitectCommented:
It is not common to read usb ports directly, this will require to read the raw packets, not a trivial task.
Usually it is done by reading the underlying driver: serial port, mass storage, video codecs (for cameras), printer driver, human input drivers (for keyboard and mice).
Please specify
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

trinitrotolueneDirector - Software EngineeringCommented:
In any USB solution there has to be a

- target
- host

Today most PCs support the EHCI on the host side...On the target side you need to identify what type of a device it is.
Is the target device a HID device(USB mouse, keyboard...) or a Mass stporage device(USB Hard Disk) ....

You will anyway need to have some firmware application running on the target USB device depending on the type of device and a correspondng host application running on the Windows host side. The host usually initiates connections...

Usually the class driver/application has to be written by you. Check and let me knwo..
klay8Author Commented:
i have a usb to serial port device, and i would like to write a code to detect when remove or plug that one..
i can program in visual studio(any language),..
do this make  a diffrence
Jaime OlivaresSoftware ArchitectCommented:
if you want to detect just insertion/extraction, you don't need specific usb programming, just use the Windows Management Instrumentation library (WMI).
Are you working with managed or unmanaged C++?
trinitrotolueneDirector - Software EngineeringCommented:
you can write a simple C program to do this. Use the datasheet of your USB to Serial converter to figure out the pins.

There will be vendor provided header files which will tell you which line to set to high or low.

I suggest you use the Keil C compiler to do the same.

Using Visual Studio is an overkill for embedded system programming.
trinitrotolueneDirector - Software EngineeringCommented:
what happened?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.