所以我最近想玩一下 MSR605X,发现很难控制硬件的任何方面。我创建了一个示例代码来尝试关闭设备上的 LED,但没有成功。
在此代码(示例代码,受我的 WPF 应用程序中的代码启发)中,我创建了一个无限响应读取缓冲区,并通过验证产品 ID 是否为 3 (MSR) 来连接到 MSR。我还验证了它是 MSR,因为拔掉电源后无法找到它,而且设备描述上写着“MSR”(磁条阅读器)。
我已经尝试了一切,包括查看文档、Python 中的随机 MSR605X 项目(此处)以及其他各种尝试谷歌搜索我能找到的任何内容。还是不行。
这是示例代码(没有错误,只是成功,但实际上不起作用)
using HidSharp;
HidDevice[] GetHidDevices()
{
var deviceList = DeviceList.Local;
return deviceList.GetHidDevices().ToArray();
}
HidDevice[] devices = GetHidDevices();
HidDevice? device = null;
foreach (HidDevice iDevice in devices)
{
if(iDevice.ProductID==3) device = iDevice; // Product ID 3 is a hacky method of verifying that its a MSR.
}
if (device == null)
{
Console.WriteLine("No device");
Environment.Exit(1);
}
Console.WriteLine($"Device found on path {device.DevicePath}.");
// Found device, connect
HidStream stream = device.Open();
stream.ReadTimeout = Timeout.Infinite;
while (stream.CanWrite == false)
{
Thread.Sleep(100);
}
Console.WriteLine("Opened stream.");
void writebytes(byte[] d)
{
try
{
stream.Write(d,0,d.Length);
stream.FlushAsync().Wait();
Console.WriteLine("Bytes sent succesfully: " + d.Length); // No errors were ever displayed writing
}
catch (Exception ex)
{
Console.WriteLine($"Error sending bytes: {ex.Message}");
}
}
writebytes(new byte[] { 0x1B, 0x61 }); // Reset Command (Does not work)
writebytes(new byte[] { 0x1B, 0x81 }); // Turn all LEDs off (Does not work)
// Read the response
new Thread(() =>
{
while (true)
{
byte[] response = new byte[64]; // adjust the buffer size as needed
int bytesRead = stream.Read(response, 0, response.Length);
stream.FlushAsync().Wait();
Console.WriteLine($"Response: {BitConverter.ToString(response, 0, bytesRead)}");
}
}).Start();
任何帮助都会有很大帮助,谢谢。
等了一天后,我决定自己编写API,因为目前似乎没有可用的API来完成此任务。您可以在这里找到 API: https://github.com/Cass-dev-web/CSHARPMSR605X 请注意,这是一个非常实验性的项目,确实不建议任何人使用,而只是作为参考。