0

I'm writing an application that uses Pos for .Net, and I'm noticing that if you call WaitForDrawerClose, then you won't be able to print receipts until it has returned.

This is not desirable behavior. Is there another way to wait for the cash drawer to close without blocking the printer?

I've looked into the OnDrawerStateChanged Event, but that is a protected member of CashDrawerBase, and I'm not entirely sure how to access it.

Here is my SSCCE:

static void Main(string[] args)
{
    var posExplorer = new PosExplorer();
    var waitTask = WaitForCloseAsync(posExplorer);
    System.Threading.Thread.Sleep(500);
    PrintText(posExplorer);
    waitTask.Wait();
}

public static Task WaitForCloseAsync(PosExplorer posExplorer)
{
    var result = Task.Factory.StartNew(() =>
    {
        Console.WriteLine("waiting");
        var cashDrawer = GetCashDrawer(posExplorer);
        cashDrawer.Open();
        cashDrawer.Claim(1000);
        cashDrawer.DeviceEnabled = true;
        cashDrawer.WaitForDrawerClose(10000, 4000, 500, 5000);
        cashDrawer.Release();
        cashDrawer.Close();
        Console.WriteLine("waited");
    });
    return result;
}

public static void PrintText(PosExplorer posExplorer)
{
    Console.WriteLine("printing");
    var printer = GetPosPrinter(posExplorer);
    printer.Open();
    printer.Claim(1000);
    printer.DeviceEnabled = true;
    var text = "abc\x1B|1lF";
    printer.PrintNormal(PrinterStation.Receipt, text);
    printer.Release();
    printer.Close();
    Console.WriteLine("printed");
}

public static CashDrawer GetCashDrawer(PosExplorer posExplorer)
{
    var deviceInfo = posExplorer.GetDevices(DeviceCompatibilities.Opos)
        .Cast<DeviceInfo>()
        .Where(d => d.Type == "CashDrawer")
        .ToList();

    var device = deviceInfo.FirstOrDefault(d => d.Compatibility == DeviceCompatibilities.Opos);

    if (device == null)
    {
        return null;
    }
    else
        return (CashDrawer)posExplorer.CreateInstance(device);
}

private static PosPrinter GetPosPrinter(PosExplorer posExplorer)
{
    var deviceInfo = posExplorer.GetDevices(DeviceCompatibilities.Opos)
        .Cast<DeviceInfo>()
        .Where(d => d.Type == "PosPrinter")
        .ToList();

    var device = deviceInfo.FirstOrDefault(d => d.Compatibility == DeviceCompatibilities.Opos);

    if (device == null)
    {
        return null;
    }
    else
    {
        return (PosPrinter)posExplorer.CreateInstance(device);
    }
}

1 Answer 1

0

so, what I did was essentially this: Instead of using (WaitForClose), I just poll it like this:

        for (var i = 0; i < 15; i++)
        {
            cashDrawer = GetCashDrawer(posExplorer);
            cashDrawer.Open();
            cashDrawer.Claim(1000);
            cashDrawer.DeviceEnabled = true;
            if (!cashDrawer.DrawerOpened)
            {
                Console.WriteLine("waited");
                return;
            }                    
            cashDrawer.Release();
            cashDrawer.Close();
            System.Threading.Thread.Sleep(1500);
        }
        Console.WriteLine("timed out");

It's not ideal, but it doesn't lock the printer up either, so It'll have to to for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.