Discuss Scratch

PullJosh
Scratcher
1000+ posts

Connecting to NXT (hardware)

Disclaimer: Hardware is not my strong suit. I have no idea what I'm doing and am pretty much just learning as I go.

I'm trying to create a hardware extension for interfacing with the the Lego Mindstorms NXT (there's already one available for the newer EV3, but I want to create an extension for communicating with my older NXT device).

After looking through the documentation, I added this line to my extension:
ScratchExtensions.register('NXT', descriptor, ext, {type: 'hid', vendor:0x0694, product:0x0002});
(I also added many other functions which seem to be needed for hardware support. The full code is available at the end of the post.)

The “vendor” and “product” values came from this information provided by Windows:
(I've added arrows pointing towards where I found the info.)
Device USB\VID_0694&PID_0002\0016531001F1 was configured.

Driver Name: oem27.inf
Class Guid: {A3330EDF-239D-4206-833B-1D58952613D5}
Driver Date: 02/21/2007
Driver Version: 4.1.1.0
Driver Provider: LEGO
Driver Section: WinUsb_Inst
Driver Rank: 0xFF0001
>>> Matching Device Id: USB\VID_0694&PID_0002
Outranked Drivers:
Device Updated: false

My expectation was that, after plugging in and turning on my NXT, the extension circle would turn green. However, it remained yellow.

The next thing I tried was using “vendor:0x02B6, product:0x0002”, which is the hexadecimal version of the numbers above. That didn't work either.

What am I doing wrong, and how can I fix it?

Full Code:
(function() {
    var device = null;
    var input = null;
    var poller = null;
    var ext = this;
    ext._shutdown = function() {
        if(poller) clearInterval(poller);
        poller = null;
        if(device) device.close();
        device = null;
    }
    ext._getStatus = function() {
        if(!device) return {status: 1, msg: 'Controller disconnected'};
        return {status: 2, msg: 'Controller connected'};
    }
    ext._deviceConnected = function(dev) {
        if(device) return;
        device = dev;
        device.open();
        poller = setInterval(function() {
            input = device.read(48);
        }, 10);
        setInterval(function() { console.log(input); }, 100);
    };
    ext._deviceRemoved = function(dev) {
        if(device != dev) return;
        device = null;
        stopPolling();
    };
    function stopPolling() {
        if(poller) clearInterval(poller);
        poller = null;
    }
    function convertByteStr(byte) { return (parseInt(byte, 16) - 128) / 128; }
    ext.getInput = function() {
        return input || "No input avilable.";
    }
    var descriptor = {
        blocks: [
            ['r', 'get NXT input data', 'getInput']
        ]
    };
    ScratchExtensions.register('NXT', descriptor, ext, {type: 'hid', vendor:0x02B6, product:0x0002});
})({});
Jonathan50
Scratcher
1000+ posts

Connecting to NXT (hardware)

The vendor ID is right because that's what the WEDO extension uses (but in decimal), and the example in the documentation, so it's probably LEGO's id. I have no idea

Last edited by Jonathan50 (Aug. 7, 2016 05:10:04)


Not yet a Knight of the Mu Calculus.

Powered by DjangoBB