From 849bb04045a9c458420e210414b5a4d211cde76d Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Thu, 5 May 2011 12:02:54 +0800 Subject: Added del method to make sure that nxt object is cleaned up --- nxt-python-fantom/nxt/fantomglue.py | 20 +++++++++++++++++++- nxt-python-fantom/nxt/usbsock.py | 35 +++++++++++++++++++++++++---------- 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 -- cgit v1.2.3 From b8c8045ffdff1e0d5337398563ed6b86d3661040 Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Thu, 5 May 2011 14:19:14 +0800 Subject: modified recv() to accept a buffer length argument in usbsock.py --- nxt-python-fantom/nxt/brick.py | 1 + nxt-python-fantom/nxt/fantomglue.py | 6 ++++++ nxt-python-fantom/nxt/usbsock.py | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/nxt-python-fantom/nxt/brick.py b/nxt-python-fantom/nxt/brick.py index 740ccbd..240ba27 100644 --- a/nxt-python-fantom/nxt/brick.py +++ b/nxt-python-fantom/nxt/brick.py @@ -217,6 +217,7 @@ class Brick(object): #TODO: this begs to have explicit methods sleep(duration / 1000.0) def __del__(self): + print "Deleting Brick" self.sock.close() find_files = FileFinder diff --git a/nxt-python-fantom/nxt/fantomglue.py b/nxt-python-fantom/nxt/fantomglue.py index a977696..16d9608 100644 --- a/nxt-python-fantom/nxt/fantomglue.py +++ b/nxt-python-fantom/nxt/fantomglue.py @@ -70,6 +70,9 @@ class BluetoothSocket: """Destroy interface.""" if self._sock is not None: del self._sock + print "NXT object deleted" + else: + print "No NXT Object when calling __del__ for BluetoothSocket" class BluetoothError(IOError): pass @@ -149,6 +152,9 @@ class USBSocket: """Destroy interface.""" if self._sock is not None: del self._sock + print "NXT object deleted" + else: + print "No NXT Object when calling __del__ for USBSocket" if __name__ == '__main__': #get_info = False diff --git a/nxt-python-fantom/nxt/usbsock.py b/nxt-python-fantom/nxt/usbsock.py index f4dc3f1..5ff2b36 100644 --- a/nxt-python-fantom/nxt/usbsock.py +++ b/nxt-python-fantom/nxt/usbsock.py @@ -128,6 +128,6 @@ if __name__ == '__main__': #print "read", struct.unpack('%dB' % len(rep), rep) #s.close() #del s - brick.sock.close() + #brick.sock.close() del brick -- cgit v1.2.3