使用hidapi进行usb通信提示写入失败

三只猴子 2017-10-23 02:42:23
我在qt下用hidapi的库进行usb通信,就是数据写入和读取的操作,编译通过了,但是文件写入函数hid_write总是失败,返回-1,求解答
...全文
2584 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Apple Joke 2018-09-13
  • 打赏
  • 举报
回复

我遇到了hid_write返回-1,用hid_error返回“不正确的函数”的问题,请问博主遇到这样的问题吗?
AI4teru 2018-03-06
  • 打赏
  • 举报
回复
引用 8 楼 u014729550 的回复:
知道问题在哪了,hid_write函数的参数必须用hextostring转成ascii码才能顺利执行
hextostring是什么意思? buf[0] = 0x0; // 0x2 返回-1 buf[1] = 0x41; buf[2] = 0x42; res = hid_write(handle, buf, sizeof(buf)); //返回 -1 buf[0] = 0x1; buf[1] = 0x41; buf[2] = 0x42; res = hid_write(handle, buf, sizeof(buf)); //返回 4 请问楼主,第一个字节buf[0]填充什么?填充实际数据长度吗?
三只猴子 2017-10-24
  • 打赏
  • 举报
回复
知道问题在哪了,hid_write函数的参数必须用hextostring转成ascii码才能顺利执行
Guang-wen 2021-07-01
  • 举报
回复
@三只猴子 楼主我跟你一样;定义了unsigned char buf[]={0x00,0x41,0x42};也需要转吗?
赵4老师 2017-10-23
  • 打赏
  • 举报
回复
使用depends.exe在行和不行的电脑上分别查看对比exe依赖的dll及其版本号有什么不同之处。
三只猴子 2017-10-23
  • 打赏
  • 举报
回复
试过了,还是不行,我觉得应该不是这方面的问题吧
赵4老师 2017-10-23
  • 打赏
  • 举报
回复
关闭UAC、防火墙、杀毒软件、…… 以管理员身份运行 ……
三只猴子 2017-10-23
  • 打赏
  • 举报
回复
我搜过了啊,找了很久了,没有找到类似的错误,我看别人发的程序和我的一样,但是可以通过,我的就是不行
赵4老师 2017-10-23
  • 打赏
  • 举报
回复
百度搜相关关键字。
三只猴子 2017-10-23
  • 打赏
  • 举报
回复
这是写入函数的内容: int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length) { DWORD bytes_written; BOOL res; OVERLAPPED ol; unsigned char *buf; memset(&ol, 0, sizeof(ol)); if (length >= dev->output_report_length) { /* The user passed the right number of bytes. Use the buffer as-is. */ buf = (unsigned char *) data; } else { /* Create a temporary buffer and copy the user's data into it, padding the rest with zeros. */ buf = (unsigned char *) malloc(dev->output_report_length); memcpy(buf, data, length); memset(buf + length, 0, dev->output_report_length - length); length = dev->output_report_length; } res = WriteFile(dev->device_handle, buf, length, NULL, &ol); if (!res) { if (GetLastError() != ERROR_IO_PENDING) { /* WriteFile() failed. Return error. */ register_error(dev, "WriteFile"); bytes_written = -1; goto end_of_function;//程序执行到这里以后就返回-1了 } } /* Wait here until the write is done. This makes hid_write() synchronous. */ res = GetOverlappedResult(dev->device_handle, &ol, &bytes_written, TRUE/*wait*/); if (!res) { /* The Write operation failed. */ register_error(dev, "WriteFile"); bytes_written = -1; printf("operation failure\n"); goto end_of_function; } end_of_function: if (buf != data) free(buf); return bytes_written; }
三只猴子 2017-10-23
  • 打赏
  • 举报
回复
#include "hidapi.h" #define MAX_STR 255 int main(int argc, char* argv[]) { int res; unsigned char buf[65]; wchar_t wstr[MAX_STR]; hid_device *handle; int i; // Open the device using the VID, PID, // and optionally the Serial number. handle = hid_open(0x4d8, 0x3f, NULL); // Toggle LED (cmd 0x80). The first byte is the report number (0x0). buf[0] = 0x0; buf[1] = 0x80; res = hid_write(handle, buf, 65); // Request state (cmd 0x81). The first byte is the report number (0x0). buf[0] = 0x0; buf[1] = 0x81; res = hid_write(handle, buf, 65); // Read requested state hid_read(handle, buf, 65); // Print out the returned buffer. for (i = 0; i < 4; i++) printf("buf[%d]: %d\n", i, buf[i]); return 0; }

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