0

I am building winforms .net Application , I have a E-Pos printer on Network , using this Code below : On Form Loading Printer initializing :

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

Function Call on Print Button :

 private void PrintReceipt()
    {

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

            }
    }

When I want to Switch to Other Form I call Disconnect Function

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


  private void DisconnectFromPrinter(PosPrinter kitchenPrinter)
    {

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

It prints successful one time ,when pressing to print next time it throws and exception
Method ClaimDevice threw an exception. Attempt was made to perform an illegal or unsupported operation with the device, or an invalid parameter value was used.

any suggestion ?

  • could you retry without the `DeviceEnabled = false` assignment? related: http://stackoverflow.com/questions/22430995/cant-claim-the-posprinter; doc https://social.msdn.microsoft.com/Forums/en-US/ec9736fb-42ff-4477-9dbd-e3af83fd14db/what-is-proper-way-of-using-posprinter?forum=posfordotnet (devices may be shareable) – Cee McSharpface Mar 03 '17 at 08:23
  • [release implies disable. close implies release](https://msdn.microsoft.com/en-us/library/dd161612(v=winembedded.10).aspx). code seems to be correct, may be a driver/device problem. – Cee McSharpface Mar 03 '17 at 08:26
  • i try to open ,claim, enable in form loading out side my button click and printing many times on button click and releasing it when switchin to other form. but once i come back to my form it hangs again. so it seems that releasing function is not working good?? – Tamer Hatoum Mar 03 '17 at 09:06
  • yes that may be. unfortunately `Release` is void... no info about success of the operation. "hang" = exception mentioned in your post? could you update the code so we see the big picture, including form/button interaction? – Cee McSharpface Mar 03 '17 at 11:23
  • hang yes means the above mentioned exception.. question been edited – Tamer Hatoum Mar 03 '17 at 11:37
  • attach a debugger and prove that `ConnectToPrinter` is called after `DisconnectFromPrinter` and before subsequent `PrintReceipt` – Cee McSharpface Mar 03 '17 at 11:41
  • yes i have checked and been called propably. do you think its about claim (10000) parameter value ? – Tamer Hatoum Mar 03 '17 at 11:59
  • You'll have to call CreateInstance() again. Calling Close() was a bad idea, just don't do that. If you want to print from other forms as well then simply declare kitchenPrinter static. – Hans Passant Mar 03 '17 at 14:12
  • since form is loading that means the new instance is created .what you are telling have no sense – Tamer Hatoum Mar 03 '17 at 15:16

1 Answers1

1

Since the Release command is not being effective and may Claim command is throwing an error every time I am loading my form because it is being Claimed before.

So I have Create a separate Class Called "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;
      }
  }

and On the Login form that it will only be accessed once I have initialized my printer

     createPOS.createPos();

and on MainForm I have called the Printing Method :

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

on that way I am able to print several times and even I navigate to other forms and come back that works fine.

Thank you guys.

  • What documentation or project did you follow to achieve this code? I am unsure whether you are supposed to claim the device each time you are going to use it, or the whole livetime of the app. I always get an exception when I do claim. – Dzyann Jun 03 '21 at 00:21