pyusb-单击按钮时如何读取数据流

时间:2019-01-31 09:36:32

标签: python qml pyqt5 pyusb

我有一个触摸屏和一个力传感器通过相同的USB流(不同的接口)连接到我的计算机。我正在使用pyUSB与提供的数据进行接口(我对设备第3个接口的IN​​端点感兴趣)。

到目前为止,我已经能够将数据读取到数组中并连续(通过while True循环打印力值(数据[3])。

现在,我希望仅在单击GUI应用程序(由PyQt5编程)中的按钮时读取此值。

我已经设法在单击按钮时读取了一个力值,但是这个值与实际情况并不相符:当我在单击按钮时在屏幕上施加〜30的力时,读取的值为〜1。当我第二次在按钮上施加不同的力(无关紧要)时,读取的值为〜30。

我试图每次单击一个按钮时读取一堆数据(20),结果略有不同,即使都不满意: 当我单击按钮时施加力时,显示的20个值将根据所施加的力而变化。但是在下一次单击按钮时,即使没有施加力,第一个值也将与上一组数据的最后一个值相同,而其他19个值是一致的。以下是读取的两束数据的摘要:第一个施加力,第二个不施加力:

2 bunches of data

这是我正在使用的代码:

main.py

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtCore import QObject, pyqtSignal

import usb.core
import usb.util

if __name__ == "__main__":
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load('main.qml')
    win = engine.rootObjects()[0]
    win.show()    

    #---------------connect to device
    dev = usb.core.find(idVendor=0x03eb, idProduct=0x2f04)
    if dev is None:
        print("Error")
        raise ValueError('Device not found')
    else:
        print("No error")

    dev.reset()


    #When you connect the device to Linux, Linux automatically sets some drivers to it. In order for this script to work, we must first detach the driver
    if dev.is_kernel_driver_active(0) == True:
        dev.detach_kernel_driver(0)
        print("true")
    else:
        print("false")

    endpoint3 = dev[0][(2,0)][0]

    interface = 2
    if dev.is_kernel_driver_active(interface) is True:
        #tell the kernel to detach
        dev.detach_kernel_driver(interface)
        #claim the device

    def twos_comp(val, bits):
        if (val & (1 << (bits - 1))) != 0:
            val = val - (1 << bits)
        return val

    def on_button_pressed():
        for x in range(0, 20):
            try:
                data = dev.read(endpoint3.bEndpointAddress, endpoint3.wMaxPacketSize)
            except usb.core.USBError as e:
                data = None
                print("error")
            except KeyboardInterrupt:
                print ("Bye")
                sys.exit()

            print(twos_comp(data[3] + (data[4] << 8), 16), ", ", data[1])
            #data[3] + (data[4] << 8) is the force value
            data = []
        print("\n")

    button = win.findChild(QObject, "myButton")
    button.messageRequired.connect(on_button_pressed)

    #---------------prepare to close...
    ret = app.exec_()

    usb.util.release_interface(dev, interface)
    dev.attach_kernel_driver(interface)

    print("end")

    sys.exit(ret)

main.qml

import QtQuick 2.9
import QtQuick.Window 2.11
import QtGraphicalEffects 1.0
import QtQuick.Controls 2.4

Window {
    visible: true
    width: 1100
    height: 650
    title: qsTr("Tutorial 1")

    Button {
        signal messageRequired
        signal messageClick
        signal messageRelease
        autoRepeat: true
        objectName: "myButton"
        anchors.top: parent.top
        width: parent.width
        height: parent.height / 2 - 100
        anchors.margins: 20
        text: "Read force"
        onPressed: messageRequired()
    }
}

我正在Linux和Windows 10上工作,而Windows中的问题更严重。

我想念什么?我一直在阅读有关端点,USB和pyUSB的文章,并介绍了每篇SO文章,试图理解正在发生的事情,但没有成功... 我的想法是,某处有一个缓冲区(可能由操作系统监视),该缓冲区未按时刷新或在适当的时间未读取。但是我还没有在网上找到任何东西……

PS:我直接在C ++中使用libusb进行了尝试,但是结果是相同的。我认为这是一个尚未刷新的缓冲区问题,但不知道哪个缓冲区,如何访问它以及如何刷新它...

0 个答案:

没有答案
相关问题