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.