firmata / arduino

Firmata firmware for Arduino
GNU Lesser General Public License v2.1
1.54k stars 514 forks source link

Serial Port Dependency #372

Closed Qaoud closed 7 years ago

Qaoud commented 7 years ago

Hi Julian!

I have a problem with Firmata.js which is "Serialport dependency" i don't know what does it mean!! and i want to know if that code is right or not:

`/**

/**

var util = require('util'), events = require('events'), chrome = chrome || undefined, Encoder7Bit = require('./encoder7bit'), OneWireUtils = require('./onewireutils'), SerialPort = null;

try { if (typeof chrome !== "undefined" && chrome.serial) { SerialPort = require('browser-serialport').SerialPort; } else { SerialPort = require('serialport').SerialPort; } } catch (err) { SerialPort = null; }

if (SerialPort == null) { console.log("It looks like serialport didn't compile properly. This is a common problem and its fix is well documented here https://github.com/voodootikigod/node-serialport#to-install"); throw "Missing serialport dependency"; }

/**

var PIN_MODE = 0xF4, REPORT_DIGITAL = 0xD0, REPORT_ANALOG = 0xC0, DIGITAL_MESSAGE = 0x90, START_SYSEX = 0xF0, END_SYSEX = 0xF7, QUERY_FIRMWARE = 0x79, REPORT_VERSION = 0xF9, ANALOG_MESSAGE = 0xE0, CAPABILITY_QUERY = 0x6B, CAPABILITY_RESPONSE = 0x6C, PIN_STATE_QUERY = 0x6D, PIN_STATE_RESPONSE = 0x6E, ANALOG_MAPPING_QUERY = 0x69, ANALOG_MAPPING_RESPONSE = 0x6A, I2C_REQUEST = 0x76, I2C_REPLY = 0x77, I2C_CONFIG = 0x78, STRING_DATA = 0x71, SYSTEM_RESET = 0xFF, PULSE_OUT = 0x73, PULSE_IN = 0x74, SAMPLING_INTERVAL = 0x7A, STEPPER = 0x72, ONEWIRE_DATA = 0x73,

ONEWIRE_CONFIG_REQUEST = 0x41,
ONEWIRE_SEARCH_REQUEST = 0x40,
ONEWIRE_SEARCH_REPLY = 0x42,
ONEWIRE_SEARCH_ALARMS_REQUEST = 0x44,
ONEWIRE_SEARCH_ALARMS_REPLY = 0x45,
ONEWIRE_READ_REPLY = 0x43,
ONEWIRE_RESET_REQUEST_BIT = 0x01,
ONEWIRE_READ_REQUEST_BIT = 0x08,
ONEWIRE_DELAY_REQUEST_BIT = 0x10,
ONEWIRE_WRITE_REQUEST_BIT = 0x20,
ONEWIRE_WITHDATA_REQUEST_BITS = 0x3C;

/**

var MIDI_RESPONSE = {};

/**

MIDI_RESPONSE[REPORT_VERSION] = function (board) { board.version.major = board.currentBuffer[1]; board.version.minor = board.currentBuffer[2]; for (var i = 0; i < 16; i++) { board.sp.write(new Buffer([REPORT_DIGITAL | i, 1])); board.sp.write(new Buffer([REPORT_ANALOG | i, 1])); } board.emit('reportversion'); };

/**

MIDI_RESPONSE[ANALOG_MESSAGE] = function (board) { var value = board.currentBuffer[1] | (board.currentBuffer[2] << 7); var port = board.currentBuffer[0] & 0x0F; if (board.pins[board.analogPins[port]]) { board.pins[board.analogPins[port]].value = value; } board.emit('analog-read-' + port, value); board.emit('analog-read', { pin: port, value: value }); };

/**

MIDI_RESPONSE[DIGITAL_MESSAGE] = function (board) { var port = (board.currentBuffer[0] & 0x0F); var portValue = board.currentBuffer[1] | (board.currentBuffer[2] << 7); for (var i = 0; i < 8; i++) { var pinNumber = 8 * port + i; var pin = board.pins[pinNumber]; if (pin && (pin.mode === board.MODES.INPUT)) { pin.value = (portValue >> (i & 0x07)) & 0x01; board.emit('digital-read-' + pinNumber, pin.value); board.emit('digital-read', { pin: pinNumber, value: pin.value }); } } };

/**

var SYSEX_RESPONSE = {};

/**

SYSEX_RESPONSE[QUERY_FIRMWARE] = function (board) { var firmwareBuf = []; board.firmware.version = {}; board.firmware.version.major = board.currentBuffer[2]; board.firmware.version.minor = board.currentBuffer[3]; for (var i = 4, length = board.currentBuffer.length - 2; i < length; i += 2) { firmwareBuf.push((board.currentBuffer[i] & 0x7F) | ((board.currentBuffer[i + 1] & 0x7F) << 7)); }

board.firmware.name = new Buffer(firmwareBuf).toString('utf8', 0, firmwareBuf.length);
board.emit('queryfirmware');

};

/**

SYSEX_RESPONSE[CAPABILITY_RESPONSE] = function (board) { var supportedModes = 0;

function pushModes(modesArray, mode) {
    if (supportedModes & (1 << board.MODES[mode])) {
        modesArray.push(board.MODES[mode]);
    }
}

for (var i = 2, n = 0; i < board.currentBuffer.length - 1; i++) {
    if (board.currentBuffer[i] === 127) {
        var modesArray = [];
        Object.keys(board.MODES).forEach(pushModes.bind(null, modesArray));
        board.pins.push({
            supportedModes: modesArray,
            mode: board.MODES.UNKNOWN,
            value : 0,
            report: 1
        });
        supportedModes = 0;
        n = 0;
        continue;
    }
    if (n === 0) {
        supportedModes |= (1 << board.currentBuffer[i]);
    }
    n ^= 1;
}
board.emit('capability-query');

};

/**

SYSEX_RESPONSE[PIN_STATE_RESPONSE] = function (board) { var pin = board.currentBuffer[2]; board.pins[pin].mode = board.currentBuffer[3]; board.pins[pin].value = board.currentBuffer[4]; if (board.currentBuffer.length > 6) { board.pins[pin].value |= (board.currentBuffer[5] << 7); } if (board.currentBuffer.length > 7) { board.pins[pin].value |= (board.currentBuffer[6] << 14); } board.emit('pin-state-' + pin); };

/**

SYSEX_RESPONSE[ANALOG_MAPPING_RESPONSE] = function (board) { var pin = 0; var currentValue; for (var i = 2; i < board.currentBuffer.length - 1; i++) { currentValue = board.currentBuffer[i]; board.pins[pin].analogChannel = currentValue; if (currentValue !== 127) { board.analogPins.push(pin); } pin++; } board.emit('analog-mapping-query'); };

/**

SYSEX_RESPONSE[I2C_REPLY] = function (board) { var replyBuffer = []; var slaveAddress = (board.currentBuffer[2] & 0x7F) | ((board.currentBuffer[3] & 0x7F) << 7); var register = (board.currentBuffer[4] & 0x7F) | ((board.currentBuffer[5] & 0x7F) << 7); for (var i = 6, length = board.currentBuffer.length - 1; i < length; i += 2) { replyBuffer.push(board.currentBuffer[i] | (board.currentBuffer[i + 1] << 7)); } board.emit('I2C-reply-' + slaveAddress, replyBuffer); };

SYSEX_RESPONSE[ONEWIRE_DATA] = function(board) { var subCommand = board.currentBuffer[2];

if(!SYSEX_RESPONSE[subCommand]) {
    return;
}

SYSEX_RESPONSE[subCommand](board);

};

SYSEX_RESPONSE[ONEWIRE_SEARCH_REPLY] = function(board) { var pin = board.currentBuffer[3]; var replyBuffer = board.currentBuffer.slice(4, board.currentBuffer.length -1);

board.emit('1-wire-search-reply-' + pin, OneWireUtils.readDevices(replyBuffer));

};

SYSEX_RESPONSE[ONEWIRE_SEARCH_ALARMS_REPLY] = function(board) { var pin = board.currentBuffer[3]; var replyBuffer = board.currentBuffer.slice(4, board.currentBuffer.length -1);

board.emit('1-wire-search-alarms-reply-' + pin, OneWireUtils.readDevices(replyBuffer));

};

SYSEX_RESPONSE[ONEWIRE_READ_REPLY] = function(board) { var encoded = board.currentBuffer.slice(4, board.currentBuffer.length -1); var decoded = Encoder7Bit.from7BitArray(encoded); var correlationId = (decoded[1] << 8) | decoded[0];

board.emit('1-wire-read-reply-' + correlationId, decoded.slice(2));

};

/**

SYSEX_RESPONSE[STRING_DATA] = function (board) { var string = new Buffer(board.currentBuffer.slice(2, -1)).toString('utf8').replace(/\0/g, ''); board.emit('string', string); };

/**

SYSEX_RESPONSE[PULSE_IN] = function (board){ var pin = (board.currentBuffer[2] & 0x7F) | ((board.currentBuffer[3] & 0x7F) << 7); var durationBuffer = [ (board.currentBuffer[4] & 0x7F) | ((board.currentBuffer[5] & 0x7F) << 7), (board.currentBuffer[6] & 0x7F) | ((board.currentBuffer[7] & 0x7F) << 7), (board.currentBuffer[8] & 0x7F) | ((board.currentBuffer[9] & 0x7F) << 7), (board.currentBuffer[10] & 0x7F) | ((board.currentBuffer[11] & 0x7F) << 7) ]; var duration = ( (durationBuffer[0] << 24) + (durationBuffer[1] << 16) + (durationBuffer[2] << 8) + (durationBuffer[3] ) ); board.emit('pulse-in-'+pin,duration); };

/**

SYSEX_RESPONSE[STEPPER] = function (board) { var deviceNum = board.currentBuffer[2]; board.emit('stepper-done-'+deviceNum, true); };

/**

util.inherits(Board, events.EventEmitter);

/**

Board.prototype.reportVersion = function(callback) { this.once('reportversion', callback); this.sp.write(new Buffer([REPORT_VERSION])); };

/**

Board.prototype.queryFirmware = function (callback) { this.once('queryfirmware', callback); this.sp.write(new Buffer([START_SYSEX, QUERY_FIRMWARE, END_SYSEX])); };

/**

Board.prototype.analogRead = function (pin, callback) { this.addListener('analog-read-' + pin, callback); };

/**

Board.prototype.analogWrite = function (pin, value) { this.pins[pin].value = value; this.sp.write(new Buffer([ANALOG_MESSAGE | pin, value & 0x7F, (value >> 7) & 0x7F])); };

/**

Board.prototype.servoWrite = function (pin, value) { this.analogWrite.apply(this, arguments); };

/**

Board.prototype.pinMode = function (pin, mode) { this.pins[pin].mode = mode; this.sp.write(new Buffer([PIN_MODE, pin, mode])); };

/**

Board.prototype.digitalWrite = function (pin, value) { var port = Math.floor(pin / 8); var portValue = 0; this.pins[pin].value = value; for (var i = 0; i < 8; i++) { if (this.pins[8 * port + i].value) { portValue |= (1 << i); } } this.sp.write(new Buffer([DIGITAL_MESSAGE | port, portValue & 0x7F, (portValue >> 7) & 0x7F])); };

/**

Board.prototype.digitalRead = function (pin, callback) { this.addListener('digital-read-' + pin, callback); };

/**

Board.prototype.queryCapabilities = function(callback) { this.once('capability-query', callback); this.sp.write(new Buffer([START_SYSEX, CAPABILITY_QUERY, END_SYSEX])); };

/**

Board.prototype.queryAnalogMapping = function (callback) { this.once('analog-mapping-query', callback); this.sp.write(new Buffer([START_SYSEX, ANALOG_MAPPING_QUERY, END_SYSEX])); };

/**

Board.prototype.queryPinState = function (pin, callback) { this.once('pin-state-' + pin, callback); this.sp.write(new Buffer([START_SYSEX, PIN_STATE_QUERY, pin, END_SYSEX])); };

/**

Board.prototype.sendI2CConfig = function(delay){ delay = delay || 0; this.sp.write(new Buffer([START_SYSEX,I2C_CONFIG,(delay & 0xFF),((delay >> 8) & 0xFF),END_SYSEX])); };

/**

Board.prototype.sendString = function(string) { var bytes = new Buffer(string + '\0', 'utf8'); var data = []; data.push(START_SYSEX); data.push(STRING_DATA); for (var i = 0, length = bytes.length; i < length; i++) { data.push(bytes[i] & 0x7F); data.push((bytes[i] >> 7) & 0x7F); } data.push(END_SYSEX); this.sp.write(data); };

/**

Board.prototype.sendI2CWriteRequest = function (slaveAddress, bytes) { var data = []; bytes = bytes || []; data.push(START_SYSEX); data.push(I2C_REQUEST); data.push(slaveAddress); data.push(this.I2C_MODES.WRITE << 3); for (var i = 0, length = bytes.length; i < length; i++) { data.push(bytes[i] & 0x7F); data.push((bytes[i] >> 7) & 0x7F); } data.push(END_SYSEX); this.sp.write(new Buffer(data)); };

/**

Board.prototype.sendI2CReadRequest = function (slaveAddress, numBytes, callback) { this.sp.write(new Buffer([START_SYSEX, I2C_REQUEST, slaveAddress, this.I2C_MODES.READ << 3, numBytes & 0x7F, (numBytes >> 7) & 0x7F, END_SYSEX])); this.once('I2C-reply-' + slaveAddress, callback); };

/**

/**

/**

Board.prototype._sendOneWireSearch = function(type, event, pin, callback) { this.sp.write(new Buffer([START_SYSEX, ONEWIRE_DATA, type, pin, END_SYSEX]));

var searchTimeout = setTimeout(function() {
    callback(new Error("1-Wire device search timeout - are you running ConfigurableFirmata?"));
}, 5000);
this.once(event, function(devices) {
    clearTimeout(searchTimeout);

    callback(null, devices);
});

};

/**

/**

/**

/**

/**

// see http://firmata.org/wiki/Proposals#OneWire_Proposal Board.prototype._sendOneWireRequest = function(pin, subcommand, device, numBytesToRead, correlationId, delay, dataToWrite, event, callback) { var bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

if(device || numBytesToRead || correlationId || delay || dataToWrite) {
    subcommand = subcommand | ONEWIRE_WITHDATA_REQUEST_BITS;
}

if(device) {
    bytes.splice.apply(bytes, [0, 8].concat(device));
}

if(numBytesToRead) {
    bytes[8] = numBytesToRead & 0xFF;
    bytes[9] = (numBytesToRead >> 8) & 0xFF;
}

if(correlationId) {
    bytes[10] = correlationId & 0xFF;
    bytes[11] = (correlationId >> 8) & 0xFF;
}

if(delay) {
    bytes[12] = delay & 0xFF;
    bytes[13] = (delay >> 8) & 0xFF;
    bytes[14] = (delay >> 16) & 0xFF;
    bytes[15] = (delay >> 24) & 0xFF;
}

if(dataToWrite) {
    dataToWrite.forEach(function(byte) {
        bytes.push(byte);
    });
}

var output = [START_SYSEX, ONEWIRE_DATA, subcommand, pin];
output = output.concat(Encoder7Bit.to7BitArray(bytes));
output.push(END_SYSEX);

this.sp.write(new Buffer(output));

if(event && callback) {
    this.once(event, callback);
}

};

/**

Board.prototype.setSamplingInterval = function (interval) { var safeint = interval < 10 ? 10 : (interval > 65535 ? 65535 : interval); // constrained this.sp.write(new Buffer([START_SYSEX, SAMPLING_INTERVAL, (safeint & 0xFF),((safeint >> 8) & 0xFF), END_SYSEX])); };

/**

Board.prototype.reportAnalogPin = function (pin, value) { if(value === 0 || value === 1) { this.pins[this.analogPins[pin]].report = value; this.sp.write(new Buffer([REPORT_ANALOG | pin, value])); } };

/**

Board.prototype.reportDigitalPin = function (pin, value) { if(value === 0 || value === 1) { this.pins[pin].report = value; this.sp.write(new Buffer([REPORT_DIGITAL | pin, value])); } };

/* /

Board.prototype.pulseIn = function (opts, callback) { var pin = opts.pin; var value = opts.value; var pulseOut = opts.pulseOut || 0; var timeout = opts.timeout || 1000000; var pulseOutArray = [ ((pulseOut >> 24) & 0xFF), ((pulseOut >> 16) & 0xFF), ((pulseOut >> 8) & 0XFF), ((pulseOut & 0xFF)) ]; var timeoutArray = [ ((timeout >> 24) & 0xFF), ((timeout >> 16) & 0xFF), ((timeout >> 8) & 0XFF), ((timeout & 0xFF)) ]; var data = [ START_SYSEX, PULSE_IN, pin, value, pulseOutArray[0] & 0x7F, (pulseOutArray[0] >> 7) & 0x7F, pulseOutArray[1] & 0x7F, (pulseOutArray[1] >> 7) & 0x7F, pulseOutArray[2] & 0x7F, (pulseOutArray[2] >> 7) & 0x7F, pulseOutArray[3] & 0x7F, (pulseOutArray[3] >> 7) & 0x7F, timeoutArray[0] & 0x7F, (timeoutArray[0] >> 7) & 0x7F, timeoutArray[1] & 0x7F, (timeoutArray[1] >> 7) & 0x7F, timeoutArray[2] & 0x7F, (timeoutArray[2] >> 7) & 0x7F, timeoutArray[3] & 0x7F, (timeoutArray[3] >> 7) & 0x7F, END_SYSEX ]; this.sp.write(new Buffer(data)); this.once('pulse-in-' + pin,callback); };

/**

/**

Board.prototype.stepperConfig = function (deviceNum, type, stepsPerRev, dirOrMotor1Pin, stepOrMotor2Pin, motor3Pin, motor4Pin) { var data = [ START_SYSEX, STEPPER, 0x00, // STEPPER_CONFIG from firmware deviceNum, type, stepsPerRev & 0x7F, (stepsPerRev >> 7) & 0x7F, dirOrMotor1Pin, stepOrMotor2Pin ]; if(type === this.STEPPER.TYPE.FOUR_WIRE) { data.push(motor3Pin, motor4Pin); } data.push(END_SYSEX); this.sp.write(new Buffer(data)); };

/**

Board.prototype.stepperStep = function (deviceNum, direction, steps, speed, accel, decel, callback) { if (typeof accel === 'function') { callback = accel; accel = 0; decel = 0; }

var data = [
    START_SYSEX,
    STEPPER,
    0x01,       // STEPPER_STEP from firmware
    deviceNum,
    direction,  // one of this.STEPPER.DIRECTION.*
    steps & 0x7F,
    (steps >> 7) & 0x7F,
    (steps >> 14) & 0x7f,
    speed & 0x7F,
    (speed >> 7) & 0x7F
];
if(accel > 0 || decel > 0) {
    data.push(
        accel & 0x7F,
        (accel >> 7) & 0x7F,
        decel & 0x7F,
        (decel >> 7) & 0x7F
    );
}
data.push(END_SYSEX);
this.sp.write(new Buffer(data));
this.once('stepper-done-'+deviceNum, callback);

};

/**

Board.prototype.reset = function () { this.sp.write(new Buffer([SYSTEM_RESET])); };

module.exports = { Board: Board, SYSEX_RESPONSE: SYSEX_RESPONSE, MIDI_RESPONSE: MIDI_RESPONSE }; `

