如何在Linux中将ESC/POS命令发送到热敏打印机 [英] How to send ESC/POS commands to thermal printer in Linux

查看:89
本文介绍了如何在Linux中将ESC/POS命令发送到热敏打印机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在热敏打印机上发送ESC/POS命令.但是每当我发送给他们时,热敏打印机就会将它们打印为文本,而不是将其作为命令执行.我正在将这些命令写入.prn文件中,每当我执行lp命令以打印文件时,这些.prn文件也将以文本形式打印出来.

我尝试了以下方法在 .prn 文件中写入 ESC/POS 命令:

  1)打印#1,CHR $(& H1D);"h"; CHR $(80);打印#1,CHR $(& H1D);"k"; CHR $(2);打印#1,"48508007"; CHR $(0);PRINT#1,CHR $(& HA);打印#1,CHR $(& H1D);"k"; CHR $(67); CHR $(12);打印#1,"48508007";2)< ESC>(0x1B)< L>(0x4C)< GS>(0x1D)< k>(0x6B)73 2 4 5 6 7 8 9 NUL<FF>(0x0c)3)< ESC L>< GS k 73 2 4 5 6 7 8 9 NUL> 

4)"ESC L""GS k 73 2 4 5 6 7 8 9 NUL""FF"我也尝试过使用C程序发送ESC/POS命令,如下所示:

 #include< stdio.h>#include< stdlib.h>#include< fcntl.h>int main(){int fd,ret;char buf [] ="HELLO"fd = open("/dev/bus/usb/003/007",O_WRONLY);if(fd< 3){恐怖(打开失败);}ret =写(fd,& buf,sizeof(buf));if(ret == -1){perror(写入失败");}} 

执行后,以上代码给出错误消息:

 写入失败:参数无效 

解决方案

这里实际上存在两个问题:数据传输和正在传输的数据.

数据传输:使用 usblp

要获取数据到打印机,请检查是否已加载 usblp .它使您可以将打印机显示为 lp 组中的文件,以便您可以将其作为/dev/usb/lp0 打开.

一旦可行,作为普通用户,您可以编写:

echo"Hello">/dev/usb/lp0

我写了博客文章主题,它涵盖了权限方面.

数据格式

第二,数据本身需要以正确的二进制格式发送.打印机不会直接理解此人类可读的 .prn (打印?)文件,因此需要将其转换为正确的二进制格式.

要解释您的问题中的一些内容:

  • ESC 表示 \ x1b ,ASCII转义符
  • k 的意思是``,即实际的ASCII字母k
  • CHR$(80) 的意思是 \x50,这就是将数字 80 发送到打印机的方式.
  • CHR $(& H1D); 表示 \ x1d ,ASCII组分隔符( GS )

您可以使用 echo -e 在CLI上直接编写一些基本命令.也许最简单的非文本示例是显示"000"的CODE39条码.

该相同命令以人类可读的形式显示,然后是十六进制,然后作为终端命令将是:

  • GS k 4 0 0 0 NUL
  • 1d 6b 04 30 30 30 00
  • echo -e'\ x1d \ x6b \ x04000 \ x00'>/dev/usb/lp0

您最好的参考格式是打印机的编程手册,但是希望可以帮助您解释它.

请注意,您尝试执行的命令包含错误,其中之一甚至会导致您陷入页面模式.

此外,@ scruss和@abartek的答案是完全准确的:使用 lsusb 命令检查CUPS是否未声明端口,并使用十六进制编辑器查看您的输出,或者一个用于生成已知良好命令的库.

I am trying to send ESC/POS commands on a thermal printer. But whenever i send them thermal printer prints them as a text instead of executing them as commands. I am writing these commands in a .prn file and whenever i executes lp command to print a file these .prn file also get printed but as a text.

I tried following method to write ESC/POS command in .prn file :

1) PRINT #1, CHR$(&H1D);"h";CHR$(80);
   PRINT #1, CHR$(&H1D);"k";CHR$(2);
   PRINT #1, "48508007";CHR$(0);
   PRINT #1, CHR$(&HA);
   PRINT #1, CHR$(&H1D);"k";CHR$(67);CHR$(12);
   PRINT #1, "48508007";

2) <ESC>(0x1B) <L>(0x4C)
   <GS>(0x1D) <k>(0x6B) 73 2 4 5 6 7 8 9 NUL
   <FF>(0x0c)

3) <ESC L>
   <GS k 73 2 4 5 6 7 8 9 NUL>

4) "ESC L" "GS k 73 2 4 5 6 7 8 9 NUL" "FF" I also tried sending ESC/POS command using C program as:

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>

int main() {
   int fd,ret;
   char buf[] = "HELLO"

   fd = open("/dev/bus/usb/003/007",O_WRONLY);
   if(fd < 3) {
      perror(open failed);
   }

   ret = write(fd,&buf,sizeof(buf));
   if(ret == -1) {
      perror("write failed");
   }
}

Upon execution the above code gives error as:

write failed: invalid arguments

解决方案

There are actually two issues here: data transport, and the data being transported.

Data transport: Use usblp

For getting the data to the printer, check that usblp is loaded. It allows you to expose the printer as a file in the lp group, so that you can open it as /dev/usb/lp0.

Once this works, as a regular user you can write:

echo "Hello" > /dev/usb/lp0

I wrote a blog post on the topic which covers the permissions side of this.

Data format

Secondly, the data itself needs to be sent in the correct binary format. A printer will not understand this human-readable .prn (print?) file directly, so it needs to be converted to the correct binary format.

To interpret a few things from your question:

  • ESC means \x1b, an ASCII escape
  • k means ``, the actual ASCII letter k
  • CHR$(80) means \x50, which is how the number 80 is sent to the printer.
  • CHR$(&H1D); means \x1d, an ASCII group separator (GS)

You can write some basic commands directly on the CLI using echo -e. Maybe the simplest non-text example is a CODE39 barcode that says '000'.

This same command in human-readable form, then hex, then as a terminal command would be:

  • GS k 4 0 0 0 NUL
  • 1d 6b 04 30 30 30 00
  • echo -e '\x1d\x6b\x04000\x00' > /dev/usb/lp0

Your best reference for this format is your printer's programming manual, but hopefully this helps you to interpret it.

Be aware that the commands you are trying to execute contain errors, and one of them will even will trap you in page mode.

Aside, the answers by @scruss and @abartek are completely accurate: Check that CUPS hasn't claimed the port by using the lsusb command, and use a hex editor to review your output, or a library to generate known-good commands.

这篇关于如何在Linux中将ESC/POS命令发送到热敏打印机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