Websocket send bin data through serial

Questions, FAQ, Feature Requests and Troubleshooting for Airconsole

Moderator: sergey

msabatini
Posts: 11
Joined: Sat Jun 21, 2014 2:13 am

Websocket send bin data through serial

Postby msabatini » Sat Jun 21, 2014 2:23 am

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

daniel
Posts: 127
Joined: Wed Jul 27, 2011 9:33 am

Re: Websocket send bin data through serial

Postby daniel » Sat Jun 21, 2014 4:16 am

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 )

msabatini
Posts: 11
Joined: Sat Jun 21, 2014 2:13 am

Re: Websocket send bin data through serial

Postby msabatini » Mon Jun 23, 2014 8:57 pm

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


Return to “Airconsole”

Who is online

Users browsing this forum: No registered users and 136 guests