V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Mmmmc
V2EX  ›  问与答

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

  •  
  •   Mmmmc · 2018-06-04 17:00:43 +08:00 · 798 次点击
    这是一个创建于 2165 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我插入两个 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();
    }
    }
    2 条回复    2018-06-04 20:05:00 +08:00
    Mmmmc
        1
    Mmmmc  
    OP
       2018-06-04 17:06:56 +08:00
    v2ex 的代码都不能缩进,

    https://bbs.csdn.net/topics/392391801 我在这里也贴了,回复请在 v2ex.谢谢
    Mmmmc
        2
    Mmmmc  
    OP
       2018-06-04 20:05:00 +08:00
    结贴
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4460 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:09 · PVG 12:09 · LAX 21:09 · JFK 00:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.