首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >libusb似乎在类编码结构下失败了,但在没有类编码结构的情况下可以工作吗?

libusb似乎在类编码结构下失败了,但在没有类编码结构的情况下可以工作吗?
EN

Stack Overflow用户
提问于 2011-09-12 13:05:45
回答 1查看 278关注 0票数 2

我有一个使用Python/ NXT /libusb的简单工作示例,如下所示(注意:使用具有USB接口的乐高NXT):

代码语言:javascript
运行
复制
import nxt.locator
from nxt.motor import *

def flip_cube(b):
   m_arm = Motor(b, PORT_B)
   m_arm.turn(75, 85)
   m_arm.turn(-50, 85)

b = nxt.locator.find_one_brick()
flip_cube(b)

上面的工作正常。

作为训练练习,我尝试将python代码“对象化”,这样我就可以开始在代码周围放置库,但是现在LibUSB库抱怨说它找不到usb设备。哈?我做错了什么。下面是我尝试使用类结构编写的代码:

代码语言:javascript
运行
复制
import nxt.locator
from nxt.motor import *

class BasicRobotTestCase():
    __test__ = True

    def __init__(self):
        b = nxt.locator.find_one_brick()

    def flip_cube(self):
        m_arm = Motor(b, PORT_B)
        m_arm.turn(75, 85)
        m_arm.turn(-50, 85)

    def test_flip_cube(self):
        flip_cube()

当我执行上面的代码时,我得到了以下错误(即使我重新执行第一个示例,它仍然执行得很好):

代码语言:javascript
运行
复制
E
======================================================================
ERROR: Failure: USBError (No such device (it may have been disconnected))
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg/nose/loader.py", line 485, in makeTest
    return self._makeTest(obj, parent)
  File "/Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg/nose/loader.py", line 539, in _makeTest
    return MethodTestCase(obj)
  File "/Library/Python/2.6/site-packages/nose-0.11.4-py2.6.egg/nose/case.py", line 331, in __init__
    self.inst = self.cls()
  File "/Users/gnunez/git-projects/pdca_automation/rubics/tests/basic_robot_test_case.py", line 8, in __init__
    b = nxt.locator.find_one_brick()
  File "/Users/gnunez/git-projects/pdca_automation/nxt/locator.py", line 112, in find_one_brick
    for s in find_bricks(host, name, silent, method):
  File "/Users/gnunez/git-projects/pdca_automation/nxt/locator.py", line 43, in find_bricks
    for s in socks:
  File "/Users/gnunez/git-projects/pdca_automation/nxt/usbsock.py", line 83, in find_bricks
    for bus in usb.busses():
  File "build/bdist.macosx-10.6-universal/egg/usb/legacy.py", line 330, in busses
    return (Bus(),)
  File "build/bdist.macosx-10.6-universal/egg/usb/legacy.py", line 326, in __init__
    self.devices = [Device(d) for d in core.find(find_all=True)]
  File "build/bdist.macosx-10.6-universal/egg/usb/legacy.py", line 311, in __init__
    self.configurations = [Configuration(c) for c in dev]
  File "build/bdist.macosx-10.6-universal/egg/usb/core.py", line 706, in __iter__
    yield Configuration(self, i)
  File "build/bdist.macosx-10.6-universal/egg/usb/core.py", line 407, in __init__
    configuration
  File "build/bdist.macosx-10.6-universal/egg/usb/_debug.py", line 52, in do_trace
    return f(*args, **named_args)
  File "build/bdist.macosx-10.6-universal/egg/usb/backend/libusb10.py", line 423, in get_configuration_descriptor
    config, byref(cfg)))
  File "build/bdist.macosx-10.6-universal/egg/usb/backend/libusb10.py", line 357, in _check
    raise USBError(_str_error[retval.value])
USBError: No such device (it may have been disconnected)
EN

回答 1

Stack Overflow用户

发布于 2011-09-12 15:51:51

创建BasicRobotTestCase时,如果不将其存储为实例(即self.b)的成员,则会丢失b变量

编辑:好吧,self的缺失并不是问题的根源,也许是鼻子实例化你的类的方式变成了U盘抓取,试着直接实例化这个类。

代码语言:javascript
运行
复制
import nxt.locator
from nxt.motor import *

class BasicRobotTestCase():
    __test__ = True

    def __init__(self):
        self.b = nxt.locator.find_one_brick()   # Store the brick in self.b

    def flip_cube(self):
        m_arm = Motor(self.b, PORT_B)   # use the stored brick in self.b to create the motor
        m_arm.turn(75, 85)
        m_arm.turn(-50, 85)

    def test_flip_cube(self):
        self.flip_cube()

if __name__=="__main__":
    BasicRobotTestCase().test_flip_cube()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7383508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档