简体   繁体   English

WPF C# POS 打印机,从下拉菜单中打开设备

[英]WPF C# POS printer, open a device from a dropdown menu

I'm trying to print a simple line of text using a POS printer.我正在尝试使用 POS 打印机打印一行简单的文本。 Currently i do not have an actual pos printer hooked up but i do have the microsoft pos printer simulator.目前我没有连接真正的 pos 打印机,但我有微软 pos 打印机模拟器。

Currently i have a combobox control that will list all found posprinters.目前我有一个组合框控件,可以列出所有找到的 posprinters。 The idea is that in this combobox i select the printer that i want and then it will use that printer to print the text.这个想法是在这个组合框中我选择我想要的打印机,然后它将使用该打印机打印文本。

The code to get all the printers and add them to the combobox is the following code:获取所有打印机并将它们添加到组合框的代码如下:

// 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);
        }

Then i have a button control to print a sample line of text, the code for the button press event is the following:然后我有一个按钮控件来打印一行文本示例,按钮按下事件的代码如下:

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());
        }

    }

And lastely the selectedPrinter object is defined as this:最后, selectedPrinter 对象定义为:

using Microsoft.PointOfService;
PosPrinter selectedPrinter;

The error that i get in the for loop in the button click event is that the printer[i] returns a DeviceInfo object rather than the device itself.我在按钮单击事件的 for 循环中得到的错误是打印机 [i] 返回一个 DeviceInfo 对象而不是设备本身。 So i would have to retreive the device and afterwards claim it and open it i assume.所以我将不得不取回设备,然后声称它并打开它我假设。 However i have no clue how to get the actual device and open it.但是我不知道如何获取实际设备并打开它。

Thanks in advance!提前致谢!

I figured it out, u can initialize the printer using the posexplorer class by feeding it the device info.我想通了,您可以通过向打印机提供设备信息来使用 posexplorer 类初始化打印机。 If anyone else ran into this issue, i changed the code and it works for me now :) I also added some checks to make sure that the printer is actually ready to print.如果其他人遇到此问题,我更改了代码,现在它对我有用:) 我还添加了一些检查以确保打印机实际上已准备好打印。 Thanks for the replies and happy holidays!谢谢回复,节日快乐!

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());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM