Last active
February 23, 2016 13:55
-
-
Save oqu/354aecb973f6bf5c0efe to your computer and use it in GitHub Desktop.
Dobot - first try
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by olipion on 2/8/16. | |
* My first shot at reverse engineering java application. | |
*/ | |
var SerialPort = require("serialport").SerialPort; | |
var serialPort; | |
/** | |
* Use a Uint8Array as an input and create a float array containing nb float starting at offset. | |
* This use a reverse byte array. most likely because arduino and this computer does not share the same endianess. | |
* @param inArray {Uint8Array} array of byte to parse | |
* @param nb {Integer} number of entries | |
* @param offset {Integer} number of byte to skip | |
* @returns {Array} output : array of float | |
*/ | |
var getFloatArrayFromUInt8ArrayReverse = function(inArray, nb, offset) { | |
var fArray = []; | |
var offset = (offset === undefined) ? 0 : offset; | |
var tmpArray = new Uint8Array(4); | |
var dataView = new DataView(tmpArray.buffer); | |
for (var i = 0;i < nb;i++) { | |
var offsetInArray = i * 4 + offset; | |
tmpArray[0] = inArray[offsetInArray + 3]; | |
tmpArray[1] = inArray[offsetInArray + 2]; | |
tmpArray[2] = inArray[offsetInArray + 1]; | |
tmpArray[3] = inArray[offsetInArray + 0]; | |
fArray.push(dataView.getFloat32(0)); | |
} | |
return fArray; | |
} | |
/** | |
* Use a byte array and translate it into a string | |
* @param hex | |
* @returns {Array} | |
*/ | |
function bytesToHex(bytes) { | |
for (var hex = [], i = 0; i < bytes.length; i++) { | |
hex.push((bytes[i] >>> 4).toString(16)); | |
hex.push((bytes[i] & 0xF).toString(16)); | |
} | |
return hex.join(""); | |
} | |
/** | |
* Send begin package to a specific port | |
* @param port {SerialPort} port to use | |
*/ | |
function sendBeginPackage(port) { | |
var beginPackage = [0xa5,0x00, 0x00,0x11,0x11,0x22, 0x22,0x33,0x33,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, | |
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, | |
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x5a]; | |
port.write(beginPackage,function() { | |
console.log("initial data written"); | |
}) | |
} | |
var connectToDobot = function(comName) { | |
console.log("Create serial link ...."); | |
serialPort = new SerialPort(comName, { | |
baudRate: 256000 | |
}); | |
serialPort.on("open", function () { | |
console.log('serial port is opened'); | |
sendBeginPackage(serialPort); | |
}); | |
serialPort.on('data', function(data) { | |
console.log('Received ' + data.length); | |
if (data.length == 38) { | |
console.log(bytesToHex(data)); | |
var floatArray = getFloatArrayFromUInt8ArrayReverse(data,9,1); | |
console.log(Array.apply([], floatArray).join(" ")); | |
} | |
//console.log(bytesToHex(data)); | |
}); | |
serialPort.on('error', function() { | |
console.log('serial cannot be opened.'); | |
}); | |
} | |
var serialPort = require("serialport"); | |
serialPort.list(function (err, ports) { | |
ports.forEach(function(port) { | |
if (port.comName === "/dev/cu.BT4-SPP-SerialPort") | |
{ | |
console.log("Found BT4 board. try to connect to it."); | |
connectToDobot(port.comName); | |
} | |
}); | |
}); | |
var timer = setInterval(function(){ | |
console.log("wait ..."); | |
},1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment