Kalreg
Kalreg

Reputation: 1012

arduino + serial + chrome + send - cant send data via serial com

I am making a chrome packaged app using serial connection to send data to arduino, but app doesnt seem to send data to it. Here's what i've done and figured out.

I have a sketch on arduino that looks like this:

SKETCH:

#define led 13  // built-in LED

int ByteReceived;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  
}

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if (Serial.available() > 0)
  {
    ByteReceived = Serial.read();

    if(ByteReceived == '1')
    {
      digitalWrite(13, HIGH);
      Serial.print(" LED ON ");
    }

    if(ByteReceived == '0')
    {
      digitalWrite(13, LOW);
      Serial.print(" LED OFF");
    }

    Serial.println();    // End the line

  }
}

It simply turns on the led when i send "1" to arduino and turns it off when i send "0". Works perfectly with Arduino IDE Serial monitor.

Now let's look on chrome app:

manifest.json

{
   "app": {
      "background": {
         "scripts": [ "background.js" ]
      }
   },
   "description": "No description",
   "icons": {
      "256": "icon.png"
   },

   "manifest_version": 2,
   "name": "My App",
   "permissions": [ "serial", "fullscreen" ],

   "version": "1.0"
}

and script.js which is loaded by background.js from manifest.json:

var str2ab = function(str) {
   var encodedString = unescape(encodeURIComponent(str));
   var bytes = new Uint8Array(encodedString.length);
   for (var i = 0; i < encodedString.length; ++i) {
      bytes[i] = encodedString.charCodeAt(i);
   }
   return bytes.buffer;
};



var listOfSerialDevices = function(ports) {
  for (var i=0; i<ports.length; i++) {
  console.log(ports[i].path + ' ' + ports[i].vendorId + ' ' + ports[i].productId + ' ' + ports[i].displayName);
    chrome.serial.connect(ports[i].path, function (ConnectionInfo) { 
        if (ConnectionInfo) {
        console.log('id:' + ConnectionInfo.connectionId) 
        var msg = '1'
        chrome.serial.send(ConnectionInfo.connectionId, str2ab(msg), function() { console.log('Message sent!')})
    }
    } );
  }
}

chrome.serial.getDevices(listOfSerialDevices)

Console.log tells "Message sent!" although serial monitor shows nothing. Also TX and RX leds on arduino dont show any data coming from or to computer. Also led 13 doesnt light up.

I also saw two solutions of connecting to serial devices via chrome app one which i use getDevices -> Connect -> Send but also (which also doesnt work at all) with Open function instead of Connect, and also chrome.serial.write instead of chrome.serial.send. If I try to use wrote function console says that ther eis no such function. Why? Are there two ways to do same thing? Which one is better? Why my approach doesnt work?

Thank you for any help!

Upvotes: 0

Views: 637

Answers (1)

Gamadril
Gamadril

Reputation: 927

Chrome serial API defines only the send function: https://developer.chrome.com/apps/serial

There seems to be an error when sending data. The call of the callback function passed to the send function does not automatically mean it was successful. You should check if there was an error:

chrome.serial.send(ConnectionInfo.connectionId, str2ab(msg), function(sendInfo) {
    if (sendInfo.error) {
        console.log(sendInfo.error);
    } else if (sendInfo.bytesSent > 0) {
        console.log('Message sent!');
    }
});

Upvotes: 1

Related Questions