Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing with ESC/POS on Windows through Serial port

I'm trying to correctly print to a serial ticket printer on the USB port (using a USB-SERIAL adapter) using ESC/POS.

I have the same problem with both PYTHON and PHP.

I'm using phpSerial to open and write to the serial port on PHP and pyserial on PYTHON.

The problem I'm having is that it seems to work, except when I try to print a longer message. All the examples I find are with Basic or Java and they seem to make it work like I'm trying to do.

Here's my python code:

import serial

ser = serial.Serial('COM5');

ser.write('\x1b\x40'); # esc @ (init)
ser.write('\x0a'); #line feed
ser.write('\x0a'); #line feed
ser.write('Hello World'); #text
ser.write('\x0a'); #line feed
ser.write('\x1d\x56\x42\x03'); #cut the paper

That works. Except if I try this:

import serial

ser = serial.Serial('COM5');

ser.write('\x1b\x40'); # esc @ (init)
ser.write('\x0a'); #line feed
ser.write('\x0a'); #line feed
ser.write('Hello World'); #text
ser.write('Hello World'); #text
ser.write('Hello World'); #text
ser.write('Hello World'); #text
ser.write('Hello World'); #text
ser.write('\x0a'); #line feed
ser.write('\x1d\x56\x42\x03'); #cut the paper

Then it prints Hello World a couple of times and it stops abruptly (it doesn't cut the paper or print the right amount of lines).

On PHP I have this (I'm using a class I found online to generate the ESC/POS commands):

require('php_serial.class.php');
require('php_receipt.class.php');

$serial = new phpSerial;
$recibo = new Receipt;

$serial->deviceSet("COM5");
$serial->deviceOpen('w'); 


$recibo->init();
$recibo->writeLf("Testing printer");;
$recibo->feedCut();
$recibo->finalize();
$escribir = $recibo->__toString();
$serial->sendMessage($escribir);

$serial->deviceClose();

Again, that works beautifully. Except this:

$recibo->init();
$recibo->writeLf("Testing printer");
$recibo->writeLf("Testing printer");
$recibo->writeLf("Testing printer");
$recibo->writeLf("Testing printer");
$recibo->writeLf("Testing printer");
$recibo->writeLf("Testing printer");
$recibo->feedCut();
$recibo->finalize();
$escribir = $recibo->__toString();
$serial->sendMessage($escribir);

On the printed paper I get Testing printer 3 times, then it dies.

What I do now to fix this, is open and close the port for each line I print. But it prints so slow like that.

What am I missing? I've found nothing online!

like image 422
Nahuel Avatar asked Dec 10 '25 19:12

Nahuel


1 Answers

I'm answering my own question after some time of fighting with it.

It appears that because I'm using a USB to Serial port adapter, the buffer size is too small causing the information transmitted to cut off. To fix it, the printer needs to be set up to a 4kb buffer (instead of 40 bytes). This is done with DIP switches at the bottom of the printer.

In the case of the TM-U220 printer (the one I used), switch number 2 from DIP switch 1 (DSW1) needs to be set to off. Here's more information on how to configure DIP switches on this particular printer:

http://content.epson.de/fileadmin/content/files/RSD/AnwenderhandPDFs/DE/TM-U220_user_multi.pdf

like image 178
Nahuel Avatar answered Dec 12 '25 10:12

Nahuel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!