From cbd7315e3adc1f8752ec09341e61d15a790e84f5 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 12 Mar 2011 00:45:56 +0100 Subject: add read and write --- pyfantom.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/pyfantom.py b/pyfantom.py index 788ece8..7d1f378 100644 --- a/pyfantom.py +++ b/pyfantom.py @@ -38,6 +38,12 @@ dll.nFANTOM100_iNXT_getFirmwareVersion.argtypes = [c_uint, POINTER(c_ubyte), dll.nFANTOM100_iNXT_getFirmwareVersion.argtypes = None dll.nFANTOM100_iNXT_getDeviceInfo.argtypes = [c_uint, c_char_p, POINTER(c_ubyte), POINTER(c_ubyte), POINTER(c_uint), POINTER(c_int)] +dll.nFANTOM100_iNXT_write.argtypes = [c_uint, c_char_p, c_uint, + POINTER(c_int)] +dll.nFANTOM100_iNXT_write.restype = c_uint +dll.nFANTOM100_iNXT_read.argtypes = [c_uint, c_char_p, c_uint, + POINTER(c_int)] +dll.nFANTOM100_iNXT_read.restype = c_uint dll.nFANTOM100_iNXT_getDeviceInfo.restype = None dll.nFANTOM100_iNXT_getResourceString.argtypes = [c_uint, c_char_p, POINTER(c_int)] @@ -254,6 +260,25 @@ class NXT: return (protocol_major.value, protocol_minor.value, firmware_major.value, firmware_minor.value) + def write(self, data): + """Write, in a generic fashion, to this NXT.""" + status = c_int(0) + data_buffer = ctypes.create_string_buffer(data) + ret = dll.nFANTOM100_iNXT_write(self.handle, data_buffer, len(data), + byref(status)) + Status.check(status) + return ret + + def read(self, length): + """Read, in a generic fashion, from this NXT.""" + status = c_int(0) + data_buffer = ctypes.create_string_buffer(length) + ret = dll.nFANTOM100_iNXT_read(self.handle, data_buffer, length, + byref(status)) + Status.check(status) + assert ret <= length + return data_buffer.raw[0:ret] + def get_device_info(self): """Get basic information about this NXT.""" status = c_int(0) @@ -288,17 +313,47 @@ class NXT: dll.nFANTOM100_destroyNXT(self.handle, byref(status)) if __name__ == '__main__': + get_info = False + write_read = True for i in NXTIterator(False): - 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 = NXT(rs) - print " resource string:", nxt.get_resource_string() - del nxt + 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 = 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