Skip to content

Instantly share code, notes, and snippets.

@erjiang
Last active April 20, 2023 23:26
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save erjiang/503896 to your computer and use it in GitHub Desktop.
Save erjiang/503896 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#
# Eric Jiang
# http://notes.ericjiang.com/posts/54
# This software is public domain.
#
# NOTE: This code is not maintained!
# There is a newer version written in C:
# https://github.com/erjiang/usbscale
#
use bytes;
my $data;
#prevents us from repeating messages
my $waitingflag = 0;
while (1) {
$data = `cat /dev/hidraw4 | head -c 7`;
my $report = ord(substr($data, 0, 1));
my $status = ord(substr($data, 1, 1));
my $unit = ord(substr($data, 2, 1));
my $exp = ord(substr($data, 3, 1));
my $lsb = ord(substr($data, 4, 1));
my $msb = ord(substr($data, 5, 1));
my $weight = ($msb * 256 + $lsb) / 10;
if($exp != 255 && $exp != 0) {
$weight ^= $exp;
}
#print "$report $status $unit $exp $weight\n";
if($report != 0x03) {
die "Error reading scale data!\n";
}
if($status == 0x01) {
die "Scale reports FAULT!\n";
} elsif ($status == 0x02 || $weight == 0) {
if($waitingflag != 0x02) {
print "Zero'd...\n";
$waitingflag = 0x02;
}
} elsif ($status == 0x03) {
if($waitingflag != 0x03) {
print "Weighing...\n";
$waitingflag = 0x03;
}
} elsif ($status == 0x04) {
my $unitName = "units";
if($unit == 11) {
$unitName = "ounces";
} elsif ($unit == 12) {
$unitName = "pounds";
}
print "$weight $unitName\n";
last;
} elsif ($status == 0x05) {
if($waitingflag != 0x05) {
print "Scale reports Under Zero...\n";
$waitingflag = 0x05;
}
} elsif ($status == 0x06) {
if($waitingflag != 0x06) {
print "Scale reports Over Weight!\n";
$waitingflag = 0x06;
}
} elsif ($status == 0x07) {
if($waitingflag != 0x07) {
print "Scale reports Calibration Needed!\n";
$waitingflag = 0x07;
}
} elsif ($status == 0x08) {
if($waitingflag != 0x08) {
print "Scale reports Re-zeroing Needed!\n";
$waitingflag = 0x08;
}
} else {
die "Unknown status code: $status\n";
}
}
@mattismyname
Copy link

FYI, I just upgraded to Ubuntu 10.10 and the script stopped working. A bit of poking and it seems that you can make it work by shifting the variable indices down by 1:

my $report = ord(substr($data, 0, 1));
my $status = ord(substr($data, 1, 1));
my $unit   = ord(substr($data, 2, 1));
my $exp    = ord(substr($data, 3, 1));
my $lsb    = ord(substr($data, 4, 1));
my $msb    = ord(substr($data, 5, 1));

@erjiang
Copy link
Author

erjiang commented Oct 12, 2010

@mattismyname My first reaction is that it doesn't make sense for the variable indices to be shifted in 10.10 unless the USB HID protocol changed or Perl suddenly switched to 1-based indices or something weird. I haven't tried 10.10 yet but I will double-check this script when I do.

@mattismyname
Copy link

Yes, it is weird. Maybe the hidraw kernel driver changed...?

@basilgohar
Copy link

I can confirm the problem + solution for Fedora 13, 64-bit (not sure if that matters). Shifting the indices down by one made it report the weights again.

@erjiang
Copy link
Author

erjiang commented Feb 14, 2011

The indices for the scale data have been shifted down, per mattismyname's investigation.

@basilgohar
Copy link

Thanks! I'll give this a try and get back with you.

@erjiang
Copy link
Author

erjiang commented Feb 14, 2011

I have just finished an early version of a C program that reads USB scales. Try it out and let me know how it goes. https://github.com/erjiang/usbscale

@sanmai
Copy link

sanmai commented Jun 6, 2014

Consider using unpack() instead of ord(substr()). Example here.

@joshuaer
Copy link

Hello, I am trying to use this script with a Metler Toledo PS60. I am able to get the script to talk with the scale, but it is reading the weight incorrectly. For example, a 4lb item will read as 255lb, how in the script can I possibly correct this?

thanks

@bhartvigsen
Copy link

Just a note, this can be adapted for Fairbanks scales by removing the line $weight ^= $exp; and changing / 10 to /100

@erjiang
Copy link
Author

erjiang commented Nov 20, 2019

@bhartvigsen Interesting find. Can you open an issue on https://github.com/erjiang/usbscale/issues with details about your scale? It'd be nice to add support for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment