# Printing a label from a Zebra ZT230 using USB connection

**URL:** <https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [April 14, 2023, 3:00pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462 "2023-04-14T15:00:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 14, 2023, 3:00pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/1 "2023-04-14T15:00:26Z")

</div>

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](https://forum.inductiveautomation.com/t/printing-barcode-to-zebra-printer/8957/26).  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/5/35fa5c604d84e46f86c3e7a2fe383c1ddd3b771c.png)

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/e/be2b675dbc334c40c72a66b8f97edf3ea3f0708e.png)

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 14, 2023, 3:11pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/2 "2023-04-14T15:11:19Z")

</div>

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

(I recommend not using the same name.)

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 14, 2023, 3:25pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/3 "2023-04-14T15:25:04Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 14, 2023, 3:55pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/4 "2023-04-14T15:55:26Z")

</div>

- 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.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 14, 2023, 3:59pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/5 "2023-04-14T15:59:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 14, 2023, 4:21pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/6 "2023-04-14T16:21:09Z")

</div>

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](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:

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 14, 2023, 4:41pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/7 "2023-04-14T16:41:15Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 14, 2023, 5:53pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/8 "2023-04-14T17:53:08Z")

</div>

> [@Raymond\_Sipes](#):
>
> now I need to figure out why it isn't printing once I run the script!

My suspicion:

> [@JordanCClark](#):
>
> 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.

Specifically, the user that the gateway service runs under.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 14, 2023, 5:59pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/9 "2023-04-14T17:59:45Z")

</div>

Gotcha,

Would that be in the Script Hint Scope? I have it set to all.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/8/b80f4507c147c7a491453ee46706e578df21d93e.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 14, 2023, 6:47pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/10 "2023-04-14T18:47:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 16, 2023, 12:16am UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/11 "2023-04-16T00:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 17, 2023, 1:43pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/12 "2023-04-17T13:43:57Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/e/8e176655235cefd0758989c8ac3ff4efa43aeaff.png)

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.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 17, 2023, 1:44pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/13 "2023-04-17T13:44:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 17, 2023, 1:47pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/14 "2023-04-17T13:47:38Z")

</div>

> [@Raymond\_Sipes](#):
>
> would using the perspective workstation be a good way to test?

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

> [@Raymond\_Sipes](#):
>
> I am using the script console at the moment to be able to view the print statements.

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. }

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 17, 2023, 1:55pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/15 "2023-04-17T13:55:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 19, 2023, 2:52pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/16 "2023-04-19T14:52:04Z")

</div>

Hello, all!

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 19, 2023, 2:54pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/17 "2023-04-19T14:54:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 19, 2023, 2:57pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/18 "2023-04-19T14:57:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 19, 2023, 3:14pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/19 "2023-04-19T15:14:18Z")

</div>

ZPL2 Guide (plus others)

> **[zpl-zbi2-pm-en.pdf](https://www.zebra.com/content/dam/zebra_new_ia/en-us/manuals/printers/common/programming/zpl-zbi2-pm-en.pdf)**
>
> 11.02 MB

'Playgound' Phil mentioned:

> **[Labelary Online ZPL Viewer](http://labelary.com/viewer.html)**
>
> An online ZPL viewer that allows you to easily edit and preview ZPL labels.

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [April 19, 2023, 3:17pm UTC](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/20 "2023-04-19T15:17:25Z")

</div>

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:

```auto
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! 🙂

Full working code for solution:

```auto
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)
	  		

```

[Next page](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462.md?page=2)
