How to read data through USB


22 posts
by sh4rif » Fri Jan 25, 2013 9:26 pm
Hello everyone

I was wondering if I can find any helpful materials or tutorials on how to read data and transfer data from one device to another Raspberry PI device using USB ports. In short it would be reading data through USB.

Please help me with this I would really appreciate your help


Many thanks

Usman
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by JF002 » Fri Jan 25, 2013 9:50 pm
You can use libusb to communicate with another USB device. Libusb encapsulates everything you need (descriptor, configuratin, device, pipe,...).
If you are familiar with Python, PyUSB is great!

Then, you have to know a little about the device you want to communicate with: what do you need to send to the device, what does it send to you, on which pipe,...

But before that, you'll need some good knownledge about the USB protocol, it will help you to understand what you are doing ;)

I don't have any specific link to give to you right now, but search these keywords on the web, you should find interesting information!
My blog [FR] : http://blog.slashome.fr.cr
Posts: 80
Joined: Sat Feb 04, 2012 8:49 am
by sh4rif » Fri Jan 25, 2013 10:25 pm
Thank you very much "JF002".... you've pointed me out towards very interesting topics....

1... PyUSB
2... USB Protocols

lemme get more information on that then i will come back to that topic again if i need any more help

thanks once again

Usman
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by JF002 » Fri Jan 25, 2013 10:48 pm
The first thing you should look at if you have no knowledge in USB at all, is how USB works... It is a bit more complicated than the good old RS232.

Then, if you have some programming skills, use libUSB if you know C, PyUSB if you know Python,...

I managed to communicate between the RaspberryPi and a device from my work, by using an example from PyUSB. Once you know how USB works, PyUSB is quite straightforward to use.
My blog [FR] : http://blog.slashome.fr.cr
Posts: 80
Joined: Sat Feb 04, 2012 8:49 am
by sh4rif » Sat Jan 26, 2013 12:08 am
Hello JF002

When you say how USB works, what exactly you mean by that. Someone like me who has no experience in programming in USB ports will put me off from the track.

I found random results when I was searching on the web “how USB works”...
if you kindly point me or rather I should say narrow down the search topic on USB to any specific one please...
I just want to send and receive data to another device doesn’t matter which port or protocol or you suggest me which protocol or port i should use within USB....

Many thanks
Usman
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by ghans » Sat Jan 26, 2013 12:11 am
What exactly do you want do ?
Might be usbnet already be enough ? People have had
good success with it here on the forum.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4630
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by sh4rif » Sat Jan 26, 2013 12:21 am
ghans wrote:What exactly do you want do ?
Might be usbnet already be enough ? People have had
good success with it here on the forum.

ghans


Hello ghans
This is my final year project, where I have to move the solar panel with the sun's rotation. I mean solar panels will follow the path of sun. Raspberry pi would be the device which will control all this movement. There’s lot more to it... but this is just to let u know y i am interested in that.

i would really appreciate if u can help me to start off

Thanks
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by ghans » Sat Jan 26, 2013 8:07 am
And what device do you want to connect to the RasPi ?


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4630
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by MrBunsy » Sat Jan 26, 2013 12:34 pm
If your aim is to transfer data between Pis, would you not be better off with a more simple approach? You could link the Pi's UART ports, or use two USB->UART adapters, or even use a real network and communicate over that.
User avatar
Posts: 179
Joined: Mon Feb 20, 2012 1:48 pm
Location: Southampton, UK
by sh4rif » Sat Jan 26, 2013 9:15 pm
Hello folks
I got all the ideas but since this is not only my first ever embedded programming its my first ever raspberry pi programming.... so i want my job done within a minimum amount of time... because i only have mid of June this year to finish all my final year project... i have to split my time between my other modules and my final project... so if you kindly please advise me....

how to receive data and
how to send data back to that device

using USB ports, dosent matter if sending and receiving data direct to USB port or any other way... shortest and easiest.... and i have to do all the programming in Java as a requirement....


thanks
usman
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by Redrobes » Sat Jan 26, 2013 9:54 pm
What kind of course would require you to write embedded code in Java....

