Reputation: 63
When attempting to initialize and then print on a POS receipt printer, my program throws a ArgumentNullExeption, stating that the "device" parameter cannot be null. Here's the code:
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;
Any ideas for a fix?
Upvotes: 0
Views: 1435
Reputation: 15237
Assuming that the error is on this line:
PosPrinter printer = (PosPrinter)posExplorer.CreateInstance(receiptPrinterDevice);
Then the problem is that GetDevice is returning null. That would mean that what you're requesting isn't available to the system.
There could be numerous reasons for that. I would look at your receiptPrinterDevices
collection and see if what you're trying to get is in there. If it is then you need to figure out why you're not referring to it correctly. For instance, be aware that the second parameter of GetDevice (where you passed in "POS58"
) needs to be the Logical Name of the device, not the Service Name (from the docs: "Before you use GetDevice, logical names must be configured for the device by using either PosDm.exe or WMI. You cannot pass the service object name for logicalName") On the other hand, if the device is not in the collection, then you'll need to figure out why that is - not plugged in, not configured correctly, running as x64 when it is only an x86 service object, etc.
Upvotes: 2