summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTat-Chee Wan (USM)2011-05-05 12:02:54 +0800
committerTat-Chee Wan (USM)2011-05-05 12:02:54 +0800
commit849bb04045a9c458420e210414b5a4d211cde76d (patch)
tree791914d385a4c2971ef41e454b7c23884daacd13
parent667197864959c669690dedd4de835328a5560201 (diff)
Added del method to make sure that nxt object is cleaned up
-rw-r--r--nxt-python-fantom/nxt/fantomglue.py20
-rw-r--r--nxt-python-fantom/nxt/usbsock.py35
2 files changed, 44 insertions, 11 deletions
diff --git a/nxt-python-fantom/nxt/fantomglue.py b/nxt-python-fantom/nxt/fantomglue.py
index 359427d..a977696 100644
--- a/nxt-python-fantom/nxt/fantomglue.py
+++ b/nxt-python-fantom/nxt/fantomglue.py
@@ -64,7 +64,13 @@ class BluetoothSocket:
def close(self):
if self._sock is not None:
del self._sock
-
+ self._sock = None
+
+ def __del__(self):
+ """Destroy interface."""
+ if self._sock is not None:
+ del self._sock
+
class BluetoothError(IOError):
pass
@@ -127,10 +133,22 @@ class USBSocket:
def recv(self, numbytes):
return self._sock.read( numbytes )
+
+ #def poll_command(self, numbytes):
+ # I'm not sure if this is specific to Direct Command Processing
+ # Or if it refers to any data transmission
+ #print "Bytes Available:", self._sock.bytes_available()
+ #return self._sock.read_buffer( numbytes, 0 )
def close(self):
if self._sock is not None:
del self._sock
+ self._sock = None
+
+ def __del__(self):
+ """Destroy interface."""
+ if self._sock is not None:
+ del self._sock
if __name__ == '__main__':
#get_info = False
diff --git a/nxt-python-fantom/nxt/usbsock.py b/nxt-python-fantom/nxt/usbsock.py
index 70af774..f4dc3f1 100644
--- a/nxt-python-fantom/nxt/usbsock.py
+++ b/nxt-python-fantom/nxt/usbsock.py
@@ -45,6 +45,7 @@ class USBSock(object):
if self.debug:
print 'Closing USB connection...'
self.sock.close()
+ #self.sock = None
if self.debug:
print 'USB connection closed.'
@@ -55,14 +56,21 @@ class USBSock(object):
print ':'.join('%02x' % ord(c) for c in data)
self.sock.send(data)
- def recv(self):
+ def recv(self, num_bytes=USB_BUFSIZE):
'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***'
- data = self.sock.recv(USB_BUFSIZE) # FIXME: This will cause an exception since we cannot read more than the actual buffer contents
+ data = self.sock.recv(num_bytes)
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)
+ print ':'.join('%02x' % ord(c) for c in data)
+ return data
+
+ def __del__(self):
+ """Destroy interface."""
+ if self.sock is not None:
+ del self.sock
+ if self.debug:
+ print 'Deleted USBSocket.'
+
def _check_brick(arg, value):
return arg is None or arg == value
@@ -72,6 +80,7 @@ def find_bricks(host=None, name=None):
'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***'
for d in usb.find_devices(lookup_names=True):
if get_info:
+ # pyfantom specific debug info
print " firmware version:", d.get_firmware_version()
print " get device info:", d.get_device_info()
rs = d.get_resource_string()
@@ -83,6 +92,9 @@ if __name__ == '__main__':
write_read = True
socks = find_bricks()
for s in socks:
+ #llsocks = usb.find_devices()
+ #for ls in llsocks:
+ #s = USBSock(ls)
print s.sock
brick = s.connect()
if write_read:
@@ -92,9 +104,9 @@ if __name__ == '__main__':
# SYS_CMD: 0x01
# VERSION: 0x88
cmd = struct.pack('2B', 0x01, 0x88)
- #brick.sock.send(cmd)
+ brick.sock.send(cmd)
#s.send(cmd)
- s.sock.send(cmd)
+ #s.sock.send(cmd)
print "wrote", len(cmd)
# Response:
# REPLY_CMD: 0x02
@@ -104,9 +116,9 @@ if __name__ == '__main__':
# PROTOCOL_VERSION major
# FIRMWARE_VERSION minor
# FIRMWARE_VERSION major
- #rep = brick.sock.recv()
- #rep = s.recv()
- rep = s.sock.recv(7)
+ rep = brick.sock.recv(7)
+ #rep = s.recv(7)
+ #rep = s.sock.recv(7)
print "read", struct.unpack('%dB' % len(rep), rep)
# Same thing, without response.
#cmd = struct.pack('2B', 0x81, 0x88)
@@ -114,5 +126,8 @@ if __name__ == '__main__':
#print "wrote", cmd
#rep = brick.sock.recv()
#print "read", struct.unpack('%dB' % len(rep), rep)
+ #s.close()
+ #del s
+ brick.sock.close()
del brick