title: how to run at command through chrome serial api
body: <p>i want to run at command at my hardware thorugh crome serial api</p>
  
  <p>I use this Opensource code 
  <a href="https://github.com/GoogleChrome/chrome-app-samples/tree/master/servo" rel="nofollow">https://github.com/GoogleChrome/chrome-app-samples/tree/master/servo</a>
  this working only for only integer</p>
  
  <p>i want to pass string in the serial port my code is like following</p>
  
  <pre><code> var connectionId = -1;
  
  function setPosition(position) {
    var buffer = new ArrayBuffer(1);
    var uint8View = new Uint8Array(buffer);
    uint8View[0] = '0'.charCodeAt(0) + position;
  
    chrome.serial.write(connectionId, buffer, function() {});
  };
  function setTxt(data) {
   // document.getElementById('txt').innerHTML = data;
    var bf = str2ab(data)
    chrome.serial.write(connectionId, bf, function() {});
  };
  function str2ab(str) {
    len = str.length;
    var buf = new ArrayBuffer(len*2); // 2 bytes for each char
    var bufView = new Uint16Array(buf);
    for (var i=0, strLen=str.length; i&lt;strLen; i++) {
      bufView[i] = str.charCodeAt(i);
    }
  
    return buf;
  }
  function onRead(readInfo) {
    var uint8View = new Uint8Array(readInfo.data);
   var value1 = String.fromCharCode(uint8View[0])
    var value = uint8View[0] - '0'.charCodeAt(0);
    var rotation = value * 18.0;
   // var dataarr = String.fromCharCode.apply(null, new Uint16Array(readInfo.data));
    //alert(rotation);
    if(uint8View[0])
    document.getElementById('txt').innerHTML = document.getElementById('txt').innerHTML + value1;
    document.getElementById('image').style.webkitTransform =
      'rotateZ(' + rotation + 'deg)';
  //  document.getElementById('txt').innerHTML=uint8View[0];
    // Keep on reading.
    chrome.serial.read(connectionId, 1, onRead);
  };
  function ab2str(buf) {
    return String.fromCharCode.apply(null, new Uint16Array(buf));
  }
  
  function onOpen(openInfo) {
    connectionId = openInfo.connectionId;
    if (connectionId == -1) {
      setStatus('Could not open');
      return;
    }
    setStatus('Connected');
  
    setPosition(0);
    chrome.serial.read(connectionId, 1, onRead);
  };
  
  function setStatus(status) {
    document.getElementById('status').innerText = status;
  }
  
  function buildPortPicker(ports) {
    var eligiblePorts = ports.filter(function(port) {
      return !port.match(/[Bb]luetooth/);
    });
  
    var portPicker = document.getElementById('port-picker');
    eligiblePorts.forEach(function(port) {
      var portOption = document.createElement('option');
      portOption.value = portOption.innerText = port;
      portPicker.appendChild(portOption);
    });
  
    portPicker.onchange = function() {
      if (connectionId != -1) {
        chrome.serial.close(connectionId, openSelectedPort);
        return;
      }
      openSelectedPort();
    };
  }
  
  function openSelectedPort() {
    var portPicker = document.getElementById('port-picker');
    var selectedPort = portPicker.options[portPicker.selectedIndex].value;
    chrome.serial.open(selectedPort, onOpen);
  }
  
  onload = function() {
    var tv = document.getElementById('tv');
    navigator.webkitGetUserMedia(
        {video: true},
        function(stream) {
          tv.classList.add('working');
          document.getElementById('camera-output').src =
              webkitURL.createObjectURL(stream);
        },
        function() {
          tv.classList.add('broken');
        });
  
    document.getElementById('position-input').onchange = function() {
      setPosition(parseInt(this.value, 10));
    };
    document.getElementById('txt-input').onchange = function() {
  
      setTxt(this.value);
     // document.getElementById('txt').innerHTML = this.value;
    };
    chrome.serial.getPorts(function(ports) {
      buildPortPicker(ports)
      openSelectedPort();
    });
  };
  </code></pre>
  
  <p>string is the passing through serial but this command not run without enter press how to do it any one know </p>
  
  <p>thanks in advance :)</p>