【问题标题】:WPF C# POS printer, open a device from a dropdown menuWPF C# POS 打印机,从下拉菜单中打开设备
【发布时间】:2020-04-07 07:52:09
【问题描述】:

我正在尝试使用 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 对象而不是设备本身。所以我必须取回设备,然后认领它并打开它,我假设。但是我不知道如何获取实际设备并打开它。

提前致谢!

【问题讨论】:

标签: c# wpf printing pos pointofservice


【解决方案1】:

我想通了,您可以使用 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());
        }
    }

【讨论】:

    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 2014-11-16
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多