新PosExplorer()命令抛出空引用异常
本文关键字:引用 异常 命令 PosExplorer | 更新日期: 2023-09-27 18:11:12
我正在尝试用c#打开Posiflex USB现金抽屉。下面是代码(由其他人提供):
using System;
using System.Collections.Generic;
using Microsoft.PointOfService;
namespace MyNamespace
{
public class CashDrawerClass
{
CashDrawer myCashDrawer;
PosExplorer explorer;
public CashDrawerClass()
{
explorer = new PosExplorer();
DeviceInfo ObjDevicesInfo = explorer.GetDevice("CashDrawer", "RR-Drawer");
myCashDrawer = explorer.CreateInstance(ObjDevicesInfo) as CashDrawer;
}
public void OpenCashDrawer()
{
myCashDrawer.Open();
myCashDrawer.Claim(1000);
myCashDrawer.DeviceEnabled = true;
myCashDrawer.OpenDrawer();
myCashDrawer.DeviceEnabled = false;
myCashDrawer.Release();
myCashDrawer.Close();
}
}
}
一旦执行到代码:
explorer = new PosExplorer(),抛出以下异常:
类型为"System"的未处理异常。
mscorlib.dll中发生NullReferenceException。如果有帮助,下面是堆栈跟踪:
at Microsoft.PointOfService.Pos4NetTelemetry.IsCeipOptInEnabled()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at Microsoft.PointOfService.Pos4NetTelemetry.get_Enabled()
at Microsoft.PointOfService.Pos4NetTelemetry.SetCurrentProcessBitness()
at Microsoft.PointOfService.PosExplorer.Initialize()
at Microsoft.PointOfService.PosExplorer..ctor()
at RunningRabbit.CashDrawerClass..ctor() in c:'C# Development'RunningRabbit'RunningRabbit'CashDrawerClass.cs:line 14
at RunningRabbit.MainPOS.btnOpenDrawer_Click(Object sender, EventArgs e) in c:'C# Development'RunningRabbit'RunningRabbit'MainPOS.cs:line 261
at System.Windows.Forms.Control.OnClick(EventArgs e)
at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
at DevExpress.XtraEditors.BaseControl.WndProc(Message& msg)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at RunningRabbit.Program.Main() in c:'C# Development'RunningRabbit'RunningRabbit'Program.cs:line 20
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
任何帮助将不胜感激。
您可以在堆栈跟踪中看到它:
初始化时的posexexplorer检查您是否允许它在为. net安装POS期间向Microsoft发送遥测数据。代码正在尝试读取这个属性:
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE''POSfor.Net''Setup").GetValue("CeipOptIn", (object) 0);
所以如果你没有安装。net的POS,这个Subkey不在你的注册表中,然后抛出异常。
注意:
遥测只包含在。net 的1.14版本的POS中。我建议使用版本1.12,具有相同的功能,没有遥测