From 99c3816f4ae37ed3f99fa6fd2da67f752fd55cfa Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Wed, 4 May 2011 08:46:36 +0800 Subject: added test case for fantomglue using pyfantom test case --- nxt-python-fantom/nxt/fantomglue.py | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/nxt-python-fantom/nxt/fantomglue.py b/nxt-python-fantom/nxt/fantomglue.py index 96bcd3c..5df42c6 100644 --- a/nxt-python-fantom/nxt/fantomglue.py +++ b/nxt-python-fantom/nxt/fantomglue.py @@ -118,3 +118,52 @@ class USBSocket: def close(self): if self._sock is not None: del self._sock + +if __name__ == '__main__': + #get_info = False + get_info = True + write_read = True + for i in pyfantom.NXTIterator(False): + # Enable Bluetooth Interface + #for i in pyfantom.NXTIterator(True): + if get_info: + print "name:", i.get_name() + print "resource string:", i.get_resource_string() + print "get_nxt:" + nxt = i.get_nxt() + print " firmware version:", nxt.get_firmware_version() + print " get device info:", nxt.get_device_info() + rs = nxt.get_resource_string() + print " resource string:", rs + del nxt + print "NXT():" + nxt = pyfantom.NXT(rs) + print " resource string:", nxt.get_resource_string() + del nxt + if write_read: + nxt = i.get_nxt() + import struct + # Write VERSION SYS_CMD. + # Query: + # SYS_CMD: 0x01 + # VERSION: 0x88 + cmd = struct.pack('2B', 0x01, 0x88) + r = nxt.write(cmd) + print "wrote", r + # Response: + # REPLY_CMD: 0x02 + # VERSION: 0x88 + # SUCCESS: 0x00 + # PROTOCOL_VERSION minor + # PROTOCOL_VERSION major + # FIRMWARE_VERSION minor + # FIRMWARE_VERSION major + rep = nxt.read(7) + print "read", struct.unpack('%dB' % len(rep), rep) + # Same thing, without response. + cmd = struct.pack('2B', 0x81, 0x88) + r = nxt.write(cmd) + print "wrote", r + rep = nxt.read(7) + print "read", struct.unpack('%dB' % len(rep), rep) + del nxt -- cgit v1.2.3 From e88dfbc47fc0a05d34368755a35ef27b4c11d89d Mon Sep 17 00:00:00 2001 From: Tat-Chee Wan (USM) Date: Wed, 4 May 2011 12:11:46 +0800 Subject: work in progress Attempting to debug pyfantom issues. Works in fantomglue, but not in usbsock --- nxt-python-fantom/nxt/fantomglue.py | 44 ++++++++++++++++++------------------- nxt-python-fantom/nxt/usbsock.py | 44 ++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/nxt-python-fantom/nxt/fantomglue.py b/nxt-python-fantom/nxt/fantomglue.py index 5df42c6..1fd8792 100644 --- a/nxt-python-fantom/nxt/fantomglue.py +++ b/nxt-python-fantom/nxt/fantomglue.py @@ -68,8 +68,9 @@ def _check_brick(arg, value): def find_devices(lookup_names=False): # parameter is ignored devicelist = [] for d in pyfantom.NXTIterator(False): - addr = d.get_resource_string() - print "NXT addr: ", addr + #name = d.get_name() + #addr = d.get_resource_string() + #print "NXT addr: ", addr nxt = d.get_nxt() # BUG?: If nxt.get_firware_version() is enabled, d.get_nxt() will throw an exception # Related to Reference Counting for Obj-C Objects? @@ -101,13 +102,18 @@ class USBSocket: self.debug = False def device_name(self): - devinfo = self._sock.deviceinfo() + devinfo = self._sock.get_device_info() return devinfo.name def connect(self): if self._sock is None: # Port is ignored + if self.debug: + print "No NXT object assigned" self._sock = pyfantom.NXT(addr) # FIXME: No address! + else: + if self.debug: + print "Using existing NXT object" def send(self, data): return self._sock.write( data ) @@ -123,25 +129,19 @@ if __name__ == '__main__': #get_info = False get_info = True write_read = True - for i in pyfantom.NXTIterator(False): + #for i in pyfantom.NXTIterator(False): # Enable Bluetooth Interface #for i in pyfantom.NXTIterator(True): + #devices = find_devices() + #for i in devices: + for i in find_devices(): if get_info: - print "name:", i.get_name() - print "resource string:", i.get_resource_string() - print "get_nxt:" - nxt = i.get_nxt() - print " firmware version:", nxt.get_firmware_version() - print " get device info:", nxt.get_device_info() - rs = nxt.get_resource_string() + print " firmware version:", i.get_firmware_version() + print " get device info:", i.get_device_info() + rs = i.get_resource_string() print " resource string:", rs - del nxt - print "NXT():" - nxt = pyfantom.NXT(rs) - print " resource string:", nxt.get_resource_string() - del nxt if write_read: - nxt = i.get_nxt() + nxt = i import struct # Write VERSION SYS_CMD. # Query: @@ -161,9 +161,9 @@ if __name__ == '__main__': rep = nxt.read(7) print "read", struct.unpack('%dB' % len(rep), rep) # Same thing, without response. - cmd = struct.pack('2B', 0x81, 0x88) - r = nxt.write(cmd) - print "wrote", r - rep = nxt.read(7) - print "read", struct.unpack('%dB' % len(rep), rep) + #cmd = struct.pack('2B', 0x81, 0x88) + #r = nxt.write(cmd) + #print "wrote", r + #rep = nxt.read(7) + #print "read", struct.unpack('%dB' % len(rep), rep) del nxt diff --git a/nxt-python-fantom/nxt/usbsock.py b/nxt-python-fantom/nxt/usbsock.py index aa766a8..1573bf8 100644 --- a/nxt-python-fantom/nxt/usbsock.py +++ b/nxt-python-fantom/nxt/usbsock.py @@ -28,7 +28,7 @@ class USBSock(object): def __init__(self, device): self.sock = usb.USBSocket(device) - self.debug = False + self.debug = True def __str__(self): return 'USB (%s)' % (self.sock.device_name()) @@ -68,7 +68,49 @@ def _check_brick(arg, value): return arg is None or arg == value def find_bricks(host=None, name=None): + get_info = False 'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***' for d in usb.find_devices(lookup_names=True): + if get_info: + print " firmware version:", d.get_firmware_version() + print " get device info:", d.get_device_info() + rs = d.get_resource_string() + print " resource string:", rs # FIXME: probably should check host and name yield USBSock(d) + +if __name__ == '__main__': + write_read = True + socks = find_bricks() + for s in socks: + brick = s.connect() + if write_read: + import struct + # Write VERSION SYS_CMD. + # Query: + # SYS_CMD: 0x01 + # VERSION: 0x88 + cmd = struct.pack('2B', 0x01, 0x88) + #brick.sock.send(cmd) + s.send(cmd) + print "wrote", len(cmd) + # Response: + # REPLY_CMD: 0x02 + # VERSION: 0x88 + # SUCCESS: 0x00 + # PROTOCOL_VERSION minor + # PROTOCOL_VERSION major + # FIRMWARE_VERSION minor + # FIRMWARE_VERSION major + #rep = brick.sock.recv() + rep = s.recv() + print "read", struct.unpack('%dB' % len(rep), rep) + # Same thing, without response. + #cmd = struct.pack('2B', 0x81, 0x88) + #brick.sock.send(cmd) + #print "wrote", cmd + #rep = brick.sock.recv() + #print "read", struct.unpack('%dB' % len(rep), rep) + del brick + + \ No newline at end of file -- cgit v1.2.3 From 667197864959c669690dedd4de835328a5560201 Mon Sep 17 00:00:00 2001 From: TC Wan Date: Wed, 4 May 2011 15:19:07 +0800 Subject: more testing code Testing code for testing pyfantom read() and write(). read() must be provided with number of bytes to read argument which does not exceed the actual buffer contents size. Otherwise an Exception would be thrown. --- nxt-python-fantom/nxt/fantomglue.py | 34 +++++++++++++++++++++------------- nxt-python-fantom/nxt/usbsock.py | 10 ++++++---- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/nxt-python-fantom/nxt/fantomglue.py b/nxt-python-fantom/nxt/fantomglue.py index 1fd8792..359427d 100644 --- a/nxt-python-fantom/nxt/fantomglue.py +++ b/nxt-python-fantom/nxt/fantomglue.py @@ -42,6 +42,13 @@ class BluetoothSocket: self._sock = _sock self._proto = proto + def __str__(self): + return 'FantomGlue BT (%s)' % self.device_name() + + def device_name(self): + devinfo = self._sock.get_device_info() + return devinfo.name + def connect(self, addrport): addr, port = addrport if self._sock is None: @@ -72,9 +79,6 @@ def find_devices(lookup_names=False): # parameter is ignored #addr = d.get_resource_string() #print "NXT addr: ", addr nxt = d.get_nxt() - # BUG?: If nxt.get_firware_version() is enabled, d.get_nxt() will throw an exception - # Related to Reference Counting for Obj-C Objects? - #print " firmware version:", nxt.get_firmware_version() devicelist.append(nxt) return devicelist @@ -101,6 +105,9 @@ class USBSocket: self._sock = device self.debug = False + def __str__(self): + return 'FantomGlue USB (%s)' % self.device_name() + def device_name(self): devinfo = self._sock.get_device_info() return devinfo.name @@ -129,11 +136,6 @@ if __name__ == '__main__': #get_info = False get_info = True write_read = True - #for i in pyfantom.NXTIterator(False): - # Enable Bluetooth Interface - #for i in pyfantom.NXTIterator(True): - #devices = find_devices() - #for i in devices: for i in find_devices(): if get_info: print " firmware version:", i.get_firmware_version() @@ -141,14 +143,14 @@ if __name__ == '__main__': rs = i.get_resource_string() print " resource string:", rs if write_read: - nxt = i + nxt = USBSocket(i) import struct # Write VERSION SYS_CMD. # Query: # SYS_CMD: 0x01 # VERSION: 0x88 cmd = struct.pack('2B', 0x01, 0x88) - r = nxt.write(cmd) + r = nxt.send(cmd) print "wrote", r # Response: # REPLY_CMD: 0x02 @@ -158,12 +160,18 @@ if __name__ == '__main__': # PROTOCOL_VERSION major # FIRMWARE_VERSION minor # FIRMWARE_VERSION major - rep = nxt.read(7) + #rep = nxt.recv(USB_BUFSIZE) # Doesn't work if it exceeds buffer content length (exception) + #rep = nxt.recv(7) # Works, since reply is 7 bytes + rep = nxt.recv(5) # Works, read length < remaining buffer content length + print "read", struct.unpack('%dB' % len(rep), rep) + rep = nxt.recv(2) # Works, read length <= remaining buffer content length print "read", struct.unpack('%dB' % len(rep), rep) + #rep = nxt.recv(1) # Doesn't work if it exceeds buffer content length (exception) # Same thing, without response. #cmd = struct.pack('2B', 0x81, 0x88) - #r = nxt.write(cmd) + #r = nxt.send(cmd) #print "wrote", r - #rep = nxt.read(7) + #rep = nxt.recv(USB_BUFSIZE) + #rep = nxt.recv(7) #print "read", struct.unpack('%dB' % len(rep), rep) del nxt diff --git a/nxt-python-fantom/nxt/usbsock.py b/nxt-python-fantom/nxt/usbsock.py index 1573bf8..70af774 100644 --- a/nxt-python-fantom/nxt/usbsock.py +++ b/nxt-python-fantom/nxt/usbsock.py @@ -57,7 +57,7 @@ class USBSock(object): def recv(self): 'Use to recieve raw data over USB connection ***ADVANCED USERS ONLY***' - data = self.sock.recv(USB_BUFSIZE) + data = self.sock.recv(USB_BUFSIZE) # FIXME: This will cause an exception since we cannot read more than the actual buffer contents if self.debug: print 'Recv:', print ':'.join('%02x' % (c & 0xFF) for c in data) @@ -83,6 +83,7 @@ if __name__ == '__main__': write_read = True socks = find_bricks() for s in socks: + print s.sock brick = s.connect() if write_read: import struct @@ -92,7 +93,8 @@ if __name__ == '__main__': # VERSION: 0x88 cmd = struct.pack('2B', 0x01, 0x88) #brick.sock.send(cmd) - s.send(cmd) + #s.send(cmd) + s.sock.send(cmd) print "wrote", len(cmd) # Response: # REPLY_CMD: 0x02 @@ -103,7 +105,8 @@ if __name__ == '__main__': # FIRMWARE_VERSION minor # FIRMWARE_VERSION major #rep = brick.sock.recv() - rep = s.recv() + #rep = s.recv() + rep = s.sock.recv(7) print "read", struct.unpack('%dB' % len(rep), rep) # Same thing, without response. #cmd = struct.pack('2B', 0x81, 0x88) @@ -113,4 +116,3 @@ if __name__ == '__main__': #print "read", struct.unpack('%dB' % len(rep), rep) del brick - \ No newline at end of file -- cgit v1.2.3