anze87
Posts: 9
Joined: Sun Jan 26, 2014 8:59 pm

How to read data from HID device?

Sun Jan 26, 2014 9:05 pm

I am new at Raspberry and Python, so I will be very glad if you help me at my problem. I need to read some data from weight scale when button on scale is pressed. When I connected this scale via USB on PC, I saw that it works as HID. When I press the button, data are written as string in some text edit document for example.
So, now I am going to connect this scale on Raspberry Pi to read and manipulate with data. Can you give me some code example, advice, which library I need to import? I want to write a script, that waits for incomming data and then something do with it. Thanks for help.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to read data from HID device?

Sun Jan 26, 2014 10:13 pm

What happens when you plug it into a Pi and press a button?

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: How to read data from HID device?

Sun Jan 26, 2014 10:32 pm

Hi,
anze87 wrote:data are written as string in some text edit document for example.
It seems that the weight scale represents itself as a standard HID keyboard.

In order to read its output, your software needs have focus and you read data 100% equally as when you type the values on your keyboard.


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

anze87
Posts: 9
Joined: Sun Jan 26, 2014 8:59 pm

Re: How to read data from HID device?

Tue Jan 28, 2014 4:33 pm

I finaly find time to moving forward. I try to read data, and it works just like from keyboard.
This works:

Code: Select all

buffer = input("new data")
I also try like this, but failed:

Code: Select all

f = open('/dev/hidraw0', 'rb')
buffer = f.read(8)
I don't know why incomming data are different in win8 and in Linux. For example, in Win I get string "G + 12.34 g" in Linux just "1234 ". When I read string data, I remove whitespaces with split() method, so I get just "1234". Because scale has fixed two decimal numbers, it's not a problem to divide with 100. But it becomes a problem, when result from weight is negative, and I can't know that. Does anybody know, what could be the reason for that? The result is the same, if I just open text editor and press button on scale, so I don't think it is problem in reading...

Thanks!

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: How to read data from HID device?

Tue Jan 28, 2014 6:26 pm

Usually when a weight comes up negative, it means that it is either in overload, there was a weight on the scale when it was powered up (tare weight) , or there is an error. Try power cycling the scale with nothing on it and see if the negative values go away.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

anze87
Posts: 9
Joined: Sun Jan 26, 2014 8:59 pm

Re: How to read data from HID device?

Tue Jan 28, 2014 7:15 pm

Yes, I understand that. But I can't figure it out why in Win is received data different than in my Raspberry. In Win I get sign (+) or (-) and decimal dot (.) and unit (g). In Raspberry I get just numbers and spaces after numbers, without anything. What would cause that, any ideas to check?

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: How to read data from HID device?

Tue Jan 28, 2014 7:48 pm

Hi,
anze87 wrote:What would cause that, any ideas to check?
Weird... I first thought about NumLock,... but this doesn't explain behavior well...
Do you have a chance to sniff USB communication ?


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

anze87
Posts: 9
Joined: Sun Jan 26, 2014 8:59 pm

Re: How to read data from HID device?

Tue Jan 28, 2014 9:09 pm

Do you mean with some kind of software? Isn't USB very complex protocol?

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: How to read data from HID device?

Tue Jan 28, 2014 9:41 pm

Hi,
anze87 wrote:Do you mean with some kind of software? Isn't USB very complex protocol?
There are also some software-only sniffers. Since I worked with hardware-based only so far, I cannot write much about these software tools... Anyway, check this: http://biot.com/blog/usb-sniffing-on-linux


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

anze87
Posts: 9
Joined: Sun Jan 26, 2014 8:59 pm

Re: How to read data from HID device?

Thu Jan 30, 2014 8:21 pm

It was very stupid problem. I had wrong keyboard emulation settings on scale. Now it works, as it should. Thanks for help!

LeeAlexander
Posts: 1
Joined: Thu Jun 25, 2015 8:49 am

Re: How to read data from HID device?

Thu Jun 25, 2015 8:51 am

I'm interested in using the Pi to read scales, could you tell me what scales you used?

Regards
Lee

ghp
Posts: 1518
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: How to read data from HID device?

Fri Jun 26, 2015 6:29 am

Hello,
some time ago I connected a barcode scanner to raspberry, using my python scratchClient software and scratch.
I wanted to avoid the problem, that HID input is accepted only when the program runs in foreground.
http://heppg.de/ikg/wordpress/?p=390
Although the target here is to connect this to scratch, the python code is straightforward and you find it in scratchClient/src/adapter/usbAdapter.py

Regards,
Gerhard

Return to “Python”