Python3阅读USB操纵杆

问题描述:

我对python和python3很绿。我的大部分背景都来自微处理器工作,如arduinos和基本邮票。Python3阅读USB操纵杆

因此,我正在写一些能够从操纵杆控制IP摄像机的东西。计划是使用像Pi这样的代码来运行代码。该程序将读取USB控制杆并将其转换为相机API的URL。我有这么远,但有一个问题。

的代码对Linux操作系统上运行proberly Ubuntu或者raspbian

到目前为止,我还得到了和似乎从我所遇到的与码位我使用的一个障碍分开运作良好阅读操纵杆。

这仅仅是一个代码的其余部分的片段

import sys 

pipe = open('/dev/input/js0', 'rb') #open joystick 
action = [] 
while True: 
    StickValue = readStik(pipe) 
    print ("StickValue") 

    def readStick(pipe): 
     action = [] 
     while stop == 1: 
      for character in pipe.read(1): 
       action += [int(character)] 
       if len(action) == 8: 
        StickValue = action 
        action = [] 
        stop = 2 
        ##when joystick is stationary code hangs here. 
        return StickValue 


#do some more stuff here while waiting for new joystick inputs apposed to hanging 

我明白为什么直到所有8个字节已经停止while循环之前被读出,但我在努力工作如何搞得晕头转向作为其等待我可以绕过它,或者有更好的阅读操纵杆的方法。我一直在寻找pygame,但意味着重新编写其余代码。

由于

尝试通过添加buffering=0open呼叫关掉缓冲。