打印开始时如何打开CashDrawer [英] How to Open CashDrawer when Printing starts

查看:133
本文介绍了打印开始时如何打开CashDrawer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我是创建POS系统的新手。



我想在打印开始时打开我的现金抽屉。我在互联网上找到了一些代码,但它们似乎没有用。它总是给我这个错误:对象引用没有设置为对象的实例。



这是我的代码:

Hi! I am new in creating POS systems.

I want to open my cash drawer when printing starts. I have found some codes in the internet but they do not seem to work. It always gives me this error: Object reference not set to an instance of an object.

This is my codes:

using Microsoft.PointOfService;

CashDrawer myCashDrawer = null;
       PosExplorer explorer;

        public void CashDrawerClass()
        {
            try
            {
                explorer = new PosExplorer();
                DeviceInfo ObjDevicesInfo = explorer.GetDevice("C4141");
                bool check = ObjDevicesInfo.IsDefault;
                myCashDrawer = (CashDrawer)explorer.CreateInstance(ObjDevicesInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show("CashDrawerClass: " + ex.Message);
            }
        }

        public void OpenCashDrawer()
        {
            try
            {
                myCashDrawer.Open();
                myCashDrawer.Claim(1000);
                myCashDrawer.DeviceEnabled = true;
                myCashDrawer.OpenDrawer();
                myCashDrawer.DeviceEnabled = false;
                myCashDrawer.Release();
                myCashDrawer.Close();
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("OpenCashDrawer: " + ex.Message);
            }
        }





现金抽屉已连接到我的打印机。



The cash drawer is connected to my printer.

推荐答案

如果在构造函数中添加一些验证代码,您可能会获得更多信息。

或者使用调试器逐行逐步执行构造函数。



您应该添加一些支票:



1. ObjDevicesInfo 获取值或是否为null?

If you add some validation code in the constructor you might get more info.
Or use the debugger to step through the constructor line by line.

You should add some checks:

1. Is ObjDevicesInfo getting a value or is it null?
if (ObjDevicesInfo == null)
   Report an error.





2.检查 myCashDrawer 被赋予一个值。


总是存在从网上复制一些代码的危险..你所显示的代码中没有很多,并且你不会说那个错误从哪里发出,所以....



...好吧,假设错误来自



Thats always the danger of copying bits of code off the net .. there's not a lot in the code you've shown, and you dont say where that error is emitted from, so ....

...ok, assuming the error comes from

myCashDrawer.Open();





当你调用OpenCashDrawer()时,最明显的事情,除了看起来有点凌乱的设计/实现方面,我看不到任何类似的东西





when you call OpenCashDrawer(), the most obvious thing, apart from it looking a bit messy design/implementation-wise, is I dont see anything that looks like

myCashDrawer = new CashDrawer();





可能将参数传递给CashDrawer对象的实例化



possibly with arguments being passed into the instantiation of the CashDrawer Object


这篇关于打印开始时如何打开CashDrawer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