<Qt USB 设备 变量> 没办法, 这里人才济济, 只想在这里求助了.

2018-06-04 17:00:43 +08:00
 Mmmmc
我插入两个 usb 获得的 handle 假设为 a1 a2

我设断点,但是我的软件只能看到一个 usb 的 handle, 第二个 usb 都看不到,所以说断点也是没用

所以我就用 qDebug()去摸排, 最后的问题就是下面这两段代码, 这个问题困扰我几天了 ...

简述一下:程序由一个定时器开始, 调用 usb 的 api 函数,读取插入的 usb 信息,发送报文,获取报文,得出插入设备的运行状态


void Worker::timerEvent(QTimerEvent *ev)
{
static int cnt = 0, cnt2 = 0, cnt3 = 0;
// 尝试打开
if (handle == NULL&&count<=MAX-1) {//开始的 handle=NULL
if (!cnt3) {
Open();//此处调用 api,获取 usb 的基本信息,handle 也是在这里获取到的
qDebug()<<count<<"count";
cnt = cnt2 = 0;
}
if (cnt3++ > 30) {
cnt3 = 0;
}
}
// 获取采样,handle 已经得到
else {
if(count==MAX)
{
count = 0;
}
Handle.insert(handle);// 将捕捉到到 handle 存入,Handle 是一个 Set 集合,因为是定时器触发, 所以 handle 同一个值被多次传入,所以我用一个 Set 去存储,保证 handle 不重复
QSet<hid_device*>::Iterator i;
qDebug()<<Handle.count()<<"----count";/我 /插入两个 usb,也识别出 两个
for(i=Handle.begin();i!=Handle.end();i++)//我将这两个 handle 循环赋值
{
handle = *i;
readIRTInfo(handle);//从这个函数开始,及进入这个函数, 我的 handle 非常奇怪的只剩 a1 了,a2 消失了???
if (!cnt2) {
unsigned char c1[64] = {outId, 0x55, 0xc1, 0x01,};
if(handle)//此处的 handle 也是 a1
hid_write(handle, c1, 64);
qDebug()<<handle<<"--1--handle";
}
if (!cnt) {
unsigned char cmd[64] = {outId, 0x55, 0x33, 0xff,};
if (m_notouch)
cmd[2] = 0x30;
if(handle)//此处的 handle 也是 a1
hid_write(handle, cmd, 64);
qDebug()<<handle<<"--2--handle";
}
if (cnt2++ > 100) {
cnt2 = 0;
}
if (cnt++ > 10) {
cnt = 0;
}
}
}
}


void Worker::readIRTInfo(hid_device *hdl) //读取报文
{
handle = hdl;
qDebug()<<handle<<"----handle"; //这里可以打印出来 a1, a2
static int ret, expect = 0;
unsigned char buf[64];
while((ret = hid_read(handle, buf, 64)) == 64) {
// qDebug()<<handle<<"++++++++++handle";//进入循环 此处的 handle 只有 a1
if (buf[0] == inId && buf[1] == 0x06 && buf[2] < buf[3] && buf[3] < 140) {
if (expect != buf[2]) {
expect = 0;
continue;
}
expect++;
// 第一包
if (buf[2] == 0x00) {
memcpy(&deviceInfo, buf+4, 22);
UpdateDeviceInfo(&deviceInfo);
memcpy((u8*)AdcSamples, buf+4+22, 60-22);
}
// 其他包
else if (buf[2] < buf[3]){
memcpy((u8*)AdcSamples+60*buf[2]-22, buf+4, 60);
}
// 更新显示
if (buf[2] == buf[3]-1) {
expect = 0;
refresh(handle);//此处的 handle 也是 a1
}
} else if (buf[0] == inId && buf[1] == 0x55 && buf[2] == 0xc1 && buf[3] == 0x01) {
memcpy(&firmwareInfo, buf+4, sizeof(firmwareInfo));
}
}
if (ret < 0) {
hid_close_handle();
}
}
807 次点击
所在节点    问与答
2 条回复
Mmmmc
2018-06-04 17:06:56 +08:00
v2ex 的代码都不能缩进,

https://bbs.csdn.net/topics/392391801 我在这里也贴了,回复请在 v2ex.谢谢
Mmmmc
2018-06-04 20:05:00 +08:00
结贴

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/460286

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX