Saeed Khan
Saeed Khan

Reputation: 537

PosExplorer.GetDevice() returns Always null in C#.net Windows form Application

I am working on Thermal Receipt Printer (ARP-990KE) for project where i tried below code for printing invoice but in code GetDevice() give me error Value cannot be null.Parameter name: device

       PosExplorer posExplorer = new PosExplorer(this);

       DeviceInfo receiptPrinterDevice = posExplorer.GetDevice("Generic/Text Only"); 
       return (PosPrinter)posExplorer.CreateInstance(receiptPrinterDevice);  //  Here it gives me null 

Upvotes: 0

Views: 4080

Answers (1)

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

The GetDevice seems to be confused by "Generic/Text Only" you're passing it.

The PosExplorer.GetDevice(String) method (i.e. the one accepting single parameter) returns a default device for the given device class. The device class should be one of the constants from DeviceType class (DeviceType.PosPrinter in your case). This is what GetDevice(String) expects as its parameter:

DeviceInfo receiptPrinterDevice = posExplorer.GetDevice(DeviceType.PosPrinter);

Please note, if you're going to use this method, make sure you've set up your printer as default (or there is no other devices of this type).

You may wish to consider using a more generic alternative. PosExplorer.GetDevice(String, String) accepts a device name in its second parameter so you're not bound to the default device only:

DeviceInfo receiptPrinterDevice = posExplorer.GetDevice(DeviceType.PosPrinter, <device name>);

The <device name> can be figured out using

"C:\Program Files (x86)\Microsoft Point Of Service\posdm.exe" listdevices /type:PosPrinter

Upvotes: 1

Related Questions