#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    47
    Rep Power
    10

    Using C to send data through USB port


    Hi,
    I am looking for a little help regarding how I can send data through the USB port using C. I am currently designing a project where I need to send a few bytes to an arduino board connected via USB.

    I have been doing some googling, but I haven't been able to get too much info. I;m not sure how many options there are for doing this, but the only one I find is by using libusb available on sourceforge.

    Anyway I downloaded the library and am not even sure how to use it. I have copied the .a, .c and .h files to the directory of my program, but when I call the usb_init(); function in my code it produces a linker error.

    I am not even sure if I have done what is nessesery with libusb due to my lack of experience and knowledge in this field, so my main questions are:

    Can anyone give me maybe a step by step guide on what I have to do with libusb in order to use the functions. (I am using Dev-C to write my programs, but can chaange if it is nessesery)

    Is there another way of sending data through USB which is more straightforward and easily understood for a beginner like me?

    Many thanks for any suggesstions
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    The linker error is because you haven't told your compiler to link to libusb.a. Just because you download the file to your computer doesn't mean the compiler knows about it. You have to go to your Project Options --> Parameters and add -lusb to the linker options.

    In addition, you may need to go to Compiler --> Options and add the directory that you have downloaded libusb into your list of directories.

    Incidentally, why Dev-C++ and why not Visual C++. It is a better compiler, comes with a set of better dev tools and there is a free version as well.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    47
    Rep Power
    10
    Thank you very much for your reply.
    I did what you recommended and I can successfully compile a program, so thank you for saving me some time.

    I am writing a program to communicate with an arduino board (www.arduino.cc) which has an FT232R USB UART.

    I have a program which seems is able to talk to the UART on the external board, as I have a small program with LED's which flash once it receives something.

    I have been using the usb_bulk_write function to send something, but I am not sure how to read the response from the board. The way I have designed the program on the arduino, is to send back data when it receives a certain character, for example it sends back the temperature when I send 'T', the Windpeed when I send 'W'..etc.

    My level of C is very basic and also I do nt find it easy to interpret how to use the functions of libusb.

    I am hoping that someone can guide me i the right direction. I think I'm close, as the external board is sending back some data. But I just need to know how I can capture it.

    I have tired using the usb_bulk_read function, but I dont understand the parameters that are passed (http://libusb.sourceforge.net/doc/function.usbbulkread.html). Maybe someone can enlighten me as to what are refered to by endpoints?

    Below are the 2 functions I have been using to communicate with the board. usb_bulk_write seems to work, but I receive -116 when I run the usb_bulk_read command. I'm not sure what I doing wrong, but any ideas wuld be much appreciated.
    Thanks

    Code:
    response = usb_bulk_write(dev, EP_OUT, tmp, sizeof(tmp), 5000); printf("Arduino says: %d\n",response);   //    { //      printf("error: bulk write failed\n"); //      system("PAUSE"); //    }   response = usb_bulk_read(dev, EP_IN, tmp, sizeof(tmp), 5000); printf("Arduino says: %d\n",response);
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    47
    Rep Power
    10
    Incidentally, why Dev-C++ and why not Visual C++. It is a better compiler, comes with a set of better dev tools and there is a free version as well.
    Can someone tell me where I can find the free version. I have been googling but I dont see it on the microsoft site

    Thanks
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Any search of "visual studio express" should lead you to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    19
    Rep Power
    0
    all the pcs i have purchased have came with visual C++ and studio express
  12. #7
  13. Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jun 2005
    Posts
    5,964
    Rep Power
    4852
    They're atypical, then.
    Write no code whose complexity leaves you wondering what the hell you did.
    Politically Incorrect DaWei on Pointers Grumpy on Exceptions

IMN logo majestic logo threadwatch logo seochat tools logo