I am trying to send ZPL commands to a Zebra ZT230 printer. The printer and drivers are installed and the printer port is "USB003". The PC communicates perfectly with the printer via either Zebra Printer Setup Utilities or ZebraDesign. I tried the following code:
Private Declare Function CreateFile Lib "kernel32.dll" (ByVal lpFileName As String, ByVal dwDesiredAccess As FileAccess, _
ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As FileMode, _
ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As IntPtr) As System.Runtime.InteropServices.SafeHandle
' Command to be sent to the printer
Dim command As String = "^XA^FO10,10,^AO,30,20^FDFDTesting^FS^FO10,30^BY3^BCN,100,Y,N,N^FDTesting^FS^XZ"
' Create a buffer with the command
Dim buffer() As Byte = New Byte((command.Length) - 1) {}
buffer = System.Text.Encoding.ASCII.GetBytes(command)
' Use the CreateFile external func to connect to the LPT1 port
Dim printer As System.Runtime.InteropServices.SafeHandle = CreateFile("USB003:", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero)
' Aqui verifico se a impressora � v�lida
If (printer.IsInvalid = true) Then
Return
End If
' Open the filestream to the lpt1 port and send the command
Dim lpt1 As FileStream = New FileStream(printer, FileAccess.ReadWrite)
lpt1.Write(buffer, 0, buffer.Length)
' Close the FileStream connection
lpt1.Close
I get an error on this line: Dim printer As System.Runtime.InteropServices.SafeHandle = CreateFile("USB003:", FileAccess.ReadWrite, 0, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero)
The error message and stack trace is:
Cannot marshal 'return value': Returned SafeHandles cannot be abstract.
at PerfectPotatoInventory.vb.PotatoGlobals.CreateFile(String& lpFileName, FileAccess dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile) at PerfectPotatoInventory.vb.PotatoGlobals.PrintLabelsTest() in C:\Potatoes\PerfectPotatoInventory.vb\PerfectPotatoInventory.vb\PotatoGlobals.vb:line 798
I am working in VB.net using VS2008 with target x86 and .Net 3.5.
Please advise what I am doing wrong.
Add this class to your project
Then in your code, call the following:
Using the Zebra .Net SDK you can call the following function: PrinterUtil.SendContents Method (String, String)
Where the first string is the ZPL to send and the second string is the printer connection string. More detail can be found here: http://techdocs.zebra.com/link-os/2-14/pc_net/