Zebra GK420d Communication Using Zebra OPOS Driver
I am using a Zebra GK420d label printer for a POS application I am developing. I am trying to communicate with a printer through its OPOS drivers provided by Zebra. But I'm in trouble. It is a simple form in Visual Basic 2008 with a button on it. Here is the complete code I am running.
Public class FrameStep1 Inherits System.Windows.Forms.Form
Private m_Printer As Microsoft.PointOfService.PosPrinter = Nothing
Private Sub ChangeButtonStatus()
'Disable control.
btnPrint.Enabled = False
End Sub
Private Sub FrameStep1_Load(ByVal sender As System.Object _
, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strLogicalName As String
Dim deviceInfo As DeviceInfo
Dim posExplorer As PosExplorer
strLogicalName = "zebra"
posExplorer = New PosExplorer
m_Printer = Nothing
Try
deviceInfo = posExplorer.GetDevice(DeviceType.PosPrinter, strLogicalName)
m_Printer = posExplorer.CreateInstance(deviceInfo)
Catch ex As Exception
ChangeButtonStatus()
Return
End Try
Try
m_Printer.Open()
m_Printer.Claim(1000)
m_Printer.DeviceEnabled = True
Catch ex As PosControlException
ChangeButtonStatus()
End Try
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
m_Printer.PrintNormal(PrinterStation.Receipt, "Hello OPOS for .Net" + vbCrLf)
Catch ex As PosControlException
End Try
End Sub
Private Sub FrameStep1_Closing(ByVal sender As Object _
, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If m_Printer Is Nothing Then
Return
End If
Try
m_Printer.DeviceEnabled = False
m_Printer.Release()
Catch ex As Exception
Finally
m_Printer.Close()
End Try
End Sub
End class
You can see that I called claim () and set DeviceEnabled = true. However, when I debug what is happening, since the control goes through m_Printer.Open (), it magically ends up at btnPrint_Click () and doesn't go any further unless I click a button on my form and then on m_Printer .PrintNormal () it breaks and throws a POSControlException, and the text in it reads "An attempt was made to access a device of exclusive use, which must be declared before the action of a method or property can be used."
I seem to be doing something wrong.
source to share
You can try this:
if (m_Printer.State == ControlState.Closed)
{ m_Printer.Open(); }
if (!m_Printer.Claimed)
{ m_Printer.Claim(0);}
if (!m_Printer.DeviceEnabled)
{ m_Printer.DeviceEnabled = true;}
Printer.PrintNormal(PrinterStation.Receipt, text);
Printer.CutPaper(100);
Also remember that some ZEBRA printers wait for the paper cutter before printing. In the meantime, there is no need to know about it. ”
source to share