/usr/share/pyshared/nxt/usbsock.py is in python-nxt 2.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | # nxt.usbsock module -- USB socket communication with LEGO Minstorms NXT
# Copyright (C) 2006, 2007 Douglas P Lau
# Copyright (C) 2009 Marcus Wanner
# Copyright (C) 2011 Paul Hollensen, Marcus Wanner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import usb, os
from nxt.brick import Brick
ID_VENDOR_LEGO = 0x0694
ID_PRODUCT_NXT = 0x0002
class USBSock(object):
'Object for USB connection to NXT'
bsize = 60 # USB socket block size
type = 'usb'
def __init__(self, device):
self.device = device
self.handle = None
self.debug = False
def __str__(self):
return 'USB (%s)' % (self.device.filename)
def connect(self):
'Use to connect to NXT.'
if self.debug:
print 'Connecting via USB...'
config = self.device.configurations[0]
iface = config.interfaces[0][0]
self.blk_out, self.blk_in = iface.endpoints
self.handle = self.device.open()
self.handle.setConfiguration(1)
self.handle.claimInterface(0)
if os.name != 'nt': # http://code.google.com/p/nxt-python/issues/detail?id=33
self.handle.reset()
if self.debug:
print 'Connected.'
return Brick(self)
def close(self):
'Use to close the connection.'
if self.debug:
print 'Closing USB connection...'
self.device = None
self.handle = None
self.blk_out = None
self.blk_in = None
if self.debug:
print 'USB connection closed.'
def send(self, data):
'Use to send raw data over USB connection ***ADVANCED USERS ONLY***'
if self.debug:
print 'Send:',
print ':'.join('%02x' % ord(c) for c in data)
self.handle.bulkWrite(self.blk_out.address, data)
def recv(self):
'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***'
data = self.handle.bulkRead(self.blk_in.address, 64)
if self.debug:
print 'Recv:',
print ':'.join('%02x' % (c & 0xFF) for c in data)
# NOTE: bulkRead returns a tuple of ints ... make it sane
return ''.join(chr(d & 0xFF) for d in data)
def find_bricks(host=None, name=None):
'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***'
# FIXME: probably should check host (MAC)
# if anyone knows how to do this, please file a bug report
for bus in usb.busses():
for device in bus.devices:
if device.idVendor == ID_VENDOR_LEGO and device.idProduct == ID_PRODUCT_NXT:
yield USBSock(device)
|