2

我正在为我正在开发的 POS 应用程序使用 Zebra GK420d 标签打印机。我正在尝试通过 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 并且其中的文本显示为“尝试访问必须声明的专用设备,然后才能使用方法或属性设置操作。”

我似乎在这里做错了什么。

4

1 回答 1

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 打印机会在开始打印之前等待切纸机。

于 2011-03-21T20:02:04.523 回答