Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

libusb

Tue Jan 01, 2013 5:35 pm

I am trying to send commands to a microcontroller in the form of strings, but I am having trouble understanding Libusb's documantion. I know the microcontroller's id number and how to initilize the library but I cannot do anything beyond that.

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: libusb

Tue Jan 01, 2013 8:39 pm

update. between now and my last post I have gained enough information to start writing the code but I still have some questions. First the bulk transfer functions require a size and a timeout as parameters. I need recomendations for both. second will the device id work for usb_device. I have include what code I could put together. It has been a while since I have written c on this level so pardon my errors.

Code: Select all

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

int main(int argc,char**argv)
{struct *usb_device;
byte store;
byte recived;
usb_device=
usb_init();
usb_open(usb_device);
usb_claim_interface(usb_device,0);
while(recived!=22){
scan("%s",store);
usb_bulk_read(*usb_device,1,recived);
usb_bulk_write(*usb_device,1,store,);
printf("%s"recived);}
usb_release_interface(*usb_device,0);
usb_close(*usb_device);
}

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: libusb

Tue Jan 01, 2013 8:40 pm

did I say level. I mean using pointers and such.

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: libusb

Wed Jan 02, 2013 1:54 pm

this post is simply to transfer the following code to my pi

Code: Select all

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

int main(int argc,char**argv)
{struct *usb_device;
byte store;
byte recived;
usb_device=
usb_init();
usb_open(usb_device);
usb_claim_interface(usb_device,0);
while(recived!=22){
scan("%s",store);
usb_bulk_read(*usb_device,1,recived);
usb_bulk_write(*usb_device,1,store,);
printf("%s"recived);}
usb_release_interface(*usb_device,0);
usb_close(*usb_device);
}

Patrick222122
Posts: 80
Joined: Sun Dec 02, 2012 5:51 pm

Re: libusb

Wed Jan 02, 2013 4:53 pm

still having trouble but I think I am close. Would anyone mind look over the code and sending me a corrected version
#include <stdio.h>
#include <usb.h>

int main()
{struct usb_device *dev;
struct usb_device_handle *dev_handel
struct usb_bus*busses;
int *store;
int *recived;
usb_init();
usb_find_busses();
usb_find_devices();

busses=usb_get_busses();
for(bus=busses;bus;bus=bus->next){/*finding the device*/
structusb_device*dev;

for(dev=bus->devices;dev;dev=dev->next){
if(dev->descriptor.bDeviceClass==7){
dev_handel=usb_open(dev);/*opening the device*/
usb_claim_interface(dev_handel,0);
break;}
}
}
while(recived!=22){/*the code from here to the next comment is sending the data*/
scan("%i",store);
usb_bulk_read(dev,1,recived,1,1);
usb_bulk_write(usb_device,1,store,1,1);
printf("%i"recived);}/**/
usb_release_interface(dev_handel,0);/*closing the port*/
usb_close(dev);
return 0;
}
Also does anybody know the device class of microcontrollers?

Return to “Automation, sensing and robotics”