Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to use Device::USB to bulk_write/read from usb device

by wulvrine (Friar)
on Aug 10, 2017 at 14:16 UTC ( [id://1197173]=perlquestion: print w/replies, xml ) Need Help??

wulvrine has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I am trying to connect to a USB device plugged into my Ubuntu linux system. The command set is a set of 64 bytes with the first byte as the command. Command 0x02 is a version cmd and doesn't require any other information so the rest of the bytes can be 0x00. From the documents Bulk interface 4 ep7=IN ep8=OUT. It should send info (64bytes) which will contain the version.

I am getting a -22 error when reading or writing and not sure what I am doing wrong. Any assistance is appreciated.

(Note: when printing the endpoint out address I am getting bEndpointAddress of 8), bulk_write needs (endpoint, bytes, timeout)

#!/usr/bin/perl use Device::USB; use Data::Dumper; $idV = 0x0432; # VendorID changed to protect the innocent $idP = 0x4321; # productID changed to protect the innocent my $usb = Device::USB->new(); my $dev = $usb->find_device($idV, $idP); if (!defined $dev) { print "Unable to find device \n"; exit; } $dev->open(); $driver = $dev->get_driver_np(0); if (defined $driver) { print "connected to driver ($driver), releasing\n"; $res = $dev->detach_kernel_driver_np(0); } $res = $dev->claim_interface(0); if ($res == 0) { print "device claimed\n"; } $dev->set_configuration(1); my $cfg = $dev->config()->[0]; my $inter = $cfg->interfaces()->[4]; #epi=endpoint in, epo=endpoint out if (!defined $inter) { print "inter not defined\n"; } my $epi = $inter->[0]->endpoints()->[0]; my $epo = $inter->[0]->endpoints()->[1]; # string of hex values in pairs i.e. 02 00 00, 64 entries # cmd string is 0x02 followed by all 0x00 $mybuff = '02000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +'; # convert string to bytestring my @hex = ($mybuff =~ /(..)/g); #grab pairs into @hex 02 00 etc my @dec = map { hex($_) } @dec; # convert to decimal my @byteArr = map { pack('C', $_) } @hex; # pack into bytearray my $bs = pack("b", @byteArr); #push back into a byte string format print "calling bulk_write\n"; $bWritten = $dev->bulk_write($epo, $bs, 5000); print "bWritten=($bWritten)\n"; my $readInfo; print "calling bulk_read\n"; $bRead = $dev->bulk_read($epi, $readInfo, 64, 5000); print "bRead=($bRead)\n"; print "readInfo=($readInfo)\n"; print "release interface 0\n"; $res = $dev->release_interface(0); print "claim release result = ($res)\n";
output:
connected to driver (snd-usb-audio), releasing device claimed calling bulk_write bWritten=(-22) calling bulk_read bRead=(-22) readInfo=() release interface 0 claim release result = (0)
I am seeing in syslog during this run, I am not sure if that means It did or did not properly claim it even through the claim response was ok
[2509634.708198] usb 2-1.7.7.2: usbfs: interface 0 claimed by usbfs wh +ile 'perl' sets config #1
Again thanks!
s&&VALKYRIE &&& print $_^q|!4 =+;' *|

Replies are listed 'Best First'.
Re: How to use Device::USB to bulk_write/read from usb device
by hippo (Bishop) on Aug 10, 2017 at 15:56 UTC

      Yes, I forgot to mention I was running it as root. Because of that, I didn't add a group and or change the fstab. Thanks though!

      s&&VALKYRIE &&& print $_^q|!4 =+;' *|

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1197173]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found