我正在使用Zebra GK420d标签打印机来开发我正在开发的POS应用程序。我试图通过Zebra提供的OPOS驱动程序与打印机进行通信。但是我遇到了麻烦。它是visual basic 2008中的一个简单形式,上面有一个按钮。这是我正在运行的完整代码。
公共类FrameStep1 继承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
结束班
你可以看到我调用了claim()并设置了DeviceEnabled = true。然而,当我调试它时发生的事情是当控件通过m_Printer.Open()时它神奇地结束于btnPrint_Click()并且不再继续,除非我单击我的表单上的按钮然后在m_Printer.PrintNormal()它中断和抛出POSControlException,其中的文本显示“尝试访问必须在使用方法或属性集操作之前声明的专用设备。”
我在这里似乎做错了什么。
答案 0 :(得分:1)
你可以试试这个:
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);
另请记住,在开始打印之前,有些ZEBRA打印机会等待切纸器。