anyways, dont use USB, use the IO lines directly and avoid all the hassle of USB. Dump the data into some shift registers and use directly or drive a few lines into a microcontrollers which are hooked up to each of the servos of the solar rig. USB is well OTT for this.

If it absolutely has to be USB because of some other silly course requirement then hook into one of the (http://www.ftdichip.com/) chips which convert USB into some IO lines and come with loads of sample code to run it - no idea about Java.... you can use bit bang to get a few IO lines running and then shove that into the microcontroller. Bit bang is very slow but a raspi will handle slewing around some solar panels. Even a simple PIC 12C509 type would do - you only need 5V line and no other components to run one of them tho you do need a PIC programmer. I am sure your college / uni etc has one.

If you have to write the USB interface by hand then its a lot of learning. USB is well cool but well too high a spec protocol just to drive some motors. You would have to write a device driver or use a library.

BTW: this-> http://www.technobotsonline.com/ftdi-ba ... board.html
User avatar
Posts: 75
Joined: Mon Dec 26, 2011 9:19 pm
Location: S.W. UK
by xranby » Sun Jan 27, 2013 1:16 am
sh4rif wrote:Hello folks
I got all the ideas but since this is not only my first ever embedded programming its my first ever raspberry pi programming.... so i want my job done within a minimum amount of time... because i only have mid of June this year to finish all my final year project... i have to split my time between my other modules and my final project... so if you kindly please advise me....

how to receive data and
how to send data back to that device

using USB ports, dosent matter if sending and receiving data direct to USB port or any other way... shortest and easiest.... and i have to do all the programming in Java as a requirement....


thanks
usman


First you need to find out which kind of protocol is in use by your USB device. Try connect your device to the raspberry pi and then type dmesg to see what the Linux kernel makes out of it. The USB devices usually fall into one of the following categories.

* a generic HID device
simply read and write, like from a text file, from the linux specific HIDRAW device files in
/dev/hidraw* http://www.mjmwired.net/kernel/Document ... hidraw.txt
these USB devices can also be interfaced by calling libhid or libusb directly using JNI from Java

* a HID device with known protocol ( mouse, keyboard, joypad )
the simplest is to read the events, like a text file, from the linux specific /dev/input/event* files
http://www.kernel.org/doc/Documentation/input/input.txt
or use libhid/libusb directly using JNI from Java

* a USB device with known linux kernel driver like a printer or serial port.
for example a thermal labelprinter can be easily directly controlled from Java by writing to the
/dev/lp0 printer file http://labb.zafena.se/?category_name=gplable this project runs fine on the raspberry pi if you have the usblp kernel module loaded.
many devices ends up as a serial port, use the java rtxt library to talk to them or run a serial port to tcp/ip socket daemon like ser2net and then talk to the serial port by opening a network socket.

* a USB device with un-known linux kernel driver
these devices can only be interfaced by calling libusb using JNI from Java

If you need direct USB device access then I highly recommend you to issue calls using libUSB or libHID from Java using JNI, thats why I mentioned it three times already. It enable multi enpoint communication using custom data packet lenghts.
JNI Java Native Interface is the only way to get the Java JVM to connect to C/C++ libraries like libUSB and libHID.
Writing your own Java JNI to interface libUSB is a quite small task because the libUSB interface is quite slim but still it can be error prone.
You can use libraries like Java Native Access, JNA (internally using JNI and libffi) to create the C/C++ library binding from within java itself.
You may also look at using Gluegen that can more or less write the JNI code for you if you supply it a config under which class name you want libusb and its functions to appear under on the Java side.

There do exist some libraries like jUSB but they have not been maintained for years. Gentoo have this package in its portage system. To my knowledge jusb is not packaged by debian but some debian users have got it to work.
http://jusb.sourceforge.net/
http://ftp.fi.debian.org/gentoo-portage/dev-java/jusb/ <- some gentoo patches for jusb is found here
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby
User avatar
Posts: 401
Joined: Sat Mar 03, 2012 10:02 pm
by xranby » Sun Jan 27, 2013 1:53 am
I forgot to add some resources:
http://www.libusb.org/ <- libUSB manuals and usage
https://github.com/trygvis/javax-usb-libusb1 <- this source tree is a good JSR-80 API Java libUSB binding.
http://libhid.alioth.debian.org/ - libHID
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby
User avatar
Posts: 401
Joined: Sat Mar 03, 2012 10:02 pm
by ghans » Sun Jan 27, 2013 9:57 am
What device are you going to use anyway ??


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4630
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by sh4rif » Sun Jan 27, 2013 2:43 pm
Thank you "xranby"
for your effort i will connect my raspberry pi to that device and try find more information asap... and see what category that other device fall into...

and for "ghans"
the device raspberry pi would connect is not known to me because this is engineering department provocative i was only concern of my side... since i only wanted to know how to communicate between a device which is connected to raspberry pi via USB... i was not paying much attention to the other device but i will find out soon i think this is a board... not sure what kind of board....


and thanks once again xranby for your resources too... they look very helpful at a first glance... let me get into it tomorrow...

thanks folks one more time and please keep giving you helpful advises to me
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by ghans » Sun Jan 27, 2013 7:03 pm
IMHO it would be very important what device you're going
to connect to the Pi. Many Dev-Boards are exposed to high-level
languages and programming enviroments , and it would be not
productive to start to re-reate support from the lowest
level.



ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Posts: 4630
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany
by thradtke » Mon Jan 28, 2013 7:27 am
sh4rif wrote:[...] and i have to do all the programming in Java as a requirement....

Seriously, given that and keeping in mind its unsolved USB problems, I'd refrain from using the Pi. This appears to be a rather simple autotracking project where the key requirements don't match the Pi's strengths.
Full-time noob forever.
Posts: 432
Joined: Wed May 16, 2012 5:16 am
Location: Germany / EL
by sh4rif » Tue Jan 29, 2013 8:01 pm
Hello folks

I would connect my raspberry pi to a PIC board not sure the full specifications of that board though...

thanks
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by sh4rif » Tue Jan 29, 2013 8:11 pm
Hello "thradtke"

I understand where you are comming from........but I only explained this bit.... there much more to it which is not relevent at the moment... for instance.....a database storing all the information of electricity produced and been used and by cars.......and if its windy weather solar panle should place flat etc etc... much much more.... a front end screen to see all the information stored in database....i hope this would clead your doubts of why we are using Raspberry Pi for this project....

thanks

Usman
Posts: 8
Joined: Wed Nov 07, 2012 1:43 am
by MrBunsy » Tue Jan 29, 2013 8:36 pm
If it's some sort of PIC board you're connecting to, USB is almost certainly over-the-top. I've not used PICs myself (I'm an AVR fan) but I would be surprised/worried if it didn't have some sort of UART or SPI interface.

