Send ZPL command to Zebra 170PAX4 via TCP/IP Socket

Hello Everyone,
I’m trying to send print labels to a Zebra printer and been somewhat successful.
I can send the first print job to the printer and the printer prints the label right away on the second send the printer takes minutes before it prints if I keep trying to send it eventually I get a socket error connection refuse.
When I use the Zebra Setup Utility to send the same exact command I have not issues therefore it has to be something within Ignition (I’m using the latest version) or I’m doing something wrong.
:scratch:

Here is a snip of the code I’m using
#========================================================================================

ZPL II commands

#========================================================================================
strMessage="^XA^LH96,96^LRY^FO10,0^ADN,18,5^FDHUE08161110136^FS^XZ"

#========================================================================================

Import Socket Library

#========================================================================================

import socket
#========================================================================================

Get Printer IP from Database

#========================================================================================
strIP = system.tag.getTagValue(“Murrieta/Printers/Zone1/Left/IP”)
#========================================================================================

Send Data to Printer

#========================================================================================
port=9100
updSocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
updSocket.connect((strIP,port))
updSocket.sendall(strMessage)
updSocket.close

Check out this forum thread and see if it helps.

viewtopic.php?f=70&p=31719

[quote=“Greg.Simpson”]Check out this forum thread and see if it helps.

viewtopic.php?f=70&p=31719[/quote]

Thanks but that is exactly what I’m doing my problem is that after the first message send the printer stops responding, don’t know if there are extra data being send to the printer that is causing it to lock withing the ignition. As mentioned earlier if I use the Zebra Setup Utility to send data it works without problems so its something in ignition.

I’m not exactly sure what is going on. When you say that you send the exact same command using the Zebra Utility does that also include the python socket connection and sendall commands?

I’m just shooting in the dark here but maybe the sendall function is not terminating like it should or is raising an exception that isn’t being caught. Did you try using the sendto function instead of connecting to a socket and then calling sendall?

Try using Wireshark to see what’s actually going on.

Great minds think alike, I was just going to post my finding with Wireshark.
I discovered that using the Python’s socket library when opening the connection Phyton send a Name Query with port 137 (netbios-ns) while the Zebra Utility doesn’t.
I switch the script to use Java’s socket seems to work the same as the Zebra Utility.
Once I go back to the customer site on Thursday I will verify with an actual printer.






Sorry for the Delay been busy, changing the script to utilize the Java socket work and now I’m able to print without issues.
Bellow if a snip of the code if anyone needs it.

strMessage = “^XA^LH10,10^LRY^F010,45^AFN,20,20^FDHELLO^FS^XZ”
#========================================================================================

Send Data to Printer

#========================================================================================
port=9100
try:
# Open Socket Connection
clientSocket=Socket(strIP,port)
#Open data output stream
outToPrinter=DataOutputStream(clientSocket.getOutputStream())
#Send Data to Printer
outToPrinter.write(strMessage)
#close data stream and socket
outToPrinter.close();
clientSocket.close();
except IOError:
print “Error”

1 Like

Hello I am trying to use your code as as script on a button


But I am getting the following errors:

Any thoughts?

You have to import the Socket class from Java.

1 Like

Please note that there is a much simpler way to communicate with Zebra nowadays using HTTP.

https://km.zebra.com/kb/index?page=content&id=WH133&actp=LIST

4 Likes

Many many thanks for your response.
I am going to read that one too!!

I believe there is going to be a Zebra printer module released very soon. Not by me, but soon, very soon.

3 Likes

REALLY!!! You have peaked my interest…

Also, really cool API I found is http://labelary.com/ it can be used to preview ZPL code to images and has a nice webservice. I have used it in Ignition to preview labels before sending them to a printer.

3 Likes

Thanks for the HTTP tip. I did not know this was possible this easily.

Here is an Ignition snippet for reference that prints ZPL to a printer

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

Also check out their SDK.
The printers now have JSON out of band management on port 9200. They should also come with Bluetooth and RFID sticker on the side, at least for the ZT410s.
We have done a lot of work around this. At one point we had a simple iPhone app that our techs could run whenever they needed to provision a printer. The app sent all the necessary payloads to configure the printer, point it back to an FTP server (Zebra Mirror) for firmware updates. etc. Another thing we tried is to put the label templates on each printer and send XML print jobs direct over HTTP, but without a management tool it was a hassle and we settled on NiceLabel. Coincidentally Zebra’s label designer is a stripped down OE version of NiceLabel. NiceLabel software is nice, but their support sucks, and development is in Europe.

… and then we got pissed at Zebra and moved to Honeywell. In the end a printer is a printer is a printer. But the techs do miss the app.

Let me know if I can help more with these printers.

2 Likes

Thank you so much for your help:
I was able to print by pressing a button on Igniton:
All I needed to do was set the following code on the Script Editor of the mousedPressed* and I was able to print Hello World! hahaha. Thank you guys.
system.net.httpPost(‘http://192.168.16.10/pstprnt’, postData="^XA^FO50,50^A0N50,50^FDHello World!^FS^XZ")

By the way, this is a great tool for generating the zpl code you need: Labelary Online ZPL Viewer
Thanks for sharing!!

2 Likes

I think I posted a video about our solution before, but here it is again.

1 Like

Hello

I need to preview a label before printing like you

can you tell me how you did it?

I use an image component with the Labelary url in the Image Path.Works pretty slick. Here’s a sample window for you.

test_LabelPreview_2018-08-01_0804.proj (5.8 KB)

2 Likes

^ what he said :slight_smile:

here is the function I use to get the preview image formatted for an Ignition Image path. depending on how I am using it, I either return it as a image path or wrapped in HTML for embedding in other things.

def getPreview(zpl, png=True):
	import urllib

	if zpl:
		zpl = zpl.replace('\r', '').replace('\n', '').replace('\t', '')
		zpl = urllib.quote(zpl, '^:~,')
		url = "http://api.labelary.com/v1/printers/12dpmm/labels/3x2/0/%s" % zpl
		if png:
			return url
		else:
			return "<html><img style='width:100%; height: auto;' src='" + url + "' /></html>"
	else:
		return ''
2 Likes