与HID设备的通信在读/写时挂起(AS3992 RFID阅读器)

时间:2014-05-31 13:58:12

标签: c# .net usb hid

我正在尝试与基于AS3992芯片的UHF RFID阅读器进行通信。 这个设备被Windows检测为标准HID,它可以与第三方应用程序一起使用(我发现LinkSprite的一些UHF RFID阅读器GUI可以工作,但它看起来像是一些旧的C ++应用程序。)

所以我试图将这个设备支持集成到我的.NET应用程序中。经过一些研究后我尝试了HidLibrary,但是当我试图给这个设备写东西时(这个样本中的初始序列),它挂起了“写”。

有人知道我做错了吗?

谢谢!

我的操作系统是Win 8.1 x64。

以下是示例应用程序:

using HidLibrary; namespace HidTest2 { class Program { static void Main(string[] args) { var devices = HidDevices.Enumerate(0x1325); var rfid = devices.First(); rfid.OpenDevice(); rfid.Write(new byte[] { 0x31, 0x03, 0x01 }); // Application hangs here while (true) // I can't get here { Thread.Sleep(50); var result = rfid.Read(); Console.Write(result.Data); } } } }

PS:我也试过HidSharp,但结果相同。检测到HID设备,但我无法写入。

PSS:这是设备:Link to ebay

3 个答案:

答案 0 :(得分:0)

我找不到你提到的AS3229芯片的数据表,所以我猜这里......

该设备可能是USB键盘,因此您通常只能为其写入LED状态位(大写锁定,Num lock,Shift)。这是你想写的吗?

尝试删除写入并等待扫描的RFID字符串进入。

编辑:看起来这个设备是通过USB呈现为串行设备......我在这里找到了一个与之匹配的描述: https://s3.amazonaws.com/linksprite/cuttonwood/datasheet.pdf

如果它与您正在测试的设备相同,那么我会尝试通过COM端口API与之通信,而不是使用您一直使用的相对较低级别的HID API。

答案 1 :(得分:0)

因为我一时间收到一封电子邮件,如果我解决了这个问题,请回答:

我必须通过固件 替换用于HID通信的原始固件进行串行通信(搜索" as399x uart 115200 hex"或" as399x uart 9600 hex"在互联网上)然后它就像一个沙姆。当然你需要适当的C8051Fxxx编程器(大约20美元来自中国),USB串行转换器并熟悉一些焊接(你必须在板上焊接引脚用于JTAG和串口)。

答案 2 :(得分:0)

如上所述,该设备实际上可能不是Hid设备。您是否尝试通过USB设备而不是Hid设备枚举?这是枚举USB或Hid设备的代码。代码是enter image description here

对于Hid设备,请使用ClassGuid:4D1E55B2-F16F-11CF-88CB-001111000030

,对于Win USB设备,请使用:dee824ef-729b-4a0e-9c14-b7117d33a817

here

public async Task<IEnumerable<DeviceDefinition>> GetConnectedDeviceDefinitions(uint? vendorId, uint? productId)
    {
        return await Task.Run<IEnumerable<DeviceDefinition>>(() =>
        {
            var deviceDefinitions = new Collection<DeviceDefinition>();
            var spDeviceInterfaceData = new SpDeviceInterfaceData();
            var spDeviceInfoData = new SpDeviceInfoData();
            var spDeviceInterfaceDetailData = new SpDeviceInterfaceDetailData();
            spDeviceInterfaceData.CbSize = (uint)Marshal.SizeOf(spDeviceInterfaceData);
            spDeviceInfoData.CbSize = (uint)Marshal.SizeOf(spDeviceInfoData);

            var guidString = ClassGuid.ToString();
            var copyOfClassGuid = new Guid(guidString);

            var i = APICalls.SetupDiGetClassDevs(ref copyOfClassGuid, IntPtr.Zero, IntPtr.Zero, APICalls.DigcfDeviceinterface | APICalls.DigcfPresent);

            if (IntPtr.Size == 8)
            {
                spDeviceInterfaceDetailData.CbSize = 8;
            }
            else
            {
                spDeviceInterfaceDetailData.CbSize = 4 + Marshal.SystemDefaultCharSize;
            }

            var x = -1;

            var productIdHex = GetHex(productId);
            var vendorHex = GetHex(vendorId);

            while (true)
            {
                x++;

                var isSuccess = APICalls.SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref copyOfClassGuid, (uint)x, ref spDeviceInterfaceData);
                if (!isSuccess)
                {
                    var errorCode = Marshal.GetLastWin32Error();
                    if (errorCode == APICalls.ERROR_NO_MORE_ITEMS)
                    {
                        break;
                    }

                    throw new Exception($"Could not enumerate devices. Error code: {errorCode}");
                }

                isSuccess = APICalls.SetupDiGetDeviceInterfaceDetail(i, ref spDeviceInterfaceData, ref spDeviceInterfaceDetailData, 256, out _, ref spDeviceInfoData);
                WindowsDeviceBase.HandleError(isSuccess, "Could not get device interface detail");

                //Note this is a bit nasty but we can filter Vid and Pid this way I think...
                if (vendorId.HasValue && !spDeviceInterfaceDetailData.DevicePath.ToLower().Contains(vendorHex)) continue;
                if (productId.HasValue && !spDeviceInterfaceDetailData.DevicePath.ToLower().Contains(productIdHex)) continue;

                deviceDefinitions.Add(GetDeviceDefinition(spDeviceInterfaceDetailData.DevicePath));
            }

            APICalls.SetupDiDestroyDeviceInfoList(i);

            return deviceDefinitions;
        });
}
相关问题