class dist_us: def __init__(self): self.dus = None # Create IP Connection self.ipcon = IPConnection() # Register IP Connection callbacks self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) # Connect to brickd, will trigger cb_connected self.ipcon.connect(constants.ownIP, PORT) #self.ipcon.enumerate() def cb_distance(self, distance): dicti = {} dicti['value'] = str(distance) dicti['name'] = str(self.dus.get_identity()[0]) + "_" + str(self.dus.get_identity()[5]) mySocket.sendto(str(dicti),(constants.server1,constants.broadPort)) mySocket.sendto(str(dicti),(constants.server1,constants.broadPort)) # Callback handles device connections and configures possibly lost # configuration of lcd and temperature callbacks, backlight etc. def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # Enumeration for Distance US if device_identifier == BrickletDistanceUS.DEVICE_IDENTIFIER: self.dus = BrickletDistanceUS(uid, self.ipcon) self.dus.register_callback(self.dus.CALLBACK_DISTANCE, self.cb_distance) self.dus.set_distance_callback_period(10000) def cb_connected(self, connected_reason): # Enumerate devices again. If we reconnected, the Bricks/Bricklets # may have been offline and the configuration may be lost. # In this case we don't care for the reason of the connection self.ipcon.enumerate()
class dist_us: def __init__(self): self.dus = None # Create IP Connection self.ipcon = IPConnection() # Register IP Connection callbacks self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) # Connect to brickd, will trigger cb_connected self.ipcon.connect(constants.ownIP, PORT) #self.ipcon.enumerate() def cb_distance(self, distance): dicti = {} dicti['value'] = str(distance) dicti['name'] = str(self.dus.get_identity()[0]) + "_" + str( self.dus.get_identity()[5]) mySocket.sendto(str(dicti), (constants.server1, constants.broadPort)) mySocket.sendto(str(dicti), (constants.server1, constants.broadPort)) # Callback handles device connections and configures possibly lost # configuration of lcd and temperature callbacks, backlight etc. def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \ enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # Enumeration for Distance US if device_identifier == BrickletDistanceUS.DEVICE_IDENTIFIER: self.dus = BrickletDistanceUS(uid, self.ipcon) self.dus.register_callback(self.dus.CALLBACK_DISTANCE, self.cb_distance) self.dus.set_distance_callback_period(10000) def cb_connected(self, connected_reason): # Enumerate devices again. If we reconnected, the Bricks/Bricklets # may have been offline and the configuration may be lost. # In this case we don't care for the reason of the connection self.ipcon.enumerate()
HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS # Callback function for distance value callback def cb_distance(distance): print("Distance Value: " + str(distance)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Register distance value callback to function cb_distance dus.register_callback(dus.CALLBACK_DISTANCE, cb_distance) # Set period for distance value callback to 0.2s (200ms) # Note: The distance value callback is only called every 0.2 seconds # if the distance value has changed since the last call! dus.set_distance_callback_period(200) input("Press key to exit\n") # Use raw_input() in Python 2 ipcon.disconnect()
# -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your Distance US Bricklet from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_distance_us import BrickletDistanceUS # Callback function for distance value callback def cb_distance(distance): print("Distance Value: " + str(distance)) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection dus = BrickletDistanceUS(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Register distance value callback to function cb_distance dus.register_callback(dus.CALLBACK_DISTANCE, cb_distance) # Set period for distance value callback to 0.2s (200ms) # Note: The distance value callback is only called every 0.2 seconds # if the distance value has changed since the last call! dus.set_distance_callback_period(200) raw_input("Press key to exit\n") # Use input() in Python 3 ipcon.disconnect()