Fork of avrgirl-arduino
Please use master branch of avrgirl-arduino for latest features and best compatibility.
A NodeJS library for flashing compiled sketch files to Arduino microcontroller boards.
For ATMega32u4 devices (onboard USB emulation) when connected as USD-HID or USB-MIDI device: The port name and/or ProductID can sometimes change during flashing. This patch attempts to fix the error, which would otherwise occur, by waiting for a new port that shows a ProductID which matches that of the target board.
Starting at line 144 in ./lib/connection.js
:
Connection.prototype._pollForOpen = function (callback) {
var _this = this;
var poll = awty(function (next) {
_this.serialPort.open(function (error) {
// Patch for port/PID changing error ///////////////////////////////////
if (error) {
Serialport.list().then(ports => {
console.log('Looking for new port...');
for (var curPort of ports) {
// Iterate through all available ports.
console.log(
'Device at:', curPort.path,
'has PID:', curPort.productId
);
for (var curTargetPID of _this.board.productId) {
// Iterate through all possible PIDs of the target board object.
let curTargetPID4 = String(curTargetPID.slice(2, 6));
// Convert 6-character hexidecimal into 4 character hex string.
let curPortPID = String(curPort.productId).toLowerCase();
// Force hex string case matching (needed for some OS)
console.log('Comparing with PID:', curTargetPID4);
if (curPortPID === curTargetPID4) {
// Does PID at the current port match the PID of the target board?
console.log(
'Found device:', _this.board.name,
'at port:', curPort.path,
'which currently has productID:', curPort.productId
);
_this.options.port = curPort.path;
// Use the current port.
break;
}
}
}
_this.serialPort = new Serialport(_this.options.port, {
baudRate: _this.board.baud,
autoOpen: false
});
});
}
// End of patch ////////////////////////////////////////////////////////
next(!error);
});
});
poll.every(200).ask(10);
poll(function (isOpen) {
var error;
if (!isOpen) {
error = new Error('could not open board on ' + _this.serialPort.path);
}
callback(error);
});
};
avrgirl-arduino author: Suz Hinton
Credit for a lot of the heavy lifting going on underneath in this library to: