LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2005, 08:23 PM   #1
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
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!!!

Last edited by The_Nerd; 02-04-2005 at 08:24 PM.
 
Old 02-04-2005, 09:41 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
What you are asking about sounds like the usbnet driver.
http://www.linux-usb.org/usbnet/
 
Old 02-04-2005, 10:00 PM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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

Last edited by jschiwal; 02-04-2005 at 10:02 PM.
 
Old 02-07-2005, 09:02 PM   #4
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Original Poster
Rep: Reputation: 32
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?
 
Old 02-10-2005, 08:08 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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?
 
Old 02-13-2005, 02:49 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Divert Email While Receiving And Sending? kiranprashant Programming 2 09-14-2005 05:21 AM
How To Divert Email While Receiving And Sending? kiranprashant Linux - Newbie 2 09-13-2005 01:51 PM
having 2 Qmail probs - sending and receiving jaymer Linux - Software 1 06-23-2005 11:11 PM
Apparently working NICs receiving but not sending CestusGW Linux - Networking 8 11-02-2003 01:28 PM
Sending And Receiving Mail Using Pine Bheki Linux - Newbie 1 01-25-2002 07:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration