LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sending, receiving through the USB interface (https://www.linuxquestions.org/questions/programming-9/sending-receiving-through-the-usb-interface-286393/)

The_Nerd 02-04-2005 08:23 PM

Sending, receiving through the USB interface
 
Ok. I need help bad. I am trying to make a program that will receive data through a USB port, and then retransmit it accross TCP/IP, so a client can talk with the device over TCP/IP.

I don't know anything about USB. So how do I talk to a USB device (say I want to get raw input from a keyboard... I know, I can do this simpler ways, but this is just an example, and the only USB device I have at the moment in which I can talk with) in an OS independant way. If there aren't any good libraries for USB that are OSI, I'll just write my code so it is OSI by using native Windows and Linux and Mac drivers.

I must do this. I must do it relitivly soon. I don't know even where to start. Please help me!!!

jschiwal 02-04-2005 09:41 PM

What you are asking about sounds like the usbnet driver.
http://www.linux-usb.org/usbnet/

jschiwal 02-04-2005 10:00 PM

Sorry, when I re-read your post, I realize I misunderstood what you wanted. If you are talking about a device such as the keyboard or the mouse, then you can do that using x-windows. For example, using the secure shell, you can forward x-windows events.

Also, remember the Unix design philosophy that everything is a file. So you can redirect the output of an audio device for example to a socket. However whatever you do should be going through the secure shell, or one or the other computer will be wide open.

This link should help some:
http://howtos.linux.com/guides/abs-guide/devref1.shtml

The_Nerd 02-07-2005 09:02 PM

What is wrong with this code?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "usb.h"

int main(int argc, char **argv)
{
        struct usb_bus *bus;
        int c, i, a, c1, i1;
        struct usb_bus *busses;
        char Bytes[256];
   
        usb_init();
        usb_find_busses();
        usb_find_devices();
   
        busses = usb_get_busses();

        for (bus = busses; bus; bus = bus->next)
        {
                struct usb_device *dev;
   
                for (dev = bus->devices; dev; dev = dev->next)
                {
                           
                        /* Loop through all of the configurations */
                        for (c = 0; c < dev->descriptor.bNumConfigurations; c++)
                        {
                                /* Loop through all of the interfaces */
                                for (i = 0; i < dev->config[c].bNumInterfaces; i++)
                                {
                                        /* Loop through all of the alternate settings */
                                        for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++)
                                        {
                                          if (dev->config[c].interface[i].altsetting[a].bInterfaceClass >= 0)
                                                {
                                                        printf("Device class = %d\n", dev->config[c].interface[i].altsetting[a].bInterfaceClass);
                                                        if (dev->descriptor.bDeviceClass >= 0)
                                                        {
                                                                    usb_dev_handle *Handle = usb_open(dev);
                                                                if (Handle)
                                                                {
                                                                        printf("Handle opened for device!\n");
                                                                        i1=usb_claim_interface(Handle, dev->config[c].interface[i].altsetting[a].bInterfaceNumber);
                                                                        if (i1>=0)
                                                                        {
                                                                                printf("Interface claimed!\n");
                                                                                while(1)
                                                                                {
                                                                                        c1 = usb_bulk_read(Handle, USB_ENDPOINT_TYPE_BULK, Bytes, 256, 32000);
                                                                                        if (c1<0)
                                                                                        {
                                                                                                printf("Failed device read\n");
                                                                                                break;
                                                                                        }
                                                                                        else
                                                                                                printf("%c", Bytes[0]);
                                                                                }
                                                                                usb_release_interface(Handle, dev->config[c].interface[i].altsetting[a].bInterfaceNumber);
                                                                        }
                                                                                else printf("Claiming interface failed!\n");
                                                                        usb_close(Handle);
                                                                }
                                                                else
                                                                {
                                                                        printf("Unable to open handle to device!\n");
                                                                }

                                                        }
                                                }
                                              }
                                }
                        }
                }
        }
}

When run, it goes through all the devices connected to the USB ports, and tries to open and read data from them. I know the code is messy, and rather, bad... but I am just trying to do some tests so I can better understand how to do USB programming. It fails at printf("Claiming interface failed!\n"); and quits. I tried skipping this step and just reading from the device. But this fails as well. What must I do to read, say, input from a keyboard?

jschiwal 02-10-2005 08:08 PM

Here is a site on a library you may want to use when it it released.

http://libusb.sourceforge.net/doc/index.html

Also, here is a Linux USB Programmers Guide that may help you.

Sorry I haven't supplied a specific answer to your question, but I'm not qualified enough to do that.

However, does your code run as root?

jschiwal 02-13-2005 02:49 AM

I didn't include a link in my previous post.

http://wwwbode.cs.tum.edu/Par/arch/usb/usbdoc/

This is a programmers guide from a University site.


All times are GMT -5. The time now is 12:53 AM.