I've used Pi4J to very easily communicate over UART from the pi (example code: http://pi4j.com/example/serial.html )
User avatar
Posts: 179
Joined: Mon Feb 20, 2012 1:48 pm
Location: Southampton, UK
by thradtke » Wed Jan 30, 2013 8:21 am
sh4rif wrote:i hope this would clead your doubts of why we are using Raspberry Pi for this project....

How? Only a subset of the most common *consumer* USB devices work reliably on the Pi, and you still don't even know the device to connect to (and likely won't have a chance to change it). I wish you luck, you'll certainly need it.
Full-time noob forever.
Posts: 432
Joined: Wed May 16, 2012 5:16 am
Location: Germany / EL
by Himanshu S » Sun Oct 13, 2013 6:20 am
sh4rif wrote:Hello everyone

I was wondering if I can find any helpful materials or tutorials on how to read data and transfer data from one device to another Raspberry PI device using USB ports. In short it would be reading data through USB.

Please help me with this I would really appreciate your help


Many thanks

Usman



hi Usman, I am doing a project where I need to read data from usb port in raspi, the deveice connected to raspi is not so standard but an obd to usb converter adapter. would you please advise as you've done the same task of reading data from usb port. Kindly advise soon.
Posts: 1
Joined: Sat Oct 12, 2013 7:12 pm