Printing a label from a Zebra ZT230 using USB connection

Hello all!

I am having a hard time getting a Zebra ZT230 to print a barcode label from a Project script. I am using a slight variation of a script from a earlier post.
image

I am getting this error:
TypeError:'com.inductiveautomation.ignition.designer.gui.tools.jythonconsole.JythonConsole$ConsoleModule' object is not callable I am calling this from the script console and also tried from a button event onActionPerformed with no luck.
image

I will admit I have never had experience setting up a printer, as well as getting a barcode to print form Ignition. Is there any articles that I should look into? Or is this still possible with the script I have with rework?

Let me know if I need to provide anymore information.

First, your call should be printLabel.printLabel(). Script module name, then function name.

(I recommend not using the same name.)

1 Like

Ah that got rid of the first error! Although, now it is giving this error:
TypeError: java.net.Socket(): 1st arg can't be coerced to java.net.InetAddress, String

  • It was written for ethernet. It won't work on USB.
  • If you are running this in a perspective session, the gateway needs access to the printer, not the client session, where I assume the printer is located.
4 Likes

I see!

I have gone ahead and put in a ticket with support as this seems a bit over my head. Thank you for the information!

While I highly recommend hanging the printer off the network (wired or wireless, and withthe same caveats in my previous post:

If in Windows, make a Generic/Text printer that points to the USB port.
https://supportcommunity.zebra.com/s/article/Print-a-zpl-file-using-the-Generic-Text-Printer?language=en_US

Then your script would look more like:

def printLabel(serialNum, printerName)
	from javax.print import PrintService, PrintServiceLookup, DocFlavor, SimpleDoc
	
	foundService = False
	printServices = PrintServiceLookup.lookupPrintServices(None, None)
	for printService in printServices:
		if printerName in printService.toString():
			foundService = True
			break
	
	if foundService:
		strMessage100 = '^XA'
		strMessage112 = '^FT065,260,^BCN,N,N,N,N^FDS%s^FS' % serialNum
		strMessage113 = '^FT170,170^A0N,40,45^FD%s^FS' % serialNum
		strMessage199 = '^PQ1,0,1,Y^XZ'
		strOut = strMessage100+strMessage112+strMessage113+strMessage199
		
		job = printService.createPrintJob()
		flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE
		doc = SimpleDoc(strOut.getBytes(), flavor, None)
		job.print(doc, None)
3 Likes

Thank you SO much!

That worked perfectly! I am not getting any errors within the script as well as the script console. Although, now I need to figure out why it isn't printing once I run the script!

Thanks again!

My suspicion:

Specifically, the user that the gateway service runs under.

Gotcha,

Would that be in the Script Hint Scope? I have it set to all.
image

The Script Hint Scope just lets you chose what types of functions it will hint you for. For instance if you set it to Client, then you wont see functions that can only run in the gateway. It’s just a “Hint” to what you can use. It doesn’t play any role in what scope the script is actually executed in.

In perspective, all scripts run on the gateway. Scripts run in the script console are local to the machine / user running the designer.

2 Likes

Ie, no USB device connected to a Perspective client will be accessible unless it presents as a HID and simply inserts characters into the active field

2 Likes

Very helpful, thank you!

Since both the script console and a designer session wont work in this context, would using the perspective workstation be a good way to test?

I also added in a loop as requested from support, which does seem to find the printer name as it loops, but once I get to the if foundService it doesn't seem to print. I am using the script console at the moment to be able to view the print statements.

I have also tested this using the Generic/Text only "printer" as well as the printer itself. Both seem to return the print statement in the for loop.

Interesting, I will see if this is a possibility with this label printer. Thank you!

Perspective workstation does not provide scripted access to client-local printers. So, no.

Testing in the script console is utterly useless for something that needs to later work in gateway scope (Perspective script scope is an extension of gateway scope.)

Use logging instead of prints (via system.util.getLogger()) and monitor the gateway logs.

{ You need to plug your USB printer into the gateway, not your local workstation. }

1 Like

Thank you, I will try this and see if I get any results.

Hello, all!

After talking with support, we were able to get this script to print a serial number:

def printLabel(serialNum, printerName):
	from javax.print import PrintService, PrintServiceLookup, DocFlavor, SimpleDoc
	from java.lang import System
	from org.python.core.util import StringUtil
	from java.net import Socket
	from java.io import DataOutputStream
	  	
	foundService = False
  	printServices = PrintServiceLookup.lookupPrintServices(None, None) 
	for printService in printServices:
	  	print("Made it in loop")
	  	if printerName in printService.toString(): 
	  		              foundService = True 
	  		              # break
	  	
		if foundService:
			print('Found serv')
	  		strMessage100 = '^XA'
	  		strMessage112 = '^FT065,260,^BCN,N,N,N,N^FDS%s^FS' % serialNum
	  		strMessage113 = '^FT170,170^A0N,40,45^FD%s^FS' % serialNum
	  		strMessage199 = '^PQ1,0,1,Y^XZ'
	  		strOut = strMessage100+strMessage112+strMessage113+strMessage199
	
	  		
	  		
	  		
	  		job = printService.createPrintJob()
	  		flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE
			byteArray = StringUtil.toBytes(strOut) 
	  		doc = SimpleDoc(byteArray, flavor, None) 
	  		job.print(doc, None)	  		

Although, I would like to print a barcode with this serial number. Is this possible with a bit of rewrite with this code, or would it be better suited to look into the reporting module to get my results?

Zebra printers tend to support barcode generation internally. You just need the correct ZPL commands. I vaguely recall someone posting a ZPL "playground" website that might help you.

2 Likes

Thank you, I will look into the zebra documentation to see about the different commands!

ZPL2 Guide (plus others)

'Playgound' Phil mentioned:

2 Likes

Thank you!

I was able to get a barcode printed out with the help of @pturmel and @JordanCClark !

I changed my foundService if statement to this:

if foundService:
			print('Found serv')
	  		strMessage100 = '^XA'
	  		strMessage112 = '^FO50,50^B3N,N,100,Y,N^FD%s^FS' % serialNum
	  		strMessage113 = '^XZ'
	  		strOut = strMessage100+strMessage112+strMessage113

Which prints out the barcode alongside the serial number. Though when I go to scan this barcode it seems that the serial number isn't within the barcode itself.

Scratch that, it seems to be printing the serial number within the barcode! :slightly_smiling_face:

Full working code for solution:

def printLabel(serialNum, printerName):
	from javax.print import PrintService, PrintServiceLookup, DocFlavor, SimpleDoc
	from java.lang import System
	from org.python.core.util import StringUtil
	from java.net import Socket
	from java.io import DataOutputStream
	  	
	foundService = False
  	printServices = PrintServiceLookup.lookupPrintServices(None, None) 
	for printService in printServices:
	  	print("Made it in loop")
	  	if printerName in printService.toString(): 
	  		              foundService = True 
	  		              # break
	  	
		if foundService:
			print('Found serv')
	  		strMessage100 = '^XA'
	  		strMessage112 = '^FO50,50^B3N,N,100,Y,N^FD%s^FS' % serialNum
	  		strMessage113 = '^XZ'
	  		strOut = strMessage100+strMessage112+strMessage113
	
	  		
	  		
	  		
	  		job = printService.createPrintJob()
	  		flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE
			byteArray = StringUtil.toBytes(strOut) 
	  		doc = SimpleDoc(byteArray, flavor, None) 
	  		job.print(doc, None)