Printing Barcode Label

I have Zebra PC43d. I am trying to print the attached label:

I used a report component with a print button.

I am not familiar with ZPL but I start using in this Print button. Here is the script:

import socket
HOST = ‘10.81.19.189’
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(’^XA’)
s.sendall(’^FO50,50^ADN,36,20^FLine 50 50’)
s.sendall(’^FS’)
s.sendall(’^FO60,50^ADN,36,20^FColor60 50’)
s.sendall(’^FS’)
s.sendall(’^FO50,80^ADN,36,20^FDLot 50 80’)
s.sendall(’^FS’)
s.sendall(’^FO50,110^ADN,36,20^FDTubeJob#50 110’)
s.sendall(’^FS’)
s.sendall(’^FO50,150^ADN,36,20^FDQuantity:50 150’)
s.sendall(’^FS’)
s.sendall(’^XZ’)
s.close()

It is printing but I don’t have any values from my database that is driving the label. Could someone tell me how to associate the data that is on the report (query Source) to get it to show on the label?
The script above is showing just Label. For example: ^FO50,150^ADN,36,20^FDQuantity:50 150’ it shows quantity name but I don’t know how to bring the value in.

I appreciate your help.

Thanks…

1 Like

If you want to use ZPL to do it you will have to concatenate the strings to pass in the ZPL.
Something like

import socket
HOST = '10.81.19.189'
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('^XA')
s.sendall('^FO50,50^ADN,36,20^FLine 50 50')
s.sendall('^FS')
s.sendall('^FO60,50^ADN,36,20^FColor60 50')
s.sendall('^FS')
s.sendall('^FO50,80^ADN,36,20^FDLot 50 80')
s.sendall('^FS')
FDTubeJob = system.tag.read('TubeJobTag').value
s.sendall('^FO50,110^ADN,36,20^'+FDTubeJob+'#50 110')
s.sendall('^FS')
FDQuanitity = system.tag.read('FDQuantity').value
s.sendall('^FO50,150^ADN,36,20^'+FDQuantity+':50 150')
s.sendall('^FS')
s.sendall('^XZ')
s.close()

Or you can install the printer on the gateway and print to it using
system.report.executeAndDistribute
I have numerous systems printing a few thousand labels a day to all different versions of zebras using the execute method.

Totally personal preference, but since ZPL is already such an obscure format I like using triple-quoted strings to keep it separate from code:
<better example below>

4 Likes

Thank you for the info. My preference is not to use the ZPL since I have to deal with a third party utility.

You mentioned that I can install the printer on the Gateway and print to it using system.report.executeAndDistribute. Can you provide an example with a little more detail.Thank you!
My situation is I have jobs running on 6 different lines. each line has a Zebra barcode printer. As soon the job is completed, a FinishGood label is printed on the printer that is associated with the line. I am using a report that gets its data from several querirs and put them as a record with 8 fields. The values of these fields are the data I want to print on label. .

Thank you P. Could you be more specific on how to use what you indicated above in the grand schema of the entire code I listed early. For example, do I put this:

import socket
HOST = ‘10.81.19.189’
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

before your code above. Can you provide me with an example? Thanks…

Put together, the code would look like this - basically, create the string and send it to the printer all at once, rather than a lot of individual sendall() calls.

import socket
HOST = '10.81.19.189'
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

label = """
^XA
^FO50,50^ADN,36,20^FLine 50 50
^FS
^FO60,50^ADN,36,20^FColor60 50
^FS
^FO50,80^ADN,36,20^FDLot 50 80
^FS
^FO50,110^ADN,36,20^FD%(TubeJob)i#50 110
^FS
^FO50,150^ADN,36,20^FD%(Quantity)i:50 150
^FS
^XZ
""" % {
	'TubeJob': system.tag.read('TubeJobTag').value, 
	'Quantity': system.tag.read('FDQuantity').value
}

s.sendall(label)
s.close()

I am getting an error on line 7 (label = """)

Traceback (most recent call last):
_ File "event:actionPerformed", line 7, in _
ValueError: unsupported format character ' ' (0x20) at index 145

_ at org.python.core.Py.ValueError(Py.java:309)_
_ _

Ignition v7.9.9 (b2018081621)
Java: Oracle Corporation 1.8.0_172

Here is the code I used:
import socket
HOST = '192.168.0.20'
PORT = 631
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

label = """
^XA
^FO50,50^ADN,36,20^FLine 50 50
^FS
^FO60,50^ADN,36,20^FColor60 50
^FS
^FO50,80^ADN,36,20^FDLot 50 80
^FS
^FO50,110^ADN,36,20^FD%(TubeJob)#50 110
^FS
^FO50,150^ADN,36,20^FD%(Quantity):50 150
^FS
^XZ
""" % {
'TubeJob': system.tag.read('TubeJobTag').value,
'Quantity': system.tag.read('FDQuantity').value
}

s.sendall(label)
s.close()

There’s a small bug in the script. When using %-interpolation, you should also denote the datatype you want (number, string, date, …). That’s done as %(variable)s for string %(variable)i for integer, etc.

So here, if both are integers, you should use %(TubeJob)i and %(Quantity)i. Combined, that gives the following code:

import socket
HOST = '10.81.19.189'
PORT = 9100
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

label = """
^XA
^FO50,50^ADN,36,20^FLine 50 50
^FS
^FO60,50^ADN,36,20^FColor60 50
^FS
^FO50,80^ADN,36,20^FDLot 50 80
^FS
^FO50,110^ADN,36,20^FD%(TubeJob)i#50 110
^FS
^FO50,150^ADN,36,20^FD%(Quantity)i:50 150
^FS
^XZ
""" % {
	'TubeJob': system.tag.read('TubeJobTag').value, 
	'Quantity': system.tag.read('FDQuantity').value
}

s.sendall(label)
s.close()
3 Likes

Good catch - that's what I get for posting totally untested code.

1 Like

Don't feel bad. There's lots of untested code posted around here, including by me. Buyer beware!

2 Likes

Thank you for your feedback.

Thank you all for your help - You guys are awesome.
Sam

See this thread for another possible direct ZPL option, and I’m going to steal ryanjmclaughlin’s example as I’ve used it to print to zebra’s as also.

postData is the ZPL string.

system.net.httpPost('http://192.168.1.10/pstprnt', postData="^XA^WD^XZ")