wulvrine has asked for the wisdom of the Perl Monks concerning the following question:
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)
output:#!/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";
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 okconnected 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)
Again thanks![2509634.708198] usb 2-1.7.7.2: usbfs: interface 0 claimed by usbfs wh +ile 'perl' sets config #1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to use Device::USB to bulk_write/read from usb device
by hippo (Archbishop) on Aug 10, 2017 at 15:56 UTC | |
by wulvrine (Friar) on Aug 11, 2017 at 11:24 UTC |