1
votes

I'm completely new to the ESC/POS thing, and I've looked around but couldn't find a solution to my problem. I'm trying to print a barcode using a generic USB POS-58 Thermal Printer, but all that shows up on the printer is the barcode data, not the barcode itself. The code I'm using is the following:

$barcode = "12345678901";

$handle = fopen('/dev/usb/lp0', 'w');

if (!$handle) {
    echo 'Cannot open printer';
    exit(0);
}

fwrite($handle, chr(hexdec('1D')).'f'.chr(0)); 
fwrite($handle, chr(hexdec('1D')).'H'.chr(2)); 
fwrite($handle, chr(hexdec('1D')).'h'.chr(60)); 
fwrite($handle, chr(hexdec('1D')).'w'.chr(2)); 
fwrite($handle, chr(hexdec('1D')).'k'.chr(1)); 
fwrite($handle, $barcode.chr(0));
fwrite($handle, chr(hexdec('0A')));
fclose($handle);

Please note that I'm using PHP but I get the same results using printf from the command line (or C). What I get on the printer is

12345678901

No barcode at all. Could it be possible that the printer doesn't support barcode printing? The "manual" is not very helpful, it just states that the printer is compatible with the ESC/POS command set. Am I missing something?

1

1 Answers

2
votes

Currently you're trying to solve four problems:

1) encoding your data using ESC/POS in a format the printer will recognise

2) managing access to a physical device on a multi-processing/multi-user operating system

3) implementing your system using PHP

4) determining the command set support on your printer

You will make your life a lot simpler by just dealing with one problem at a time.

You did specifically ask if the printer does (not) support barcodes - you'd have to check the manual / contact the supplier - after all you paid them good money for this thing, didn't you?

From your device naming I divine that you are using Linux/Unix/BSD. Hence I would suggest you start by creating static files to represent the print jobs - you can send them to the printer from the command line by simply:

 cat print_sample_v1.0 >>/dev/usb/lp0

The other thing you might consider is printing to the device via CUPs from an application like OpenOffice. This will allow you to try out different printer emulations (but if it is a particularly cheap device then you'll need to use a large raster for the barcode to avoid aliasing).

You shouldn't be writing directly to the printer port. That's fine on a single tasking system (DOS, CP/M) but sooner or later you will run into problems - that's why every OS written in the last 20 years has a printer spooling daemon. The easiest way to submit a print job from PHP is to generate a temporary file and exec lpr on it.