Created
February 27, 2015 03:44
-
-
Save jacobrosenthal/bb61d2c2c06ec3d48b4d to your computer and use it in GitHub Desktop.
chrome.serial.update bug
This file contains hidden or 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
function convertArrayBufferToString(buf){ | |
var bufView = new Uint8Array(buf); | |
var encodedString = String.fromCharCode.apply(null, bufView); | |
return decodeURIComponent(encodedString); | |
} | |
var onReceiveCallback = function(info) { | |
console.log('received', convertArrayBufferToString(info.data)); | |
}; | |
//convertStringToArrayBuffer('hello') | |
var convertStringToArrayBuffer=function(str) { | |
var buf=new ArrayBuffer(str.length); | |
var bufView=new Uint8Array(buf); | |
for (var i=0; i<str.length; i++) { | |
bufView[i]=str.charCodeAt(i); | |
} | |
return buf; | |
}; | |
var onGetDevices = function(ports) { | |
for (var i=0; i<ports.length; i++) { | |
console.log(ports[i].path); | |
} | |
}; | |
console.log(chrome.serial.getDevices(onGetDevices)); | |
var onConnect = function(connectionInfo) { | |
console.log(chrome.runtime.lastError, connectionInfo); | |
chrome.serial.onReceive.addListener(onReceiveCallback); | |
var connectionId = connectionInfo.connectionId; | |
var buffer = new ArrayBuffer(1); | |
var dataView = new DataView(buffer); | |
dataView.setInt8(0, 0xaa); | |
chrome.serial.send(connectionId, buffer, function(){ | |
chrome.serial.update(connectionId, {bitrate: 9600}, function(result){ | |
console.log(chrome.runtime.lastError, result); | |
chrome.serial.send(connectionId, buffer, console.log.bind(console)); | |
}); | |
}); | |
}; | |
chrome.serial.connect('/dev/tty.usbserial-A700eTSd', {bitrate: 200}, onConnect); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment