Reputation: 38
I'm trying to start working with ESC/P commands with a label printer Brother TD-4000. I have tested the properly software of the printer, P-touch Editor 5.1, and I can made several labels, the printer works well but, when I'm tried to make my own labels from Java code, the printer doesn't work at all, it doesn't response. I've have worked with other label printers with EZPL and I hadn't any problems with this method. What do I can try now?
My code is very simple, here you are:
public class PrintESC_P {
public static void main(String[] args) {
PrintService printService = null;
String printerName = "Brother TD-4000";
HashAttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName(printerName, null));
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet);
if (services.length == 0) {
throw new IllegalArgumentException("Printer not found.");
} else if (services.length > 1) {
System.out.println("Found more than one printer. Only the first printer will be used.");
}
printService = services[0];
System.out.println("Printer found: "+printService.getName());
try {
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
String _ESC_P_Code = "ESC i a 00h\r\n" +
"ESC @\r\n" +
"ESC i L 01h\r\n" +
"ESC ( C 02h 00h FCh 02h\r\n" +
"ESC $ 2Bh 00h\r\n" +
"ESC ( V 02h 00h 6Dh 01h\r\n" +
"ESC k 0bh\r\n" +
"ESC X 00h 64h 00h\r\n" +
"PRINTER TEST\r\n" +
"ESC i C\r\n" +
"FF\r\n";
SimpleDoc doc = new SimpleDoc(_ESC_P_Code.getBytes(), flavor, null);
DocPrintJob job = printService.createPrintJob();
job.print(doc, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Thanks in advance!
Upvotes: 1
Views: 2209
Reputation: 349
I believe your problem is you are including spaces in the string, which is not allowed in ESC/P language.
Instead of (incorrect) string:
String _ESC_P_Code = "ESC i a 00h\r\n"
You must write:
String _ESC_P_Code = "\x1Bia\x00" // that is 4 bytes: 0x1B, 0x69, 0x61, 0x00
You do not have to follow the way I wrote the string, just make sure you are sending raw data.
I solved my problems printing in ESC/P by first debugging the program and viewing the string sent to the printer in binary form, and manually checking if there are no extra bytes - printer will not accept any such errors.
Upvotes: 1
Reputation: 38
Finally, I had to change the way to send the code to the printer. Instead of a String I had to send the code in a byte array in hexadecimal format. Now the printer works well and recognizes the commands.
Upvotes: 1