# Reading data from usb device...



## osx-addict (Mar 3, 2010)

Hi all.. I'm trying my hand at reading data from a USB device that collects
solar data from a pair of Fronius inverters.. If I plug this data logger box
in, I get the following from FreeBSD :


```
Mar  2 23:05:21 srv1 root: Unknown USB device: vendor 0x101e product 0x0005 bus uhub0
Mar  2 23:05:21 srv1 kernel: ugen0: <Fronius Datalogger, class 0/0, rev 1.10/2.00, addr 3> on uhub0
```

I found some code that uses libusb to read/write to devices and tailored it slightly for this device.. 

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

usb_dev_handle *locate_fronius(void);

int main (int argc,char **argv)
{
   struct usb_dev_handle *fr_handle;
   struct usb_device *fr_device;
   int send_status;
   int open_status;
   unsigned char send_data=0xff;
   unsigned char receive_data=0;

   usb_init();
   usb_set_debug(2);
   if ((fr_handle = locate_fronius())==0) 
   {
      printf("Could not open the Fronius device\n");
      return (-1);
   }  

   open_status = usb_set_configuration(fr_handle,1);
   printf("conf_stat=%d\n",open_status);
   
   open_status = usb_claim_interface(fr_handle,0);
   printf("claim_stat=%d\n",open_status);
   
   open_status = usb_set_altinterface(fr_handle,0);
   printf("alt_stat=%d\n",open_status);

/*   send_status=usb_bulk_write(fr_handle,4,&send_data,1,500);
   printf("TX stat=%d\n",send_status);*/
   
   usb_bulk_read(fr_handle,3,&receive_data,1,500);   
   printf("RX stat=%d -> RX char=%d\n",send_status,receive_data);

   usb_close(fr_handle);
}  

usb_dev_handle *locate_fronius(void) 
{
   unsigned char located = 0;
   struct usb_bus *bus;
   struct usb_device *dev;
   usb_dev_handle *device_handle = 0;
      
   usb_find_busses();
   usb_find_devices();
 
   for (bus = usb_busses; bus; bus = bus->next)
   {
      for (dev = bus->devices; dev; dev = dev->next)  
      {
         if (dev->descriptor.idVendor == 0x101e) 
         {  
            located++;
            device_handle = usb_open(dev);
            printf("Fronius Device Found @ Address %s \n", dev->filename);
            printf("Fronius Vendor  ID 0x0%x\n",dev->descriptor.idVendor);
            printf("Fronius Product ID 0x0%x\n",dev->descriptor.idProduct);
         }
         else printf("** usb device %s found **\n", dev->filename);        
      }  
  }

  if (device_handle==0) return (0);
  else return (device_handle);   
}
```

In running that code I get the following output :


```
srv1# ./usb
usb_set_debug: Setting debugging level to 2 (on)
usb_os_find_busses: Found /dev/usb0
usb_os_find_devices: Found /dev/ugen0 on /dev/usb0
USB error: error sending control message: Operation not permitted
Unable to get descriptor (-1)
Fronius Device Found @ Address /dev/ugen0 
Fronius Vendor  ID 0x0101e
Fronius Product ID 0x05
USB error: could not set config 1: Operation not permitted
conf_stat=-1
claim_stat=0
USB error: could not set alt intf 0/0: Operation not permitted
alt_stat=-1
USB error: can't open /dev/ugen0.3 for bulk read: No such file or directory
usb_bulk_read: got negative open file descriptor for endpoint 3
RX stat=-1077941032 -> RX char=0
```

I'm not sure what to make of the various operation not permitted issues but the show stopper is the can't open /dev/ugen0.3 device file.. Below is what I've got in my /dev directory for usb devices :


```
srv1# ls -la /dev/ugen*
crw-r--r--  1 root  operator    0,  98 Feb  7 15:37 /dev/ugen0
crw-r--r--  1 root  operator    0,  99 Feb  7 15:37 /dev/ugen0.1
crw-r--r--  1 root  operator    0, 100 Feb  7 15:37 /dev/ugen0.2
```

Do I need to create the next character device to get the missing /dev/ugen0.3?  If so, is that with mknod, and what options?  Thx!


----------



## osx-addict (Mar 3, 2010)

I forgot this :


```
FreeBSD srv1.mydomain.com 7.1-RELEASE FreeBSD 7.1-RELEASE #0: Thu Jan  1 14:37:25 UTC 2009     root@logan.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
```


----------

