#! /usr/bin/python from labjack import ljm import sys # Open first found LabJack handle = ljm.open(ljm.constants.dtANY, ljm.constants.ctANY, "ANY") info = ljm.getHandleInfo(handle) print("Opened a LabJack with Device type: %i, Connection type: %i,\n" \ "Serial number: %i, IP address: %s, Port: %i,\nMax bytes per MB: %i" % \ (info[0], info[1], info[2], ljm.numberToIP(info[3]), info[4], info[5])) # Configure the I2C communication. ljm.eWriteName(handle, "I2C_SDA_DIONUM", 17) # SDA pin number = 17 (CIO1) ljm.eWriteName(handle, "I2C_SCL_DIONUM", 16) # SCL pin number = 16 (CIO0) ljm.eWriteName(handle, "I2C_SPEED_THROTTLE", 65516) ljm.eWriteName(handle, "I2C_OPTIONS", 0) # Options # ljm.eWriteName(handle, "I2C_SLAVE_ADDRESS", 35) # Slave Address of the I2C chip = 35 (0x23) ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", 1) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", 0) # Set the number of bytes to receive # Set the TX bytes. We are sending 1 byte for the address. num = 127 if len(sys.argv) > 1: num = int(sys.argv[1]) aNames = ["I2C_DATA_TX"] aWrites = [ljm.constants.WRITE] # Indicates we are writing the values. aNumValues = [1] aValues = [num] ljm.eNames(handle, len(aNames), aNames, aWrites, aNumValues, aValues) print("\nWriting User Memory [0] = %s" % " ".join([("%.0f"%val) for val in aValues])) ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. ack = ljm.eReadName(handle, "I2C_ACKS") print("Write acks is " + str(int(ack)) ) # Read the RX bytes. ljm.eWriteName(handle, "I2C_NUM_BYTES_TX", 0) # Set the number of bytes to transmit ljm.eWriteName(handle, "I2C_NUM_BYTES_RX", 1) # Set the number of bytes to receive ljm.eWriteName(handle, "I2C_GO", 1) # Do the I2C communications. aNames = ["I2C_DATA_RX"] aWrites = [ljm.constants.READ] # Indicates we are reading the values. aNumValues = [1] # The number of bytes aValues = [0] aValues = ljm.eNames(handle, len(aNames), aNames, aWrites, aNumValues, aValues) print("\nRead User Memory [0] = %s" % " ".join([("%.0f"%val) for val in aValues])) ack = ljm.eReadName(handle, "I2C_ACKS") print("Read acks is " + str(int(ack)) ) ljm.close(handle)