Ammar
Ammar

Reputation: 33

Sending ESC/POS command to printer using serial port

I have a thermal printer. and I am re-writing the SDK. I am using pi4j library to send ESC/POS commands through the serial port. my problem is I do not know how to send integer to the printer since the pi4j library does not accept integer

This is my function

public void printAndFeed(String str, int feed) throws IOException {
    //escInit();
    serial.write(str);
    //output extra paper          ESC d n
    serial.write((char) 0x1B);
    serial.write("d");
    serial.write(feed);
}

the Error is here

serial.write(feed);

And the command reference:

ESC d n [Name] Print and feed n lines [Format] ASCII ESC d n Hex 1B 64 n Decimal 27 100 n [Range] 0≤n≤255 [Description] Prints the data in the print buffer and feeds n lines. [notes] •This command sets the print starting position to the beginning of the line. •This command does not affect the line spacing set by ESC 2 or ESC 3. •The maximum paper feed amount is 1016 mm {40"}. If the paper feed a mount (n× line spacing) of more than 1016 mm {40"} is specified, the printer feeds the paper only 1016 mm {40"}. [Reference] ESC 2(default line spacing),ESC 3(set line spacing)

the pi4j reference http://pi4j.com/apidocs/com/pi4j/io/ser ... e-char...-

I tried this one: The code can accept:

            char[] F = new char[]{0x1B,'d',11};
            serial.write(F);

but it does not accept this

            char[] F = new char[]{0x1B,'d',feed};
            printer.write(F);

Upvotes: 0

Views: 2800

Answers (1)

Ammar
Ammar

Reputation: 33

@Yazan,@Mike Harris Thank you so much for your help. I converted all my integer to byte as they suggested the code is (if someone came across this and needed help)

int feed=11; byte [] B=new byte[] {0x1B,'d',(byte)feed}; printer.write(B);

Upvotes: 1

Related Questions