I am having trouble with setting the horizontal print position in an Epson LX-300 II dot matrix printer. The command to set the horizontal print position does not work somet
ESC/P Set Absolute Horizontal Print Position
From the given 3rd Party Java code on ESCPrinter.java, the approach to send ESC/P control code to the printer port is waiting for disaster. For your case, when the value is larger than 7-bit data (127 / 0x7F).
Given the command to set absolute horizontal position:
ESC $ nL nH
nL value: 0 <= nL < 256
When nL value is over 127, the value is converted incorrectly and sent to the printer port. The incorrect conversion is caused by PrintStream() class which will call the default charset encoding based on your system locale (internally create java.io.Writer() class). That's why the value nL is never correctly send to printer port.
To fix this problem, you must never try to use String() class or any other charset encoding related class to write the control code (e.g. .toString(), .toByteArray(charset), Writer).
You can try UTF-8 encoding for PrintStream(), to see if it fixes the bug or not.
The problem I found is that UTF-8 is multi-byte encoding and ESC-P needs the commands to be in a single byte, from 0-256
ISO-8859-1 is single byte encoding up to 256.
Java byte is a decimal from a signed 2's compliment so numbers over 127 are negative.
public byte getN1(int total) {
// a java byte can only be -127 > 127
// so the negative numbers represent the numbers greater than 127
int n1 = total % 256;
if (n1 > 127) {
n1 = n1 - 256;
}
return (byte) n1;
}
public byte getN2(int total) {
// N2 is a factor of 256. so it is multiplied by 265 and added to N1.
// generally it is either 1 or 0 but can be more for things like vertical
// position.
int n2 = total / 256;
return (byte) n2;
}
public String setAbsoluteHorizontalPosition(int dots) throws UnsupportedEncodingException {
byte[] x = { ESC, '$', this.getN1(dots), this.getN2(dots) };
return new String(x, "ISO-8859-1");
}