aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas Schodet2011-03-12 00:45:56 +0100
committerNicolas Schodet2011-03-30 21:02:02 +0200
commitcbd7315e3adc1f8752ec09341e61d15a790e84f5 (patch)
treee066002b26a7cc63d7311826276b6662e35c3433
parent99e96dee78a6e9733182e9cd94a2926e9d89e902 (diff)
add read and write
-rw-r--r--pyfantom.py81
1 files 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