#! /usr/bin/env mpscript # # This script tests USB access by reading the current position from a # USB motor controller from Phidgets, Inc. The script assumes that the # Phidget controller has already had its firmware uploaded. Running the # test script 'mphidget' found in the 'test' directory of the main MX # package is sufficient to do that. However, if the MX test script # 'mphidget' is still running when you invoke the current script, the # find_device_by_order() method will fail, since the device is already # in use by the 'mphidget' script. # # Since this script accesses USB devices that are not handled by 'hotplug', # it is necessary to run this script as 'root' on Linux. # import struct def main( record_list, argv ): libusb = record_list.get_record( 'libusb' ) phidget = libusb.find_device_by_order( 0x6c2, 0x46, 0, 1, 0, 0, 0 ) status_string = phidget.bulk_read( 0x82, 16, 1.0 ) unpack_format = "%iB" % len( status_string ) status_tuple = struct.unpack( unpack_format, status_string ) m1_position = status_tuple[0] + ( status_tuple[1] << 8 ) \ + ( status_tuple[2] << 16 ) + ( status_tuple[3] << 24 ) m2_position = status_tuple[4] + ( status_tuple[5] << 8 ) \ + ( status_tuple[6] << 16 ) + ( status_tuple[7] << 24 ) print "m1 = %i, m2 = %i" % ( m1_position, m2_position )