Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from usb device using node-usb

I am trying to read data from a USB device "connected to my PC" using tessel/node-usb in my electron application but i am not able to read anything or getting any error ! i am using following js :

function connectdevice(vID, pId){
var device = usb.findByIds(vID, pId);
device.open();
var deviceINTF=device.interface(0);

if (deviceINTF.isKernelDriverActive())
    deviceINTF.detachKernelDriver();
deviceINTF.claim();


var ePs = deviceINTF.endpoints;
var epIN;
$.each( ePs, function( index, ep ){
    if(ep.direction=="in"){
        epIN=ep;
}
});
if(epIN){
    epIN.on('data', function (data) {
        alert("1"+data);
    });
    epIN.transferType = 2;
    alert("non empty port : "+epIN);
    epIN.transfer(64, function(error, data) {
        console.log(error, data); 
    });
    alert("after transfer");
}else{
    alert("unable to read .."); 
}
}
like image 679
k.elgohary Avatar asked Sep 15 '26 02:09

k.elgohary


1 Answers

The code has no issue but my device has 2 interfaces , and my code checking the first interface when i change this line code :

var deviceINTF=device.interface(0);

to :

var deviceINTF=device.interface(1);

It works !

Hope this help anybody.

like image 130
k.elgohary Avatar answered Sep 16 '26 16:09

k.elgohary