ammaralroz
Posts: 7
Joined: Tue May 23, 2017 10:09 am

Sending command to printer using serial port

Tue Aug 01, 2017 10:48 am

Dear All,
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 serial port does not accept integer

This is my function

Code: Select all

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

Code: Select all

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...-

Thank you

ammaralroz
Posts: 7
Joined: Tue May 23, 2017 10:09 am

Re: Sending command to printer using serial port

Tue Aug 01, 2017 11:17 am

Update
The code can accept this one

Code: Select all

                
                char[] F = new char[]{0x1B,'d',7};
                serial.write(F);
                
but it does not accept

Code: Select all

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

clicky
Posts: 202
Joined: Thu Oct 25, 2012 7:34 am

Re: Sending command to printer using serial port

Fri Aug 04, 2017 11:13 am

You can always cast integer to a byte or char:
serial.write((byte)feed);
serial.write((char)feed);
Also, if you need to send something other than -127 to 128 you'll need to convert your integer to two bytes paying attention to order (little or big-endian)...

Return to “Java”

Who is online

Users browsing this forum: No registered users and 4 guests