Send hex command to ESC/POS Printer Android

DJ-DOO picture DJ-DOO · Dec 19, 2014 · Viewed 7k times · Source

I am working on an application that needs to print directly to an ESC/POS printer. Printing is fine, however when issuing commands to the printer they just print!! I'm trying to convert some c# code and the commands I'm trying to send are hex strings as follows:

public static String PRINTLOGOCOMPANY = "\x1c\x70\x01\x30";

Of course I understand that there are illegal escape characters in this so I changed it to:

"\\x1c\\x70\\x01\\x30"

I then converted to a byte array and tried sending it via a dataoutput stream like so:

String WIDTH_1 ="\\x1d\\x57\\x120\\x01";

Log.i("Width String: ", WIDTH_1);
final byte [] width = WIDTH_1.getBytes();
final int portNo =  xxxx;
final String ipAddress = "xxx.xxx.x.xxx";

Thread thread = new Thread() {
    @Override
    public void run() {
        try {
            Socket sock = new Socket(ipAddress, portNo);
            DataOutputStream dOut = new DataOutputStream(sock.getOutputStream());
            dOut.writeInt(width.length);
            dOut.write(width);
            dOut.close();
            sock.close();
        } catch (UnknownHostException e) {

            e.printStackTrace();
            Log.i("Unknown Host Exception Error: ", String.valueOf(e));
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("IO Exception Error: ", String.valueOf(e));
        }
    }
};
thread.start(

but it just prints it on the other end;

Could anyone offer any advice on this Id greatly appreciate it.

Answer

grig picture grig · Dec 23, 2014

If I understand your problem, you can't send commands to printer, it just prints all of your symbols. All you need is send bytes. For example, to open the till you need to do:

Socket mSocket = new Socket(PRINTER_IP, PRINTER_PORT);
OutputStream mPrinter = mSocket.getOutputStream();
mPrinter.write(0x1B);
mPrinter.write(0x70);
mPrinter.write(0);              
mPrinter.write(200);  // t1 
mPrinter.write(255);  // t2