5

I have an application that communicates with a thermal printer in Java and makes the thermal printer print receipts with a barcode/emphasis/different sizes and so forth using a Star tsp 100 Printer.

I can make the program print exaclty what i like but the printer is very slow. I believe the reason is that I am using non-preferable way/method of sending the byte commands.

    public static void Command(byte[] bytes) throws Exception{
    //The bytes array is the argument, consisting of a byte[] array such as
    //byte[] Emphasis = {0x1B, 0x45}; //Which gives the text boldness.
    //And Command(Emphasis); To execute the command with this method.
    DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    Doc doc = new SimpleDoc(bytes, flavor, null);
    job.print(doc, null);
    }

And when i want to print a String i use this method.

    public void PrintString(String s) throws Exception{
    DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
    System.out.println(job + " <- printer");
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    byte[] b = s.getBytes("CP437");
    Doc doc = new SimpleDoc(b, flavor, null);
    job.print(doc, null);          
}

So i use this method to print the bulk of the String for as long as the text has the same style(Etc no additional commands necessary). So my code that prints the receipts looks somewhat like this.

PrintClass P = new PrintClass();
String Greating = "Hello";
P.Command(P.Emphasis);
P.Command(P.DoubleSize);
P.Command(P.Underline);
P.PrintString(Greating);
P.Command(P.CancelEmphasis);
P.Command(P.ResetSize);
P.Command(P.CancelUnderline);
String body = GenerateReceiptBody();
P.PrintString(body);
P.Command(P.Halfsize);
String footer = Constants.getFooter();
P.PrintString(footer);
P.Command(P.Cut);

The receipt gets printed exactly the way i want but it is a very sluggish process.

I am by no means an expert when it comes to sending POS/ESC commands. I feel however that there must be a better/faster way to do this since many applications can print a receipt with different size/barcode/style/logo without it taking 10-20 seconds.

When the receipt printer comes to a the main bulk or "body" of the receipt where everything has the same size/styling then it goes quickly, this makes me believe that the reason this is going slow for me is because i am making am creating/executing so many individual "print jobs".

So is there any faster way to send ESC/POS commands to a Thermal printer as byte commands ? In this case the thermal printer is a Star tsp 100 but i don't believe that it makes any difference for the answer.

Any answers would be very appreciated. I am sorry if this was an easy question as I am still learning how to code.

4
  • Can you pls share the code for printing, i am a beginner, thanks
    – Smith
    Aug 10, 2017 at 16:00
  • I don't have the code anymore since I've upgraded the code long ago : P But why are you having a tough time getting it to work? Aug 11, 2017 at 11:27
  • what did you upgrade to? it would have been nice if you could really help out one way or the other
    – Smith
    Aug 20, 2017 at 11:10
  • Well Smith make an attempt and post it as a question and I will take a look at it. Aug 21, 2017 at 19:12

1 Answer 1

7

I have no idea if this will improve your printing speed, but in answer to your question about reducing the number of "print jobs", you could write all the bytes to a stream first, then send the whole stream to a single print job. I've attempted to convert your code to do this.

    public static void test() throws Exception
    {
        ByteArrayOutputStream printData = new ByteArrayOutputStream();

        printData.write(PrintClass.Emphasis);
        printData.write(PrintClass.DoubleSize);
        printData.write(PrintClass.Underline);
        printData.write("Hello".getBytes("CP437"));
        printData.write(PrintClass.CancelEmphasis);
        printData.write(PrintClass.ResetSize);
        printData.write(PrintClass.CancelUnderline);
        printData.write(GenerateReceiptBody().getBytes("CP437"));
        printData.write(PrintClass.Halfsize);
        printData.write(Constants.getFooter().getBytes("CP437"));
        printData.write(PrintClass.Cut);

        printBytes(printData.toByteArray());
    }

    public static void printBytes(final byte[] bytes) throws Exception
    {
        DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
        System.out.println(job + " <- printer");
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        Doc doc = new SimpleDoc(bytes, flavor, null);
        job.print(doc, null);
    }
1
  • Thanks a bunch this worked like a dream ! I can see on the number of downvotes that this wasn't a very appreciated question..But oh well I am still very glad i asked it ^^. Feb 20, 2015 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.