1

I am trying to create a script for printing labels with zebra printers. I can send ZPL commands, using sockets.

def zebra_print(format, printer_option):
    mysocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)         
    printer = printer_option
 
    host = Printers.objects.get(name = printer).ip

    if host == '':
        host = Printer.objects.get(name = printer).path
    
    port = 9100

    try:           
        mysocket.connect((host, port)) # connecting to host
        mysocket.send(bytes(format, encoding='ascii')) # using bytes
        mysocket.close () #closing connection

    except Exception as e:
        print(e)

Both network path or IP work, but only if the printer has an ethernet connection.

For printers which do not have ethernet (directly plugged to pc) I am unable to connect. Tried to use PC ip or printer shared path with no success.

How can I achieve this?

  • 1
    Are the printers directly connected using USB serial ports? If so, you will need to determine the COM port that is being emulated and use pyserial for communication. For example, see https://stackoverflow.com/questions/44056846/how-to-read-and-write-from-a-com-port-using-pyserial. – Mark Warren Apr 19 '23 at 17:43
  • Use the printer API of the running OS – Delphi Coder Apr 24 '23 at 05:51
  • @Mark Warren, the printer is not connected to the printing computer, as this is a virtual machine inside a server. The printer is connected to a specific user computer, although shared into the network – Antonio Amo Sánchez Apr 25 '23 at 07:28
  • @Delphi Coder, could you provide any link/example of this? I need to send a prn (file-printed archive) to the Zebra, as this prn contains the specific values of an overwritten template. – Antonio Amo Sánchez Apr 25 '23 at 07:29
  • Sry, but no. I'm not a python programmer and you didn't provide any information about the used OS. – Delphi Coder Apr 25 '23 at 08:51
  • O.S. is Windows Server 2019 (~Windows 10) – Antonio Amo Sánchez Apr 25 '23 at 09:21

1 Answers1

0

You can do this with two options COM Ports and USB Ports

with the com you need to send via serial with pyserial creating the serial interface enabling the com port(windows) or tty(linux) and with the method .write("string zpl") you must print something

the another way is to install the printer via native drivers (linux is very easy) and you print the zpl file via terminal like this #lpr -P ALIASPRINTER -o raw filezpl.zpl and the printer will prints via LPR Command on terminal

Here is the main doc and support for pySerial library https://pypi.org/project/pyserial/

Here is a guide for LPR https://man7.org/linux/man-pages/man1/lpr.1.html

code below

class W_THREAD(QThread, object): w_signal = pyqtSignal(object)

def __init__(self,  event,portstr):
    QThread.__init__(self)
    self.port = portstr
    self.baudrate = 2400
    self.parity = serial.PARITY_NONE
    self.stopbits = serial.STOPBITS_ONE
    self.bytesize = serial.EIGHTBITS
    self.timeout = 2
    self.serial = serial.Serial(
        port=self.port,
        baudrate = self.baudrate,
        parity=self.parity,
        stopbits=self.stopbits,
        bytesize=self.bytesize,
        timeout=self.timeout
    )
    self.stopped = event

def kill_thread(self):
    self.w_signal.emit(0)

def run(self):
    try:
        print("here >>> "+str(self.stopped))
        while not self.stopped.wait(0.001):   
            self.get_w()
    except:
        self.w_signal.emit(0)

def get_w(self):
    w_active = self.serial.readline()
    self.w_signal.emit(w_active)

def writteserial(self,cmd): #here write the zpl output!!
    self.serial.write(cmd)


def LISTENER_W(self):
    try:
        self.stop_flag_time =  Event()
        self.w_1 = W_THREAD(self.stop_flag_time,self.WEIGHT_PORT)
        self.w_1.start()
        self.w_1.w_signal.connect(self.update_w44)
        print(str(self.stop_flag_time))
    except:
        print("exception")

def update_w44(self,w):
    try:
        number = self.only_num2(str(w))
        the_fuck_number_w = str(number)
    except:
        print("exception")

Cheers up

  • Thanks for your answer, but Zebras are network connected (ethernet) and we need to use Windows (script runs in W Server 19) where no console print command is available (configured). I'll talk with IT in order to see if they can solve the later. – Antonio Amo Sánchez Jun 19 '23 at 06:29