You are here:

C++/usb input/output in c and c++ is there any way?


this is venkatesh and i am an engineering computer student.i like to know if there is any way to write code in c or c++ to take input or out put from the usb port. in fact what exactly i like to do is i want to control the hard ware(any thing)through the usb port.i like to sent some sort of signal (may be 0 or 1 or else some voltage level- i mean a signal) through the port.
 for example the audio jack .we send several signals in the form of voltage signals(amplitude)for bringing out a sound. the same way i like to send some signal through the usb connected wire and i like to do some work with that signal(abstract).
 if i cant then how do these audio players designed?how can they send the signals through the audio jack? how they are programed?
 help me....
         thank you.

The answer is yes - probably. It depends on what sort of system you are using. Most modern desktop / workstation / server operating systems [MS Windows 32 and 64 bit (except 9x and ME), UNIX, Linux, Mac OSX, etc.]  do not allow you direct access to the hardware. Instead access to devices is provided through device drivers and operating system services. However C and C++ are perfectly valid choices here as well.

In some cases raw access to hardware (if you have access to it or say are writing a device driver) will require facilities not provided by standard C/C++ - Intel X86 I/O ports for example. In such cases you either have to rely on support via non-standard language extensions or additional library support or do such access using assembler.

However USB ports although simple in their physical make up are complex from the protocols and (presumably) signals and software points of view - I am a C++ expert not a USB specification expert, however I would think that looking at USB as more like a small network would be a better idea.

I remind you the computers we use day to day are _digital_ computers and this in fact is reflected in the interfaces they use including USB. USB is a serial binary interface - hence you only get to send signals meaning 0 or 1 down the wire - although these may of course be encoded electrically so that more than one bit at a time can be represented - again I am a C++ expert not a USB interface / signal expert. You do _not_ get to just hijack the interface to send your own arbitrary analogue signal down it. In fact signals such as MP3 only get decoded to audio analogue signals at the very last stages. I suspect decoding and listening to an MP3 track stored on your PC via a USB audio device connected to that PC would have a rough process path something like so:

(probably best to view using a fixed pitch font such as Courier)

    [PC computer
        MP3 encoded binary data
         <MP3 decoder>
       Raw binary audio samples
       <USB audio device driver>
         <USB protocol stack>
         |   (USB cable)
         |   (still binary data)
   [USB Audio device
       <Digital to analogue conversion>
         Analogue audio signal
       <amplification, filtering etc.>
         | ( headphone cable)

Of course you can have variations - for example if the device understood MP3 formatted data then the MP3 decoding could be moved to the audio device.

Of course if the device were a MP3 player then it is likely that the USB connection is used more like this second scenario - the USB connection is used primarily for raw data transport - remember such devices have their own storage, and so appear more like an external storage device. If they can be programmed or controlled via the PC as well then their device driver will incorporate some sort of control system for sending commands and the like which the PC program can use.

I am not sure where you think audio jacks fit into the USB picture - they don't.

Of course some devices have custom connectors that might allow either a USB connection or an audio connection (or for other functions such as recharging the device) - this was popular with mobile phones until recently for example.

If you wish to know more about USB and PC interfacing in general then I suggest you start at a site like Beyond Logic:

You will notice the whole menu at the top of the page devoted to USB topics.

For some information on hardware interfacing using C++ (and C) from the ISO C++ standards committee then look at:

Specifically section 8.

Hope this is of use.


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.

[an error occurred while processing this directive]