dtwy2858 2013-12-30 11:52
浏览 275

ESC / POS PHP条码打印

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条回答 默认 最新

  • drsvw88664 2013-12-30 12:19
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码