B4J Question Connect ESC/POS Library to USB

Chris Guanzon

Active Member
Licensed User
Longtime User

Hello everyone, I found this sample code in the forum, and it works fine. However, this code doesn't include the fontSize option like in the ESC/POS library by agraham. I tried editing the ESC/POS code using the code above, but I'm unsure how to do it. I can't connect the ESC/POS printer via USB like in the code above. How can I connect the ESC/POS printer to USB, similar to the code above?
 

Chris Guanzon

Active Member
Licensed User
Longtime User
First you said it works fine.
Then you asked,
Question 1, how to set the font size.
Question 2, can't make connection with USB but you said it works fine.

It works fine using the code I found in the forum posted above. However, the code above doesn't include the font size feature, unlike the code in agraham's ESC/POS implementation. So, I attempted to edit the ESC/POS code using the code I found in the forum, but I am unsure how to do it. Additionally, I am unable to connect the ESC/POS printer via USB like in the code above.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Try to understand and do more experiments by experiments.

I have wasted rolls and rolls of thermal paper by printing gibberish.
Money wasted but I get solution by sacrificing, not free.


The above code module utilize java inline code to print using javax.print
The important methods are:
1. printString - pass value in raw String and it should print as it-is
2. printBytes - pass value in byte characters as command according to printer documentation

Do you see the code for cut?
B4X:
Dim by() As Byte = jo.RunMethodjo("cut", Null)
jo.RunMethod("printBytes", Array(printerGen, by))
The above is how B4X call the inline Java code.

Check or trace back the Java code, in this case "cut".
Java:
    public byte[] cut() {
        byte[] cutP = new byte[] { 0x1d, 'V', 1 };
        return cutP;
    }
{ 0x1d, 'V', 1 } meaning you need to pass a command (which is a combination of these 3 characters) and converted it to byte array.
This value is to passed to the printBytes and send to the printer to ask it to cut the paper after printing something.

When you want to "alter" the output, such as changing the font size, you need to pass another combination depending on the printer. Maybe another 3 or 4 characters. You may need to find the ASCII/decimal/hex table for the equivalent values.
Next, pass the text to print such as "Invoice".
When you want to exit this font size or reset to normal font size, you need to issue another combination.
This is similar if you want to align the text to left, center or right.

e.g:
B4X:
Dim size As Int = 1
Dim data As String = Chr(27) & "M" & Chr(Bit.And(1, size))    ' Chr(27) = ESC
jo.RunMethod("printString", Array(printerGen, data))
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
For USB connection...
The S in USB is Serial. Therefore you are dealing with "Serial" connection or AsyncStreams.
Make sure you pass the correct printer name added in the OS, as printerGen in printString method.
 
Upvote 0
Top