0

我正在构建 winforms .net 应用程序,我在网络上有一台 E-Pos 打印机,使用以下代码:在表单加载打印机初始化时:

    explorer = new PosExplorer(this);
        DeviceInfo receiptPrinterDevice = explorer.GetDevice("PosPrinter", Properties.Settings.Default.KitchenPrinter); //May need to change this if you don't use a logicial name or use a different one.
        kitchenPrinter = (PosPrinter)explorer.CreateInstance(receiptPrinterDevice);
       ConnectToPrinter();


    private void ConnectToPrinter()
    {  
        kitchenPrinter.Open();
        kitchenPrinter.Claim(10000);
        kitchenPrinter.DeviceEnabled = true;
     }

打印按钮上的函数调用:

 private void PrintReceipt()
    {

         try
            {  kitchenPrinter.PrintNormal(PrinterStation.Receipt, "test");
              }
            finally
            {

            }
    }

当我想切换到其他形式时,我调用断开功能

        DisconnectFromPrinter(kitchenPrinter);
        Reporting frm = new Reporting(curuser);
        frm.Show();
        this.Hide();


  private void DisconnectFromPrinter(PosPrinter kitchenPrinter)
    {

        try
        {
           kitchenPrinter.Release();
            kitchenPrinter.Close();
        }
       catch { }
    }

一次打印成功,下次按打印时抛出异常
Method ClaimDevice 抛出异常。试图对设备执行非法或不受支持的操作,或者使用了无效的参数值。

有什么建议吗?

4

1 回答 1

1

由于 Release 命令无效,并且可能 Claim 命令在我每次加载表单时都会引发错误,因为它之前已被声明。

所以我创建了一个名为“createPOS”的单独类

         class createPOS
{
 public  static PosExplorer explorer;
  public  static PosPrinter kitchenPrinter;
  public static void createPos()
  {
      explorer = new PosExplorer();
      DeviceInfo receiptPrinterDevice = explorer.GetDevice("PosPrinter", Properties.Settings.Default.KitchenPrinter); //May need to change this if you don't use a logicial name or use a different one.
      kitchenPrinter = (PosPrinter)explorer.CreateInstance(receiptPrinterDevice);
      kitchenPrinter.Open();
      kitchenPrinter.Claim(10000);
      kitchenPrinter.DeviceEnabled = true;
  }
      public static void Print(string text){
          if (kitchenPrinter.Claimed)
              PrintTextLine(kitchenPrinter, text);  // kitchenPrinter.PrintNormal(PrinterStation.Receipt, text ); //Print text, then a new line character.
       }
      private static void PrintTextLine(PosPrinter printer, string text)
      {
          if (text.Length < printer.RecLineChars)
              printer.PrintNormal(PrinterStation.Receipt, text + Environment.NewLine); //Print text, then a new line character.
          else if (text.Length > printer.RecLineChars)
              printer.PrintNormal(PrinterStation.Receipt, TruncateAt(text, printer.RecLineChars)); //Print exactly as many characters as the printer allows, truncating the rest, no new line character (printer will probably auto-feed for us)
          else if (text.Length == printer.RecLineChars)
              printer.PrintNormal(PrinterStation.Receipt, text + Environment.NewLine); //Print text, no new line character, printer will probably auto-feed for us.
      }
      private static string TruncateAt(string text, int maxWidth)
      {
          string retVal = text;
          if (text.Length > maxWidth)
              retVal = text.Substring(0, maxWidth);

          return retVal;
      }
  }

并且在登录表单上,只有在我初始化打印机后才能访问它

     createPOS.createPos();

在 MainForm 上我调用了打印方法:

        createPOS.Print("This allows me to Print Several times");

这样我就可以打印多次,甚至可以导航到其他表单并返回正常工作。

谢谢你们。

于 2017-03-03T17:30:03.827 回答