Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Sending ESC/POS commands to a thermal receipt printer

$
0
0

I have been trying to figure out a way of sending ESC/POS commands to a thermal receipt printer from an ios app and have come across the following link: http://davidkelley.me/development/work/javascript/web/2013/08/31/javascript-interface-for-esc-pos.html

Which explains how to convert the character commands into an array of bytes to be sent to the printer. I have tried to use this information to build an array of bytes to add to a titanium buffer which is then sent to the printer via TCP/IP with not much luck as the Ti.createBuffer function expects a string value. Not having any experience using TCP/IP to communicate with these sort of devices I am kind of stabbing around in the dark trying to understand what format I should be sending the commands through as.

The following is the test code I am working with:

String.prototype.toBytes = function() {
 var arr = [];
 for (var i = 0; i < this.length; i++) {
  arr.push(this[i].charCodeAt(o));
 }
 return arr;
};
 
var data = "hello world".toBytes().concat([0x01B, 0x64, 10]);
 
var print = function() {
 
 var connectingSocket = null;
 var printContent = data;
 
 if (connectingSocket == null) {
  try {
   connectingSocket = Ti.Network.Socket.createTCP({
    host : '192.168.0.1',
    port : 9100,
    connected : function(e) {
     if (connectingSocket != null && connectingSocket.isWritable()) {
      connectingSocket.write(Ti.createBuffer({
       value : printContent
      }));
      if (connectingSocket != null) {
       connectingSocket.write(Ti.createBuffer({
        value : printContent
       }));
       try {
        connectingSocket.close();
        connectingSocket = null;
       } catch (e) {
        alert(e);
       }
      }
     }
    },
    error : function(e) {
    },
    closed : function(e) {
    }
   });
   connectingSocket.connect();
  } catch (e) {
  }
 }
};
 
 
setTimeout(function() {
 print();
}, 1000);
Can anyone point me in the right direction with this?

Many thanks


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>