如何在ubuntu中使用python将原始字符串发送到dotmatrix打印机? [英] How to send raw string to a dotmatrix printer using python in ubuntu?

查看:169
本文介绍了如何在ubuntu中使用python将原始字符串发送到dotmatrix打印机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台点阵打印机LX-300通过网络连接到我的计算机.如何使用Python将带有ESCP字符的原始字符串直接发送到打印机?

I have a dot-matrix printer LX-300 connected to my computer through the network. How do I send a raw string with ESCP characters directly to my printer in Python?

该计算机通过另一台计算机连接到打印机.我需要发送原始字符串,因为LX-300图像打印结果模糊.

The computer is connected to the printer through another computer. I need to send a raw string because LX-300 image printing result is blurry.

推荐答案

问题

要通过此路由发送数据:

The Problem

To send data down this route:

客户端计算机--->服务器(Windows计算机)--->打印机(点矩阵)

Client computer ---> Server (Windows machine) ---> printer (dot-matrix)

...并且让Windows弄乱数据;而是直接从客户端计算机发送原始数据,包括打印机控制代码.

...and to not let Windows mess with the data; instead to send the raw data, including printer control codes, straight from the client computer.

这是我为小型内部数据库应用程序解决的一个几乎相同的问题:

Here's how I solved a near-identical problem for a small in-house database application:

第1步)使打印机可通过网络访问,而Windows无需将其手指放在路由到它的数据上.我通过使用"Generic/Text Only"驱动程序安装打印机来完成此任务,然后安装 在连接到打印机的Windows计算机上 RawPrintServer .

Step 1) Make the printer network-accessible without Windows getting its fingers in the data routed to it. I accomplished this by installing the printer using the "Generic/Text Only" driver, then installing RawPrintServer on the Windows machine connected to the printer.

步骤2)通过网络将原始数据发送到设置RawPrintServer时指定的TCP/IP端口(默认为9100).有多种方法可以做到这一点,这就是我所做的:

Step 2) Send raw data over the network to the TCP/IP port specified when you set up RawPrintServer (default is 9100). There are various ways to do that, here's what I did:

data = b"\x1B@A String To Print\x1B@" # be sure to use the right codes for your printer
ip_addr = 123.123.123.123 # address of the machine with the printer
port = 9100 # or whatever you set it to
s = socket.socket()
try:
    s.connect((ip_addr, port))
    s.send(data)
except:
    # deal with the error
finally:
    s.close()

背景

我分两部分考虑了这个问题:

Background

I thought about the problem in two parts:

  1. 客户端计算机:使用适用于打印机的正确格式/控制代码从Python分发我需要的数据,并通过网络发送
  2. 打印服务器计算机:将数据传输到本地连接的打印机

数字1是容易的部分.实际上,PyPI中有一些一些库带有所有打印机代码,但我发现其中大多数都是针对小型销售点标签打印机的,对我来说用途有限.因此,我只是将所需的内容硬编码到了我的Python程序中.

Number 1 is the easy part. There are actually some libraries in PyPI that may help with all the printer codes, but I found most of them are aimed at the little point-of-sale label printers, and were of limited use to me. So I just hard-coded what I needed into my Python program.

当然,您选择的解决数字2的方式将影响您如何从Python发送数据.我选择TCP/IP路由以避免处理Samba和Windows打印问题.

Of course, the way you choose to solve number 2 will effect how you send the data from Python. I chose the TCP/IP route to avoid dealing with Samba and Windows print issues.

您可能已经发现,Windows通常会非常努力地将想要打印的内容转换为位图,然后以图形模式运行打印机.为了防止这种情况,我们可以使用通用驱动程序并将数据直接转储到(本地)打印机端口中.

As you probably discovered, Windows normally tries very hard to convert whatever you want to print to a bitmap and run the printer in graphics mode. We can use the generic driver and dump the data straight into the (local) printer port in order to prevent this.

然后,缺少的链接从网络到达连接到打印机的机器上的本地打印机端口.同样,有多种方法可以解决此问题.您可以尝试以某种方式访问​​Windows打印机共享.如果像我一样走TCP/IP路由,则可以用Python编写自己的打印服务器.就我而言,RawPrintServer程序正常工作",因此我没有进一步调查.显然,它所做的只是从TCP端口9100抓取传入的数据,并将其推入本地打印机端口.显然,您必须确保防火墙不会阻止打印服务器计算机上的传入连接.就Windows而言,此方法不需要共享"打印机.

The missing link, then, is getting from the network to the local printer port on the machine connected to the printer. Again, there are various ways to solve this. You could attempt to access the Windows printer share in some way. If you go the TCP/IP route like I did, you could write your own print server in Python. In my case, the RawPrintServer program "just worked" so I didn't investigate any further. Apparently all it does is grab incoming data from TCP port 9100 and shove it into the local printer port. Obviously you'll have to be sure the firewall isn't blocking the incoming connections on the print server machine. This method does not require the printer to be "shared" as far as Windows is concerned.

根据您的情况(如果使用DHCP),可能需要做一些额外的工作才能用Python获取服务器的IP地址.就我而言,由于我的应用程序的特殊性,我免费获得了IP.

Depending on your situation (if you use DHCP), you might need to do some extra work to get the server's IP address in Python. In my case, I got the IP for free because of the peculiarity of my application.

这个解决方案对我来说似乎很好.我有一台旧的松下打印机在连接Windows 7机器的Epson ESC/P兼容模式下运行,可以从本地网络上的任何其他计算机进行打印.顺便说一句,无论客户端计算机正在运行什么操作系统,该基本思想都应该起作用.

This solution seems to be working out very well for me. I've got an old Panasonic printer running in Epson ESC/P compatibility mode connected to a Windows 7 machine, which I can print to from any other computer on the local network. Incidentally, this general idea should work regardless of what OS the client computer is running.

这篇关于如何在ubuntu中使用python将原始字符串发送到dotmatrix打印机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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