Page 1 of 1

Websocket send bin data through serial

Posted: Sat Jun 21, 2014 2:23 am
by msabatini
Hi,
my question is about the use of websocket to send binary data through airconsole to my serial connected equipment.
I'm trying the websockets_sdk/airconsole.html (airconsole.js) , and I checked through Docklight that when sending the following data:
"7E 00 0E 00 07 FF 00 07 00 00 01 00 21 CB 34 02 3C 7F"
through the command textbox, Docklight shows that I'm receiving an ASCII string.

How could I find a way to send that binary data above through the provided demo, eventually modifying the js code? are there any option I could try to change?

PS: that string is a custom command that I'm using to start a connection with my custom device, it answers me with an ack value.

Thank you
Best Regards

Re: Websocket send bin data through serial

Posted: Sat Jun 21, 2014 4:16 am
by daniel
Hello. The example airconsole.html doesn't directly show how to send binary data although the airconsole.js is capable of doing this via the "sendBinary()" function.

For example - here is some sample code which will parse the hex string in the command box into hex bytes and then send as binary to the Airconsole (I'm not sure of your programming ability so hopefully this isn't too difficult for you)

Code: Select all

function sendHexBytes() {
  var str = document.getElementById("command").value;
  var bytes = [];
  while (str.length >= 2) {
    bytes.push(parseInt(str.substring(0, 2), 16));
    str = str.substring(2, str.length).trim();
  }
  ac.sendBinary(bytes);
}

To use this, paste the above code into the <script> section of airconsole.html, then update the button handler that instead of calling "doSend" it calls "sendHexBytes" instead. (Note: the above code hasn't actually been tested, so hopefully there are no typos in it! :o )

Re: Websocket send bin data through serial

Posted: Mon Jun 23, 2014 8:57 pm
by msabatini
Thank you very much, with a very little modification it worked:

airconsole.js:

Code: Select all

Airconsole.prototype.sendHexBytes = function(str) {
   var bytes = [];
   while (str.length >= 2) {
      bytes.push(parseInt(str.substring(0, 2), 16));
      str = str.substring(2, str.length).trim();
   }
   ac.sendBinary(bytes);
}


and in airconsole.html:

Code: Select all

function doSendHex() {
    ac.sendHexBytes(document.getElementById("commandHex").value +"\r");
}
<!-- ... -->
<p>Command: <input type="text" id="commandHex" value=""> <button id="sendHex" onClick="doSendHex();">Send Hex</button><br>


Thank you very much!
Best regards