Interfacing via USB using libusb to arduino

Hi,
I am trying to develop an application using C to try and interface to the arduino board using the libusb library available from sourceforge.net.

My level of C is poor and am having some problems understanding how to use and understand some of the functions. Basically I have developed code on the arduino which samples weather conditions from sensors. The plan is to have a C program on the PC which runs every 10 mins and sends for example T for temperature and H when it wants a humidiity reading etc.. Anyway I have some of the C code written to send these characters and from using LED's on the arduino board, I can see that the arduino board successfully receives and responds with the relevant data. The problem I am having is in reading the data which the arduino responds with. Below is my code:

#include <usb.h>
#include <stdio.h>
#include <time.h>
#include <dos.h>


/* the device's vendor and product id */
#define MY_VID 0x0403
#define MY_PID 0x6001

/* the device's endpoints */
#define EP_IN 0x81
#define EP_OUT 0x02

#define BUF_SIZE 1

usb_dev_handle *open_dev(void);

usb_dev_handle *open_dev(void)
{
  struct usb_bus *bus;
  struct usb_device *dev;

  for(bus = usb_get_busses(); bus; bus = bus->next) 
    {
      for(dev = bus->devices; dev; dev = dev->next) 
        {
          if(dev->descriptor.idVendor == MY_VID
             && dev->descriptor.idProduct == MY_PID)
            {
              printf("Found Specified Device\n");  
              return usb_open(dev);
            }
           else
           {
               printf("Didn't Find Him\n");  
           } 
        }
    }
  return NULL;
}

int main(void)
{
  usb_dev_handle *dev = NULL; /* the device handle */
  char tmp[BUF_SIZE] = "H";
  int response;

  usb_init(); /* initialize the library */
  usb_find_busses(); /* find all busses */
  usb_find_devices(); /* find all connected devices */


      if(!(dev = open_dev()))
    {
      printf("error: device not found!\n");
      system("PAUSE");
      return 0;
    }

  if(usb_set_configuration(dev, 1) < 0)
    {
      printf("error: setting config 1 failed\n");
      usb_close(dev);
      system("PAUSE");
      return 0;
    }

  if(usb_claim_interface(dev, 0) < 0)
    {
      printf("error: claiming interface 0 failed\n");
      usb_close(dev);
      system("PAUSE");
      return 0;
    }
  
  if(usb_bulk_write(dev, EP_OUT, tmp, sizeof(tmp), 5000) 
     != sizeof(tmp))

response = usb_bulk_write(dev, EP_OUT, tmp, sizeof(tmp), 5000);
printf("Arduino says: %d\n",response); 


  if(usb_bulk_read(dev, EP_IN, tmp, sizeof(tmp), 5000) 
     != sizeof(tmp))
    {
      printf("error: bulk read failed\n");
      system("PAUSE");
    }

  usb_release_interface(dev, 0);
  usb_close(dev);
  

  system("PAUSE");
  return 0;
}

When I call the usb_bulk_write function it returns a 1 to say it was run successfully and the arduino seems to respond. I don't understand what I have to do to read data. Using the usb_bulk_read does not work for me. I can't fully grasp how it should be used. I also don't fully understand what is meant by endpoints?
When the arduino board responds using the serial.write function, is this data automatically transmitted, or is it held in a buffer until another application requests it?
I would be very grateful if anyone who has experience using libusb or who understands a little more about USB than me could point me in the right direction.
I think I'm getting close to a solution but i have found it difficult to find help on the use of libusb.

Many thanks for any help.

Is there a reason why you're wanting to use libusb rather than just access a serial port?

--Phil.

Hi Phil,
No there is no real reason for using libusb. When you say serial port are you referring to RS232 or USB?

If there are other methods which may be simpler to implement and there is no easy way to overcome the problem that I am having with libusb, then I would have no problem in changing as I'm not to knowlegable in this area.

Thanks

Try the serial proxy http://www.arduino.cc/en/Main/Software

Nicolas

