Microsoft point of sales for .net solution's can't create new PosExplorer Object

Lasitha Lankajeewa 0 Reputation points
2024-04-17T12:11:00.1366667+00:00

I'm Trying to use Microsoft point of sales for .net 1.14.1 version and trying to use it's Cash drawer capabilities for my Windows form application. But when i'm trying to create PosExplorer new object it throws "System.NullReferenceException: 'Object reference not set to an instance of an object.'" Error. I installed Microsoft point of sales for .net 1.14.1 runtime for my pc as well but i couldn't find the issue in here. below i have mentioned the relevant code segment for this issue.

PosExplorer explorer = new PosExplorer();

// Find the first connected cash drawer

DeviceCollection cashDrawerDevices = explorer.GetDevices(DeviceType.CashDrawer);

if (cashDrawerDevices.Count == 0)

{

MessageBox.Show("No cash drawer found.");

return;

}

DeviceInfo cashDrawerInfo = cashDrawerDevices[0];

// Claim the cash drawer

_cashDrawer = (CashDrawer)explorer.CreateInstance(cashDrawerInfo);

if (_cashDrawer == null)

{

MessageBox.Show("Failed to initialize cash drawer.");

return;

}

_cashDrawer.Open();

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,830 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,385 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 31,981 Reputation points Microsoft Vendor
    2024-04-18T09:36:23.16+00:00

    Hi @Lasitha Lankajeewa , Welcome to Microsoft Q&A,

    The following is the official example given by the official:

    The following code example demonstrates how to create an instance of PosExplorer, connect to Plug and Play events, and use it to identify all connected Magnetic Stripe Reader (MSR) devices. The code example prints information about the MSR to the console and closes the device after it has finished.

    // Creates a new instance of an MSR.
    void CreateMsr(DeviceInfo msrinfo)
    {
        msr = (Msr)explorer.CreateInstance(msrinfo);
        msr.Open();
        msr.Claim(1000);
        msr.DeviceEnabled = true;
    }
    
    static void Main(string[] args)
    {
    
        // Create a new instance of PosExplorer and use it to
        // collect device information.
        PosExplorer explorer = new PosExplorer();
        DeviceCollection devices = explorer.GetDevices();
    
        // Search all connected devices for an MSR, print its service
        // object name to the console, and close it when finished.
        foreach (DeviceInfo device in devices)
        {
          if (device.Type == DeviceType.Msr)
          {
             if (device.ServiceObjectName == currentMsr)
             {
                CreateMsr(device);
                Console.WriteLine(device.ServiceObjectName);
    
                // It is important that applications close all open
                // Service Objects before terminating.
                msr.Close();
                msr = null;
             }
          }
        }
    }
    

    Although you mention that the Microsoft Point of Sale .NET 1.14.1 runtime is installed, make sure it is installed correctly and is compatible with your application.

    Make sure your project references the correct versions of Microsoft.Pos.Common.dll and Microsoft.PointOfService.dll and that these files are found and loaded correctly . Make sure your application has sufficient permissions to access the POS device. Sometimes, insufficient permissions can cause similar problems.

    Make sure your cash drawer is properly connected to the computer and is being recognized properly.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments