Printing to a local client printer in a Vision client

Hi I have a non networked Zebra printer that is connected to a local PC that I will be running a vision client on.
I want to know how I could send the print command to the printer through the client directly to the computers default printer.

I have searched the forum and found many posts on sending via IP address - but not specific to a local printer connected to the PC running the vision client.

Thanks

You should be able to use system.print.createPrintJob() to configure a print job to the printer.

https://docs.inductiveautomation.com/display/DOC81/system.print.createPrintJob

3 Likes

I you want to send ZPL directly, you will need to open the local port for the printer (as if a file) instead of a socket like the network examples. If the client is on Windows, you will need to drill into the printer settings to get the port name.

Ok I have had a look at the client PC and the Local printer settings shows port as USB001
could you assist with an example script for opening the network port name and sending to the printer?

please see my script currently setup for a network printer below

from java.net import Socket
from java.io import DataOutputStream

RollNum = event.source.parent.getComponent('Roll_No_Numeric Text Field').intValue
width = event.source.parent.getComponent('Width_Numeric Text Field').floatValue
Supplier = event.source.parent.getComponent('Supplier_Dropdown').selectedStringValue
SupplierRoll = event.source.parent.getComponent('Supplier_Piece_No_Text Field').text
PurchaseOrder = event.source.parent.getComponent('POText Field').text
Length = event.source.parent.getComponent('Length_Numeric Text Field').floatValue
Fabric = event.source.parent.getComponent('Dropdown').selectedStringValue
SupplierPiece = event.source.parent.getComponent('Supplier_Piece_No_Text Field').text

strMessage100 ="^XA"
StrMessage110 ="^CF0,90"
StrMessage111 ="^FO35,50^FD%s^FS" % Fabric
strMessage112 ="^CF0,50"
StrMessage113 ="^FO30,125^FDWIDTH:%s ^FS" % width
StrMessage114 ="^BY04,1,165"
StrMessage115 = "^CF0,50"
StrMessage116 = "^FO30,180^BC^FD%s^FS" % RollNum
StrMessage117 = "^FO30,600^FDSUPPLIER:%s ^FS" % Supplier
StrMessage118 = "^FO30,700^FDPURCHASE ORDER: %s ^FS" % PurchaseOrder
StrMessage119 = "^FO30,800^FDLENGTH:%s ^FS" % Length
StrMessage120 = "^FO30,500^FDSUPPLIER PIECE : %s ^FS" %SupplierPiece
StrMessage121 = "^XZ"



strOut = strMessage100+StrMessage110+StrMessage111+strMessage112+StrMessage113+StrMessage114+StrMessage115+StrMessage116+StrMessage117+StrMessage118+StrMessage119+StrMessage120+StrMessage121
  
  
printerIP='172.016.007.133'
port=9100
 
try:
  # Open Socket Connection
  clientSocket=Socket(printerIP,port)
  #Open data output stream
  outToPrinter=DataOutputStream(clientSocket.getOutputStream())
  #Send Data to Printer
  outToPrinter.write(strOut)
  #close data stream and socket
  outToPrinter.close();
  clientSocket.close();
except IOError:
  print "Error"

type or paste code here

You have exhausted my knowledge of Windows printers. (Because I avoid Windows like the plague.)

3 Likes

I had this done up yesterday, but then got yanked to go to to one of our sister plants. :roll_eyes:

I prefer looking up the printer by name, because somehow, somewhere, someone will add another printer and make it the default.

from javax.print import DocFlavor, SimpleDoc, PrintServiceLookup
from java.io import ByteArrayInputStream

# Get all available printers
printServices = PrintServiceLookup.lookupPrintServices(None, None)

# Initialize print service variable
printService = None

# Check for specific printer name
for service in printServices:
	if service.name == 'Label Printer': # Replace with your own printer name.
		printService = service

# Send message if the printer exists
if printService is not None:
	fgPartNum = '4CJ9196WA65'
	serialNum = 74889000
	strMessage = '^XA^BY3,3,167^FT040,280,^B3N,N,,N,N^FDS{sn}^FS^FT0060,080^A0N,40,45^FDS{sn}    {pn}^FS^PQ1,0,0,N^XZ'.format(sn=serialNum, pn=fgPartNum)
	
	# Create an input stream
	messageStream = ByteArrayInputStream(strMessage)
	flavor = DocFlavor.INPUT_STREAM.AUTOSENSE
	doc = SimpleDoc(messageStream, flavor, None)
	
	job = printService.createPrintJob()
	job.print(doc, None)
	messageStream.close()
	
else:
	print 'Printer Not Found'
4 Likes

@batman01793, I had forgotten to mention (though you probably alread know) is that the printer driver needs to be the Generic/Text Only one.

1 Like

This works perfect from the Script Console, as well as from a vision button action calling from the project library but it will not print when called from a tag change event, it will go into the printer queue but will Error and not fully process, will create the file but will not contain any text. I have tried putting all the code directly in the tag change event with the same result.

Any insights as to what could be causing this?

What kind of tag change event? Only a Tag Change defined under Vision Client Events will have access to a local printer.

Tag value change in a new type tag UDT, tested through 1 insrance of the UDT, not in the client event script specifically. I set up the code to print each printer it finds in the wrapper log, inwhich in finds and prints all those configured.

If this is caused by not directly using the vision client script is there a way to incorporate the UDT tag change into the client script so I do not need to add every tag instance I create?

Nope. Gateway scope is gateway scope, while Vision client scope is Vision client scope.

You may be able to print to network printers from gateway scope, as long as they are actually configured for the gateway's service user. The gateway is not going to print to a Vision client's locally-attached printer.

1 Like

Would it make a difference that im using Ignition Edge (Trial, latest version). Your comment on an IP printer made me think of that, Im not actually using an Ignition Server on some remote Server and a local vision client, will all be on the same local PC.

Was hoping that I could call a Project library script from the UDT.

The Print queue shows the Print Job as comming from SYSTEM, and the ignition server service is set to run as Local.

Printer configurations are per-user in Windows. You may need a Windows person help you. (I avoid Windows where I can.)

The down and dirty method may be to bind a vision client tag to the gateway tag (to avoid tryng to set up a printer for the gateway service to use) and trigger the script from the Client tag trigger a Vision Client Tag Event.

Got it working, was due to the Windows User Issue as mentioned, Changed the "Logged on As" account for the Ignition Service to use the same user account and password as the PC logon. Has been working fine and spinning out print jobs as fast as Ignition can supply. Thanks!

2 Likes