Microsoft POS C# - 在DeviceInfo中获取空值

时间:2015-06-09 16:47:36

标签: c# printing

当尝试初始化然后在POS收据打印机上打印时,我的程序会抛出一个ArgumentNullExeption,说明"设备"参数不能为null。这是代码:

PosExplorer posExplorer = new PosExplorer();
DeviceCollection receiptPrinterDevices = posExplorer.GetDevices(DeviceType.PosPrinter);
DeviceInfo receiptPrinterDevice = posExplorer.GetDevice(DeviceType.PosPrinter, "POS58");
PosPrinter printer = (PosPrinter)posExplorer.CreateInstance(receiptPrinterDevice);

printer.Open();
printer.Claim(5000);
printer.DeviceEnabled = true;
printer.PrintNormal(PrinterStation.Receipt, cmds);
printer.DeviceEnabled = false;

有关修复的想法吗?

1 个答案:

答案 0 :(得分:1)

假设错误在这一行:

PosPrinter printer = (PosPrinter)posExplorer.CreateInstance(receiptPrinterDevice);

然后问题是GetDevice返回null。这意味着您要求的内容无法供系统使用。

可能有很多原因。我会查看你的receiptPrinterDevices集合,看看你想要的是什么。如果是,那么你需要找出你没有正确引用它的原因。例如,请注意GetDevice的第二个参数(您传入"POS58"的位置)需要是设备的逻辑名称,而不是服务名称(来自文档:“在使用GetDevice之前,必须使用PosDm.exe或WMI为设备配置逻辑名。您无法传递logicalName的服务对象名称“)另一方面,如果设备不在集合中,那么您将需要弄清楚为什么 - 没有插入,没有正确配置,当它只是一个x86服务对象时作为x64运行,等等。