Qt 关于全局变量的问题

南山以南 2018-06-04 04:52:20
我插入两个usb 获得的handle 假设为 a1 a2

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

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



void Worker::timerEvent(QTimerEvent *ev)
{
static int cnt = 0, cnt2 = 0, cnt3 = 0;
// 尝试打开
if (handle == NULL&&count<=MAX-1) {
if (!cnt3) {
Open();
qDebug()<<count<<"count";
cnt = cnt2 = 0;
}
if (cnt3++ > 30) {
cnt3 = 0;
}
}
// 获取采样
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();
}
}
...全文
467 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
南山以南 2018-06-04
  • 打赏
  • 举报
回复
if (cnt2++ > 100) { cnt2 = 0; } if (cnt++ > 10) { cnt = 0; } 放在遍历外面. 结贴

16,233

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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