Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ to USB interface

Options
  • 03-11-2010 12:56pm
    #1
    Registered Users Posts: 366 ✭✭


    I'm wondering if anyone knows how to communicate from C++ to a USB serial port? It seems like ill have to write my own driver for the communication and was just wondering if anyone has previous experience of this?


Comments

  • Registered Users Posts: 856 ✭✭✭firefly08


    I presume you're using Windows?

    Try this: http://sourceforge.net/apps/trac/libusb-win32/wiki

    I have no direct experience of this, but I can tell you writing a device driver for something like this can be extremely difficult. Windows kernel programming is about as complicated as Windows programming can possibly be.

    Chances are, if you're in any doubt, you're probably better off not writing one yourself. There is a really good series of articles on Windows driver development at The Code Project if you're interested in seeing what it involves.

    Can I ask what kind of device are you trying to communicate with? Usually existing drivers will suffice. If you're looking at some kind of home made device, then usually the USB modules you get in places like Maplin will come with a driver and sample code.


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    I'm wondering if anyone knows how to communicate from C++ to a USB serial port? It seems like ill have to write my own driver for the communication and was just wondering if anyone has previous experience of this?

    You communicate to a USB serial port the same was as you communicate to any serial port, it should be transparent to your application.

    What OS are you using?

    If your using Windows you'll be stuck using file I/O to communicate with the serial port from C++. There are wrapper classes available, which might make things a bit easier for you.


  • Registered Users Posts: 981 ✭✭✭fasty


    firefly08 wrote: »
    I have no direct experience of this, but I can tell you writing a device driver for something like this can be extremely difficult. Windows kernel programming is about as complicated as Windows programming can possibly be.

    I 100% agree with this. Issues with threads, buffers and memory allocation in kernel land are incredidbly impossible to debug since usually it'll just bluescreen your computer and you'll get a crash dump to eyeball.

    As mentioned, a USB serial port in Windows is just a serial port. Find out what COM port it is and read/write from it!


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    firefly08 wrote: »

    I have no direct experience of this, but I can tell you writing a device driver for something like this can be extremely difficult. Windows kernel programming is about as complicated as Windows programming can possibly be.

    He's not trying to write a USB driver, just communicate over a USB serial port. They are 2 very different things...


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Depends on the purpose here.

    If it's for commercial use, you're probably better off going through an abstract wrapper class.

    If it's as a learning exercise, then writing your own driver is a really interesting thing to do.


  • Advertisement
  • Registered Users Posts: 2,426 ✭✭✭ressem


    As DublinDilbert has said, from what you've stated it doesn't look like you need a driver. You just want to talk to a serial port?

    Are there requirements that you've not stated e.g. allowing multiple applications to communicate with one or more devices with your code preventing them from talking over each other?

    In windows and unmanaged C++ there are examples of configuring and monitoring serial ports ...
    configuring
    http://msdn.microsoft.com/en-us/library/aa363201%28v=VS.85%29.aspx
    monitoring (so that multiple threads don't talk over each other)
    http://msdn.microsoft.com/en-us/library/aa363424%28v=VS.85%29.aspx

    You can use ReadFile and WriteFile, passing the com port's handle to send and receive the character arrays.


Advertisement