0

我正在尝试使用 POS 打印机打印一行简单的文本。目前我没有连接实际的 pos 打印机,但我有 microsoft pos 打印机模拟器。

目前我有一个组合框控件,它将列出所有找到的 posprinters。这个想法是,在这个组合框中,我选择我想要的打印机,然后它将使用该打印机打印文本。

获取所有打印机并将它们添加到组合框的代码如下:

// Get available printers and add them to the combobox in the settings menu
        PosExplorer posExplorer = new PosExplorer();
        DeviceCollection printers = posExplorer.GetDevices("PosPrinter");
       for (int i = 0; i < printers.Count; i++)
        {
            settings_ComboBox_Printer.Items.Add(printers[i].ServiceObjectName);
        }

然后我有一个按钮控件来打印示例文本行,按钮按下事件的代码如下:

private void ButCart_PrintOrder_Click(object sender, RoutedEventArgs e)
    {
        PosExplorer posExplorer = new PosExplorer();

        // Get connected printer devices
        DeviceCollection printers = posExplorer.GetDevices("PosPrinter");
        for (int i = 0; i < printers.Count; i++)
        {
            if (printers[i].ServiceObjectName == settings_ComboBox_Printer.Text)
            {
                selectedPrinter = (PosPrinter)printers[i];
            }
        }

        try
        {
            string text = "test text";
            selectedPrinter.PrintNormal(PrinterStation.Receipt, text);
        } catch (Exception ae)
        {
            MessageBox.Show("An error occured: " + ae.ToString());
        }

    }

最后 selectedPrinter 对象定义如下:

using Microsoft.PointOfService;
PosPrinter selectedPrinter;

我在按钮单击事件的 for 循环中得到的错误是打印机 [i] 返回一个 DeviceInfo 对象而不是设备本身。所以我必须取回设备,然后认领它并打开它,我假设。但是我不知道如何获取实际设备并打开它。

提前致谢!

4

1 回答 1

0

我想通了,你可以通过提供设备信息来使用 posexplorer 类初始化打印机。如果其他人遇到此问题,我更改了代码,它现在对我有用:) 我还添加了一些检查以确保打印机实际上已准备好打印。感谢您的回复,祝您节日快乐!

private void ButCart_PrintOrder_Click(object sender, RoutedEventArgs e)
    {
        PosExplorer posExplorer = new PosExplorer();

        // Get connected printer devices
        DeviceCollection printers = posExplorer.GetDevices(DeviceType.PosPrinter);
        for (int i = 0; i < printers.Count; i++)
        {
            if (printers[i].ServiceObjectName == settings_ComboBox_Printer.Text)
            {
                selectedPrinter = posExplorer.CreateInstance(printers[i]) as PosPrinter;
            }
        }

        try
        {
            selectedPrinter.Open();
            if (!selectedPrinter.Claimed)
            {
                selectedPrinter.Claim(0);
                selectedPrinter.DeviceEnabled = true;
            }

            bool printerReady = true;
            if (selectedPrinter.CoverOpen)
            {
                MessageBox.Show("Printer cover is open.");
                printerReady = false;
            }
            if (selectedPrinter.PowerState == PowerState.OffOffline)
            {
                MessageBox.Show("Printer is has no power or is offline");
                printerReady = false;
            }
            if (selectedPrinter.State != ControlState.Idle)
            {
                MessageBox.Show("Printer is busy");
                printerReady = false;
            }

            if (printerReady)
            {
                string text = "test text";
                selectedPrinter.PrintNormal(PrinterStation.Receipt, text);
            }

        }
        catch (Exception ae)
        {
            MessageBox.Show("An error occured: " + ae.ToString());
        }
    }
于 2019-12-13T21:19:48.113 回答