hidapi通过vid,pid打开设备,输出指令,读取信息
//开线程使用hidapi
AfxBeginThread(&USBHidTestThread, (LPVOID)this, THREAD_PRIORITY_NORMAL, 0);
根据实际需求写的方法USBHidTestThread:
UINT USBHidTestThread(LPVOID pParam) {
Sleep(200);
int Unplug = 0;//拔插状态
USBTestDlg* pthis = (USBTestDlg*)pParam;
int res = 0;
unsigned char buf[64] = {};
wchar_t wstr[MAX_STR] = {};
hid_device* handle;
CString tempStr = _T("");
int i;
char chstr[128] = {};
struct hid_device_info* devs, * cur_dev;
//有问题 从界面输入指令 而且指令要是0x01 界面输入的是01 这个切割的方式可能有问题
string instruct1, instruct2, instruct3;
instruct1 = (CStringA)Ins;
instruct2 = (CStringA)Ins1;
instruct3 = (CStringA)Ins2;
debuglogpr(L"DebugUSB.log", _T("instruct string =") + Ins + Ins1 + Ins2);
if (hid_init()) {
return 0;//??
}
//将CSTRING转换为unsigned short
//string strv,strp;
//strv = (CStringA)Hid;//CString 转换为 string
unsigned short vids, pids;
string vv = (CStringA)Hid;
const char* cv = vv.c_str();
vids = strtol(cv, NULL, 16);
string pp = (CStringA)Pid;
const char* cp = pp.c_str();
pids = strtol(cp, NULL, 16);
devs = hid_enumerate(vids, pids);
cur_dev = devs;
vector<string> vpath;//装遍历出的每个地址
while (cur_dev) {
vpath.push_back(cur_dev->path);
string str = cur_dev->path;//char* 转换为 string 可以直接赋值
CA2T szr(str.c_str());
tempStr = (LPCTSTR)szr;
//tempStr.Format(_T("%s"), sss);//string 转换为 CString
debuglogpr(L"DebugUSB.log", _T("--HID PATH:") + tempStr);
//将unsigned short 转换为 string
unsigned short us = cur_dev->product_id;
char puf[20];
itoa(us, puf, 16);
string query = puf;
CA2T szc(query.c_str());
tempStr = (LPCTSTR)szc;
debuglogpr(L"DebugUSB.log", _T("--PID:") + tempStr);
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
::memset(buf, 0x00, sizeof(buf));//报错:E0266 "memset" 不明确 加::表明调用全局函数
CString log;//输出log用的
for (int d = 0; d < vpath.size(); d++) {
handle = hid_open_path(vpath.at(d).c_str());
string path;
if (!handle)
{
path = "this device path: ";
path += vpath.at(d).c_str();
CA2T szc(path.c_str());
log = (LPCTSTR)szc;
debuglogpr(L"DebugUSB.log", _T("Path:") + log);
debuglogpr(L"DebugUSB.log", L"--------------------unable to open device ");
continue;
}
debuglogpr(L"DebugUSB.log", L"open device Success !");
// Read the Manufacturer String
wstr[0] = 0x0000;
res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
if (res < 0)
{
debuglogpr(L"DebugUSB.log", L"Unable to read manufacturer string ***");
}
else
{
//QString test = QString::fromWCharArray(wstr);
//tempStr = "manufacturer string =" + test.toStdString();
debuglogpr(L"DebugUSB.log", L"read manufacturer string OK***");
/*writeLog(tempStr);*/
}
hid_set_nonblocking(handle, 1);
debuglogpr(L"DebugUSB.log", L"write ins:");
unsigned short write1, write2, write3;
string sw1 = (CStringA)Ins;
const char* cw1 = sw1.c_str();
write1 = strtol(cw1, NULL, 16);
string sw2 = (CStringA)Ins1;
const char* cw2 = sw2.c_str();
write2 = strtol(cw2, NULL, 16);
string sw3 = (CStringA)Ins2;
const char* cw3 = sw3.c_str();
write3 = strtol(cw3, NULL, 16);
int p = 0;//p是typeCport口的标识,而不是配置界面portd的标识 从1开始
if (write3 == 0x01) {
p = 1;
}
else if (write3 == 0x02) {
p = 2;
}
else if (write3 == 0x03) {
p = 3;
}
else if (write3 == 0x04) {
p = 4;
}
else if (write3 == 0x05) {
p = 5;
}
else if (write3 == 0x06) {
p = 6;
}
::memset(buf, 0x00, sizeof(buf));
buf[0] = 0xFF & write1;
buf[1] = 0xFF & write2;
buf[2] = 0xFF & write3;
int write = 0;//write 是否写入数据成功的标记
res = hid_write(handle, buf, 64);
sprintf_s(chstr, "write info: %02x %02x %02x ...", buf[0], buf[1], buf[2]);
CA2T szc(std::string(chstr).c_str());
log = (LPCTSTR)szc;
debuglogpr(L"DebugUSB.log", log);
if (res < 0)
{
path = "this device path: ";
path += vpath.at(d).c_str();
CA2T szc(path.c_str());
log = (LPCTSTR)szc;
debuglogpr(L"DebugUSB.log", log);
debuglogpr(L"DebugUSB.log", L"------------------------Unable to write()");
continue;
}
else {
path = "this device path: ";
path += vpath.at(d).c_str();
CA2T szc(path.c_str());
log = (LPCTSTR)szc;
debuglogpr(L"DebugUSB.log", log);
debuglogpr(L"DebugUSB.log", L"write Success------------------------");
write = 1;//能够写入成功说明打开设备地址正确
}
res = 0;
std::string result = "";
int num = 0;
CString port;//USBHidTestThread port标识 只针对TypeC
debuglogpr(L"DebugUSB.log", L"read start:");
while (1) {
Sleep(100);
if (aa && bb && !Unplug) {//当前port A/B面均check时 并且USB拔出 跳到下一个接口
port.Format(L"port:%d ,check A+B OK!", p);
&n

本文介绍了一个使用hidapi库进行USB设备测试的应用案例。通过hidapi库,程序能够实现USB设备的枚举、指令发送及信息读取等功能。特别针对Type-C接口的USB设备进行了详细的测试流程说明。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