rwaldron commented 7 years ago

When you install this module via npm, npm will also install firmata.js's dependencies, which includes node-serialport. The code you've pasted is the entire source of firmata.js module, and I'm not sure why? Can you paste the code you have that's problematic?

Qaoud commented 7 years ago

i have a problem with serialport. i'm doing a project which is a robotic hand i can control it with my hand using LeapMotion controller and Arduino Uno and i'm using Node.js to the LeapMotion communicate with the Adruino. I have everything installed, but when i try to run the app that written in javascript a message appears saying issue throw serialport dependencies. I'll upload the files that i'm using to let you see it, and i hope you can help me. @rwaldron F29DBFRHUPHK4ZJ (1).zip

soundanalogous commented 7 years ago

@Qaoud did you install the StandardFirmata firmware on your Arduino?

Also, no one is going to download and open a zip posted to a public forum, please post your code to a Github gist.

Qaoud commented 7 years ago

@soundanalogous yes i did and i don't really know where's the problem because i tried other project also using arduino and leapmotion and it works, but with this one the one i need because it's my graduation project doesn't work!! and i don't know if the serial port is working or not!! this's the message i got when i run the program in Node.js: `It looks like serialport didn't compile properly. This is a common problem and its fix is well documented here https://github.com/voodootikigod/node-serialport#to-install

/Users/M.Qaoud/Desktop/Tesi/node_modules/johnny-five/node_modules/firmata/lib/firmata.js:28 throw "Missing serialport dependency"; ^ Missing serialport dependency`

soundanalogous commented 7 years ago

Try the following:

npm uninstall firmata
npm install serialport
npm install firmata
Qaoud commented 7 years ago

i did this but the same problem (serialport dependencies)!!

Qaoud commented 7 years ago

https://www.instructables.com/id/Robotic-Hand-controlled-by-Gesture-with-Arduino-Le/ this's the project that i'm trying to do! @soundanalogous

soundanalogous commented 7 years ago

This forum is for issues with the Firmata firmware that you install on an Arduino board, but the issue you are experiencing is with the node serialport module. Please browse the issues in the node-serialport repository and file an issue there if you don't find anything that helps you to resolve it: https://github.com/EmergingTechnologyAdvisors/node-serialport.

soundanalogous commented 7 years ago

Also try the node-serialport gitter channel: https://gitter.im/EmergingTechnologyAdvisors/node-serialport