How to send raw string to dotmatrix printer using python in ubuntu?

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

The computer is connecting to the printer through another computer. I need to send the original line because the print result of the LX-300 images is blurry.

+5
source share
2 answers

Problem

To send data on this route:

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

... and not allow Windows to work with data; instead, send raw data, including printer control codes, directly from the client computer.

My decision

Here's how I solved an almost identical problem for a small internal database application:

Step 1) Make access to the printer available without access to Windows, so that his fingers get into the data sent to him. I accomplished this by installing the printer using the "Generic / Text Only" driver, then installing RawPrintServer on a Windows machine connected to the printer.

Step 2) Send the raw data over the network to the TCP / IP port specified when setting up RawPrintServer (9100 by default). There are various ways to do this, here is 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:

  • Client machine: spills out the data I need with Python with the correct formatting / control codes for my printer and sends them over the network.
  • Print Server Machine: Transferring Data to a Locally Connected Printer

Number 1 is the easy part. There are actually a few libraries in PyPI that can help with all printer codes, but I found that most of them were aimed at selling label printers, and were limited for me. So I just encoded what I needed into my Python program.

Of course, the way you decide to solve number 2 will affect how you send data with Python. I chose the TCP / IP route to avoid printing problems with Samba and Windows.

As you probably discovered, Windows usually very hard converts everything you want to print into a bitmap and start the printer in graphical mode. We can use a generic driver and upload data directly to the (local) printer port to prevent this.

There is no link from the network to the local printer port on the computer connected to the printer. Again, there are various ways to solve this problem. You can somehow try to access the Windows printer resource. If you go over TCP / IP, like me, you can write your own print server in Python. In my case, the RawPrintServer program "just worked", so I did not investigate it further. Obviously, all he does is capture incoming data from TCP port 9100 and drag it to the local printer port. Obviously, you need to be sure that the firewall is not blocking incoming connections on the print server machine. This method does not require the printer to be "shared" for Windows.

Depending on your situation (if you use DHCP), you may need additional work to get the server IP address in Python. In my case, I got IP for free due to the features of my application.

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

+3
source

Ultimately you will need and you will want to write your own packaging / script to do this. And since you are using a Linux distribution, it is relatively simple.

On Linux, the easiest way to complete a print job is to open subprocess to lpr . As a rule, using lpr allows you to access the printer without having to log in as an administrator (which is the superuser), which is desirable, given the amount of damage that can be done during the login as "superuser".

The code is as follows:

 import subprocess lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE) lpr.stdin.write(data_to_send_to_printer) 

It must be a good leap for you. Essentially, this code should allow you to accomplish what you need.

Be careful; depending on your privilege levels, a call to open a subprocess may require root / Superuser permission.

Subprocesses typically inherit the user IDs and permissions for the user executing this command. For example, if a subprocess is created by the root user, you will need root / Superuser user rights to access this subprocess.

For more information, check out the hyperlinks that I included in the post.

Good luck

+2
source

Source: https://habr.com/ru/post/1257779/


All Articles