title: chrome extension - reading from serial port
body: <p>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.</p>
  
  <p>Running this code</p>
  
  <pre><code>var onGetDevices = function(ports) {
  
  for (var i=0; i&lt;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);
  </code></pre>
  
  <p>gets me "/dev/ttyUSB0" in the console, so it appears to be finding the device.</p>
  
  <p>How do I then connect to the device? I've included the serial.connect line above, with the following functions:</p>
  
  <pre><code>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 &amp;&amp; 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;
        }
    }
  };
  </code></pre>
  
  <p>but I get the following error:</p>
  
  <blockquote>
    <p>Error in response to serial.connect: ReferenceError: _this is not defined
        at Object.onGetDevices [as callback]</p>
  </blockquote>
  
  <p>I'm not sure exactly what I'm doing right or wrong here so any pointers appreciated.</p>