请教USB HID通讯问题!

在学习板上可以正常收发数据 , 但转到开发板上发送的数据可以正常发送,但是开发板接收不到, 开发板和调试助手可以正常调试的,不知道有没有大神遇到这种问题?

    buffer = new byte[outputReportLength];

                     buffer[0] = r.reportID;

                     int maxBufferLength = 0;

                     if (r.reportBuff.Length < outputReportLength - 1)

                         maxBufferLength = r.reportBuff.Length;

                     else

                         maxBufferLength = outputReportLength - 1;

                     for (int i = 1; i < maxBufferLength; i++)

                         buffer[i] = r.reportBuff[i - 1];

                    hidDevice.Write(buffer, 0, OutputReportLength);

扫描二维码关注公众号,回复: 10654670 查看本文章

对比一下给学习板和开发板发送的buffer和OutputReportLength是否一样

学习板的outputreportLength是2 然后我这边就发送两个字节, lrc歌词下位机可以正常收发。
开发板的ou'tputrepo'rtLength获取的是65,但其实是64,我就发送65个字节会报错, 发送64个字节没报错,但是下位机收取不到

1、为什么学习板发过来的是两个字节,而开发板发过来的是65个字节,协议不一样?
2、给开发板发送65个字节是开发板报错吧?报错是报的什么错,是没有按照协议回复?

估计是你没有按照通讯协议收发信息

学习板上定的长度就是2个字节
开发版上的定的长度是64个字节, 但上位机收到outputreportlength是65,发65字节报"参数错误", 发64个字节可以正常发,但是开发板收不到数据
我今天让他们换个开发板试试
你有c# hid方面的成功使用的代码吗, 我可以和你的代码参考下

for (int i = 0; i < maxBufferLength; i++)
                         buffer[i + 1] = r.reportBuff[i];

这个buffer我现在就直接手动赋值了

顶起  没人玩USB HID通讯吗?

这个创建通信类的的代码

   public HID_RETURN OpenDevice(UInt16 vID, UInt16 pID)

        {

            // IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

            if (deviceOpened == false)

            {

                //获取连接的HID列表

                List<string> deviceList = new List<string>();

                GetHidDeviceList(ref deviceList);

                if (deviceList.Count == 0)

                    return HID_RETURN.NO_DEVICE_CONECTED;

                for (int i = 0; i < deviceList.Count; i++)

                {

                    device = CreateFile(deviceList[i], DESIREDACCESS.GENERIC_READ | DESIREDACCESS.GENERIC_WRITE, 0, 0, CREATIONDISPOSITION.OPEN_EXISTING, FLAGSANDATTRIBUTES.FILE_FLAG_OVERLAPPED, 0);

                    if (!device.IsInvalid)

                    // strDevicePath = GetDevicePath(hInfoSet, ref oInterface);

                        HIDD_ATTRIBUTES attributes;

                        //IntPtr serialBuff = Marshal.AllocHGlobal(512);

                        HidD_GetAttributes(device, out attributes);

                        //HidD_GetSerialNumberString(device, serialBuff, 512);

                        //string deviceStr = Marshal.PtrToStringAuto(serialBuff);

                        //Marshal.FreeHGlobal(serialBuff);

                        if (attributes.VendorID == vID && attributes.ProductID == pID)   // && deviceStr == serial

                        {

                            IntPtr preparseData;

                            HIDP_CAPS caps;

                            HidD_GetPreparsedData(device, out preparseData);

                            HidP_GetCaps(preparseData, out caps);

                            HidD_FreePreparsedData(preparseData);

                            outputReportLength = caps.OutputReportByteLength;

                            inputReportLength = caps.InputReportByteLength ;

                            hidDevice = new FileStream(device, FileAccess.ReadWrite, inputReportLength, true);

                            deviceOpened = true;

                            //BeginAsyncRead();

                            Guid gHid = HIDGuid;

                            IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);

                            DeviceInterfaceData oInterface = new DeviceInterfaceData();

                            strDevicePath = GetDevicePath(hInfoSet, ref oInterface);

                            bool canRead = hidDevice.CanRead;       //获取一个值,该值指示当前流是否支持读取。

                            bool canWrite = hidDevice.CanWrite;     //获取一个值,该值指示当前流是否支持写入。

                            return HID_RETURN.SUCCESS;

                        }

                    }

                }

                return HID_RETURN.DEVICE_NOT_FIND;

            }

            else

                return HID_RETURN.DEVICE_OPENED;

        }

顶起,usbhid,只能发64个字节,下位机返回时第一个字节有特殊用途具体干啥的忘记了,实际有效数据为63个字节

大错特错,发65字节=1字节report id + 64 data

发布了122 篇原创文章 · 获赞 2 · 访问量 5216

猜你喜欢

转载自blog.csdn.net/liuji0517/article/details/105217044