This is my first attempt at developing with a chrome app or extension. I have a GPS receiver on the USB port which is emulated as a serial device.
Running this code
var onGetDevices = function(ports) {
for (var i=0; i<ports.length; i++) {
// show me some output
console.log(ports[i].path);
// Connect to the serial port /dev/ttyUSB0
chrome.serial.connect(ports[i].path, {bitrate: 9600}, onConnect);
}
}
chrome.serial.getDevices(onGetDevices);
gets me "/dev/ttyUSB0" in the console, so it appears to be finding the device.
How do I then connect to the device? I've included the serial.connect line above, with the following functions:
var onConnect = function(connectionInfo) {
// The serial port has been opened. Save its id to use later.
_this.connectionId = connectionInfo.connectionId;
// Do whatever you need to do with the opened port.
chrome.serial.onReceive.addListener(onReceiveCallback);
}
var stringReceived = '';
var onReceiveCallback = function(info) {
if (info.connectionId == expectedConnectionId && info.data) {
var str = convertArrayBufferToString(info.data);
if (str.charAt(str.length-1) === '\n') {
stringReceived += str.substring(0, str.length-1);
onLineReceived(stringReceived);
stringReceived = '';
}
else {
stringReceived += str;
}
}
};
but I get the following error:
Error in response to serial.connect: ReferenceError: _this is not defined at Object.onGetDevices [as callback]
I'm not sure exactly what I'm doing right or wrong here so any pointers appreciated.
This is my first attempt at developing with a chrome app or extension. I have a GPS receiver on the USB port which is emulated as a serial device.
Running this code
var onGetDevices = function(ports) {
for (var i=0; i<ports.length; i++) {
// show me some output
console.log(ports[i].path);
// Connect to the serial port /dev/ttyUSB0
chrome.serial.connect(ports[i].path, {bitrate: 9600}, onConnect);
}
}
chrome.serial.getDevices(onGetDevices);
gets me "/dev/ttyUSB0" in the console, so it appears to be finding the device.
How do I then connect to the device? I've included the serial.connect line above, with the following functions:
var onConnect = function(connectionInfo) {
// The serial port has been opened. Save its id to use later.
_this.connectionId = connectionInfo.connectionId;
// Do whatever you need to do with the opened port.
chrome.serial.onReceive.addListener(onReceiveCallback);
}
var stringReceived = '';
var onReceiveCallback = function(info) {
if (info.connectionId == expectedConnectionId && info.data) {
var str = convertArrayBufferToString(info.data);
if (str.charAt(str.length-1) === '\n') {
stringReceived += str.substring(0, str.length-1);
onLineReceived(stringReceived);
stringReceived = '';
}
else {
stringReceived += str;
}
}
};
but I get the following error:
Error in response to serial.connect: ReferenceError: _this is not defined at Object.onGetDevices [as callback]
I'm not sure exactly what I'm doing right or wrong here so any pointers appreciated.
Share Improve this question edited Jul 28, 2014 at 16:28 Xan 77.6k18 gold badges197 silver badges217 bronze badges asked Jul 27, 2014 at 21:55 user2672288user2672288 3-
It looks like the variable
_this
isn't defined in your code. – Derek 朕會功夫 Commented Jul 28, 2014 at 9:46 - the code was taken directly from chromes tutorial example though, does it need defining, if so what as? link here developer.chrome./apps/app_serial – user2672288 Commented Jul 28, 2014 at 9:49
- this is great, good question – Shassain Commented Jun 24, 2018 at 0:21
1 Answer
Reset to default 4First the example does not work properly. Try this instead:
var connectionId;
$(document).ready(function() {
chrome.serial.getDevices(function(devices) {
for (var i = 0; i < devices.length; i++) {
$('select#portList').append('<option value="' + devices[i].path + '">' + devices[i].path + '</option>');
}
});
// ui hook
$('button#open').click(function() {
var clicks = $(this).data('clicks');
if (!clicks) {
var port = $('select#portList').val();
chrome.serial.connect(port, {bitrate: 9600}, function(info) {
connectionId = info.connectionId;
$("button#open").html("Close Port");
console.log('Connection opened with id: ' + connectionId + ', Bitrate: ' + info.bitrate);
});
} else {
chrome.serial.disconnect(connectionId, function(result) {
$("button#open").html("Open Port");
console.log('Connection with id: ' + connectionId + ' closed');
});
}
$(this).data("clicks", !clicks);
});
});
Now as for the actual reading of the input from the serial connection it will work but converting the ArrayBuffer to a string is a bit harder than expected.