You should be able to ignore the fact the connection is USB-based if you have the correct device drivers installed (assuming you're using an Arduino with a FTDI chip).

In this case you should be able to access the device as a standard serial port (e.g. /dev/... on unix-ish, COM1 etc on Windowsish).

--Phil.

The arduino is a serial device that just happens to have its own USB/RS232 adapter built in. Ignore the USB thing, and write your code to talk to a serial (aka COM) port.

-j

Yes I am using an arduino duemilanova with an FDTI chip. Please if you have time explain to me a little regarding what drivers I need to use and what language etc. I am using windows xp.

I was reading the section about interfacing with software Arduino Playground - InterfacingWithSoftware but I ended up with more questions after reading it. I am bit knew to this side of things.

I´d be very grateful if some one could just point me in the right direction. I am more than happy to begin studying something if I know it´s possible to use the method. It´s just that now with my limited knowledge I find it difficult to get stuck into a certain topic when I don´t understand the basics of it.

Please if you have time explain to me a little regarding what drivers I need to use and what language etc. I am using windows xp.

Have you installed the "arduino" drivers, and can you download a program to the arduino?

If so, you are done - all the drivers are present and accounted for.

Sorry, but I can't offer any advice on Windows code to talk to a serial port - I don't do windows. I can guarantee it will be easier than trying to interface a usb device with libusb, though.

-j

Thank you for the responses.
Yes I have downloaded the drivers and installed them from the ftdi site. I can download and run programs on the board without problems.

If so, you are done - all the drivers are present and accounted for.

You make it sound easy!
I'd be very grateful if someone could give me some advice on the next step from here using windows. In the meantime I am going to dig out some old serial port code I had and see what I can do.

Thanks

I'd be very grateful if someone could give me some advice on the next step from here using windows.

Which language/toolset are you using on windows? There are several options. The page you linked has examples for several languages (maybe too many :slight_smile: ).

If you have done serial port interfacing in the past, maybe looking at your old code is the best place to start.

-j

Hi again kg4wsv,
It seems it was as easy as you made it sound!!!!
I had a look at my old code I used a few years ago while messing around with serials ports and after a few modifications it seems to be working.
So thank you all very much for sending me in the right direction. I would still be here come next Chrristmas using the libusb approach!

Incidentally I am using C, originally I was using Dev-C++ but it doesn't support the conio.h librray so I reverted to TurboC (from my college days) and with it I can use the same functions.

Does anyone know if there is a more modern free compiler which would support the conio library?

Anyway, thank you all so much for your suggestions, I am so grateful really.

(EDIT: I'll post my code here once I finish modyfing it for anyone in the same boat as me)

did you try tod code
http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino/

Nicolas

the todbot code above should work in dev-c++ since it uses termios for the serial port. Once you open() the port, it's easy to read() and write(). If you want to use conio, google turned up a few results of people modifying dev-c++ to use it.

If you want to use the windows libraries, you could download visual c++ express (for free). But i have no idea which windows libraries manage serial port communication.

Hi again
As promised here is my code. I put it here in the hope that it may save someone from hours of googling and may send them in the right direction. The code is nothing special but hopefully it will help someone get started. Basically all it does is send a chaaracter, wait and read a response.

#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define PORT1 0x2E8                  //Com4 base address

void main(void)
{
      int c;
      int i, j=0, init;

      char Command = 'H';
      char ch;

      outp(PORT1 + 1, 0);                  //Configure the serial port 9600 baud
      outp(PORT1 + 3, 0x80);
      outp(PORT1 + 0, 0x0C);
      outp(PORT1 + 1, 0x00);
      outp(PORT1 + 3, 0x03);
      outp(PORT1 + 2, 0xC7);
      outp(PORT1 + 4, 0x0B);

      outp(PORT1, Command);                  //Send a character to the port

      delay(5000);                              //Delay to allow for response

      c=inp(PORT1 + 5);                        //Check t see if anything has arrived
      if (c & 1)
      {
            ch=inp(PORT1);                        //Read in the character and print it
             printf("%c", ch);
      }      
}

Thanks for taking the time to contribute this code back. :slight_smile:

--Phil.