我已经使用 WPF 和 .NET Framework v:4.8 开发了一个 POS 应用程序,我的客户想要一个功能,使他能够通过单击按钮 F1 打开现金抽屉(应用程序打印时现金抽屉不会自动打开)一张票)。 现在,他的钱箱通过 USB 电缆(抽屉侧面的 USB 类型 B)连接,并且默认使用 COM3 端口。
我尝试过以下代码:
try
{
PosExplorer explorer = new PosExplorer();
DeviceInfo info = explorer.GetDevice("CashDrawer");
CashDrawer drawer = (CashDrawer)explorer.CreateInstance(info);
drawer.Open();
drawer.Claim(1000);
drawer.DeviceEnabled = true;
drawer.OpenDrawer();
drawer.DeviceEnabled = false;
drawer.Release();
drawer.Close();
}
catch(Exception ex) { _ = new MyMessageBox($"ERROR trying to open the Cash Drawer!\n\n{ex.Message}"); }
但不幸的是代码不断抛出异常, 现在,我还尝试安装一些我在此处找到的 USB cashDrawer 驱动程序 https://logiccontrols.com/product-drivers/ ,该驱动程序甚至没有安装,并显示“未找到 cashDrawer”并停止安装。
要使用 C# 打开 USB 连接的钱箱,通常需要通过 USB 接口向钱箱发送特定的命令或信号。具体细节可能会因现金抽屉的制造商和型号而异。
以下是有关如何完成此任务的一般指南:
using System.IO.Ports;
class Program
{
static void Main()
{
// Replace "COMx" with the actual COM port assigned to your cash drawer
string portName = "COM3";
using (SerialPort serialPort = new SerialPort(portName))
{
serialPort.Open();
// Send command to open the cash drawer
serialPort.Write("your_open_command");
serialPort.Close();
}
}
}