This is the description of the Python API bindings for the RED Brick. General information and technical specifications for the RED Brick are summarized in its hardware description.
An installation guide for the Python API bindings is part of their general description.
The example code below is Public Domain (CC0 1.0).
Download (example_start_program.py)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
HOST = 'localhost'
PORT = 4223
UID = 'XXYYZZ' # Change XXYYZZ to the UID of your RED Brick
PROGRAM = 'test' # Change to your program identifier
from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_red import BrickRED
def check_error(error_code, *args):
if error_code != 0:
print('RED Brick error occurred: {0}'.format(error_code))
exit(1)
if len(args) == 1:
return args[0]
return args
def start_program(red, identifier):
# Create session and get program list
session_id = check_error(*red.create_session(10))
program_list_id = check_error(*red.get_programs(session_id))
# Iterate program list to find the one to start
started = False
for i in range(check_error(*red.get_list_length(program_list_id))):
program_id, _ = check_error(*red.get_list_item(program_list_id, i, session_id))
# Get program identifier string
string_id = check_error(*red.get_program_identifier(program_id, session_id))
string_length = check_error(*red.get_string_length(string_id))
string_data = ''
while len(string_data) < string_length:
string_data += check_error(*red.get_string_chunk(string_id, len(string_data)))
check_error(red.release_object(string_id, session_id))
# Check if this is the program to be started
if string_data.decode('utf-8') == identifier:
check_error(red.start_program(program_id))
started = True
check_error(red.release_object(program_id, session_id))
if started:
break
check_error(red.release_object(program_list_id, session_id))
check_error(red.expire_session(session_id))
return started
if __name__ == '__main__':
ipcon = IPConnection() # Create IP connection
red = BrickRED(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
if start_program(red, PROGRAM):
print('Started RED Brick program: {0}'.format(PROGRAM))
else:
print('RED Brick program not found: {0}'.format(PROGRAM))
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
|
Download (example_read_file.py)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
HOST = 'localhost'
PORT = 4223
UID = 'XXYYZZ' # Change XXYYZZ to the UID of your RED Brick
REMOTE_PATH = '/home/tf/foobar.txt' # Change to your remote path
LOCAL_PATH = 'foobar.txt' # Change to your local path
import sys
from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_red import BrickRED
def check_error(error_code, *args):
if error_code != 0:
raise Exception('RED Brick error occurred: {0}'.format(error_code))
if len(args) == 1:
return args[0]
return args
def allocate_string(red, string, session_id):
string_id = check_error(*red.allocate_string(len(string), string[:58], session_id))
for offset in range(58, len(string), 58):
check_error(red.set_string_chunk(string_id, offset, string[offset:offset + 58]))
return string_id
def read_file(red, remote_path, local_path):
# Create session
session_id = check_error(*red.create_session(60))
# Create remote non-executable file for writing as user/group tf
remote_path_id = allocate_string(red, remote_path, session_id)
remote_file_id = check_error(*red.open_file(remote_path_id,
BrickRED.FILE_FLAG_READ_ONLY |
BrickRED.FILE_FLAG_NON_BLOCKING,
0, 0, 0, session_id))
check_error(red.release_object(remote_path_id, session_id))
# Open local file for writing
local_file = open(local_path, 'wb')
# Read remote file and write to local file
transferred = 0
while True:
data, length_read = check_error(*red.read_file(remote_file_id, 61))
data = data[:length_read]
if len(data) == 0:
break
if sys.version_info[0] > 2:
local_file.write(bytes(data))
else:
local_file.write(''.join(map(chr, data)))
check_error(red.keep_session_alive(session_id, 30))
transferred += length_read
# Close local file
local_file.close()
# Close remote file
check_error(red.release_object(remote_file_id, session_id))
# Expire session
check_error(red.expire_session(session_id))
print('{0} bytes transferred'.format(transferred))
if __name__ == '__main__':
ipcon = IPConnection() # Create IP connection
red = BrickRED(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
read_file(red, REMOTE_PATH, LOCAL_PATH)
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
|
Download (example_write_file.py)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
HOST = 'localhost'
PORT = 4223
UID = 'XXYYZZ' # Change XXYYZZ to the UID of your RED Brick
LOCAL_PATH = 'foobar.txt' # Change to your local path
REMOTE_PATH = '/home/tf/foobar.txt' # Change to your remote path
import sys
from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_red import BrickRED
def check_error(error_code, *args):
if error_code != 0:
raise Exception('RED Brick error occurred: {0}'.format(error_code))
if len(args) == 1:
return args[0]
return args
def allocate_string(red, string, session_id):
string_id = check_error(*red.allocate_string(len(string), string[:58], session_id))
for offset in range(58, len(string), 58):
check_error(red.set_string_chunk(string_id, offset, string[offset:offset + 58]))
return string_id
def write_file(red, local_path, remote_path):
# Open local file for reading
local_file = open(local_path, 'rb')
# Create session
session_id = check_error(*red.create_session(60))
# Create remote non-executable file for writing as user/group tf
remote_path_id = allocate_string(red, remote_path, session_id)
remote_file_id = check_error(*red.open_file(remote_path_id,
BrickRED.FILE_FLAG_WRITE_ONLY |
BrickRED.FILE_FLAG_CREATE |
BrickRED.FILE_FLAG_TRUNCATE |
BrickRED.FILE_FLAG_NON_BLOCKING,
0o644, 1000, 1000, session_id))
check_error(red.release_object(remote_path_id, session_id))
# Read local file and write to remote file
transferred = 0
while True:
data = list(local_file.read(61))
if sys.version_info[0] < 3:
data = map(ord, data)
if len(data) == 0:
break
length_to_write = len(data)
data += [0] * (61 - length_to_write)
length_written = check_error(*red.write_file(remote_file_id, data, length_to_write))
if length_written != length_to_write:
print('Short write')
exit(1)
check_error(red.keep_session_alive(session_id, 30))
transferred += length_written
# Close remote file
check_error(red.release_object(remote_file_id, session_id))
# Close local file
local_file.close()
# Expire session
check_error(red.expire_session(session_id))
print('{0} bytes transferred'.format(transferred))
if __name__ == '__main__':
ipcon = IPConnection() # Create IP connection
red = BrickRED(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
write_file(red, LOCAL_PATH, REMOTE_PATH)
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
|
Download (example_list_directory.py)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
HOST = 'localhost'
PORT = 4223
UID = 'XXYYZZ' # Change XXYYZZ to the UID of your RED Brick
DIRECTORY_PATH = '/home/tf' # Change to your directory path
import sys
from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_red import BrickRED
DIRECTORY_ENTRY_TYPE = {
BrickRED.DIRECTORY_ENTRY_TYPE_UNKNOWN: 'unknown',
BrickRED.DIRECTORY_ENTRY_TYPE_REGULAR: 'regular',
BrickRED.DIRECTORY_ENTRY_TYPE_DIRECTORY: 'directory',
BrickRED.DIRECTORY_ENTRY_TYPE_CHARACTER: 'character',
BrickRED.DIRECTORY_ENTRY_TYPE_BLOCK: 'block',
BrickRED.DIRECTORY_ENTRY_TYPE_FIFO: 'fifo',
BrickRED.DIRECTORY_ENTRY_TYPE_SYMLINK: 'symlink',
BrickRED.DIRECTORY_ENTRY_TYPE_SOCKET: 'socket'
}
def check_error(error_code, *args):
if error_code != 0:
raise Exception('RED Brick error occurred: {0}'.format(error_code))
if len(args) == 1:
return args[0]
return args
def allocate_string(red, string, session_id):
string_id = check_error(*red.allocate_string(len(string), string[:58], session_id))
for offset in range(58, len(string), 58):
check_error(red.set_string_chunk(string_id, offset, string[offset:offset + 58]))
return string_id
def get_string(red, string_id):
length = check_error(*red.get_string_length(string_id))
string = []
while len(string) < length:
chunk = check_error(*red.get_string_chunk(string_id, len(string)))
string += map(ord, chunk)
if sys.version_info[0] > 2:
string = bytes(string)
else:
string = ''.join(map(chr, string))
return string.decode('utf-8')
def list_directory(red, directory_path):
# Create session
session_id = check_error(*red.create_session(60))
# Open directory
directory_path_id = allocate_string(red, directory_path, session_id)
directory_id = check_error(*red.open_directory(directory_path_id, session_id))
check_error(red.release_object(directory_path_id, session_id))
# Get directory entries
while True:
result = red.get_next_directory_entry(directory_id, session_id)
if result.error_code == BrickRED.ERROR_CODE_NO_MORE_DATA:
break
entry_name_id, entry_type = check_error(*result)
entry_name = get_string(red, entry_name_id)
check_error(red.release_object(entry_name_id, session_id))
print('name: {0}, type: {1}'.format(entry_name, DIRECTORY_ENTRY_TYPE[entry_type]))
# Close directory
check_error(red.release_object(directory_id, session_id))
# Expire session
check_error(red.expire_session(session_id))
if __name__ == '__main__':
ipcon = IPConnection() # Create IP connection
red = BrickRED(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
list_directory(red, DIRECTORY_PATH)
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
|
Download (example_remove_file.py)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
HOST = 'localhost'
PORT = 4223
UID = 'XXYYZZ' # Change XXYYZZ to the UID of your RED Brick
REMOTE_PATH = '/home/tf/foobar.txt' # Change to your remote path
import sys
import time
from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_red import BrickRED
def check_error(error_code, *args):
if error_code != 0:
raise Exception('RED Brick error occurred: {0}'.format(error_code))
if len(args) == 1:
return args[0]
return args
def allocate_string(red, string, session_id):
string_id = check_error(*red.allocate_string(len(string), string[:58], session_id))
for offset in range(58, len(string), 58):
check_error(red.set_string_chunk(string_id, offset, string[offset:offset + 58]))
return string_id
def remove_file(red, remote_path):
# Create session
session_id = check_error(*red.create_session(60))
# Prepare spawn-process call
executable_id = allocate_string(red, '/bin/rm', session_id)
remote_path_id = allocate_string(red, remote_path, session_id)
arguments_id = check_error(*red.allocate_list(1, session_id))
check_error(red.append_to_list(arguments_id, remote_path_id))
check_error(red.release_object(remote_path_id, session_id))
environment_id = check_error(*red.allocate_list(0, session_id))
working_directory_id = allocate_string(red, '/', session_id)
dev_zero_id = allocate_string(red, '/dev/zero', session_id)
stdin_id = check_error(*red.open_file(dev_zero_id,
BrickRED.FILE_FLAG_READ_ONLY |
BrickRED.FILE_FLAG_NON_BLOCKING,
0, 1000, 1000, session_id))
check_error(red.release_object(dev_zero_id, session_id))
dev_null_id = allocate_string(red, '/dev/null', session_id)
stdout_id = check_error(*red.open_file(dev_null_id,
BrickRED.FILE_FLAG_WRITE_ONLY |
BrickRED.FILE_FLAG_NON_BLOCKING,
0, 1000, 1000, session_id))
check_error(red.release_object(dev_null_id, session_id))
# Spawn rm process to remove remote file
process_id = check_error(*red.spawn_process(executable_id,
arguments_id,
environment_id,
working_directory_id,
1000, 1000,
stdin_id,
stdout_id,
stdout_id,
session_id))
check_error(red.release_object(executable_id, session_id))
check_error(red.release_object(arguments_id, session_id))
check_error(red.release_object(environment_id, session_id))
check_error(red.release_object(working_directory_id, session_id))
check_error(red.release_object(stdin_id, session_id))
check_error(red.release_object(stdout_id, session_id))
# Busy wait for rm process to finish
# FIXME: Could use CALLBACK_PROCESS_STATE_CHANGED instead
state, timestamp, exit_code = check_error(*red.get_process_state(process_id))
while state in [BrickRED.PROCESS_STATE_UNKNOWN, BrickRED.PROCESS_STATE_RUNNING]:
time.sleep(0.1)
state, timestamp, exit_code = check_error(*red.get_process_state(process_id))
check_error(red.keep_session_alive(session_id, 10))
check_error(red.release_object(process_id, session_id))
# Expire session
check_error(red.expire_session(session_id))
# Report result
if state == BrickRED.PROCESS_STATE_ERROR:
print('Removing {0} failed with an internal error'.format(remote_path))
elif state == BrickRED.PROCESS_STATE_EXITED:
if exit_code == 0:
print('Removed {0}'.format(remote_path))
else:
# FIXME: Could report stdout/stderr from /bin/rm here
print('Removing {0} failed with /bin/rm exit code {1}'.format(remote_path, exit_code))
elif state == BrickRED.PROCESS_STATE_KILLED:
print('Removing {0} failed with /bin/rm being killed by signal {1}'.format(remote_path, exit_code))
elif state == BrickRED.PROCESS_STATE_STOPPED:
print('Removing {0} failed with /bin/rm being stopped')
else:
print('Removing {0} failed with an unknown error'.format(remote_path))
if __name__ == '__main__':
ipcon = IPConnection() # Create IP connection
red = BrickRED(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
remove_file(red, REMOTE_PATH)
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
|
Download (example_spawn_process.py)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
HOST = 'localhost'
PORT = 4223
UID = 'XXYYZZ' # Change XXYYZZ to the UID of your RED Brick
import sys
import time
from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_red import BrickRED
def check_error(error_code, *args):
if error_code != 0:
raise Exception('RED Brick error occurred: {0}'.format(error_code))
if len(args) == 1:
return args[0]
return args
def allocate_string(red, string, session_id):
string_id = check_error(*red.allocate_string(len(string), string[:58], session_id))
for offset in range(58, len(string), 58):
check_error(red.set_string_chunk(string_id, offset, string[offset:offset + 58]))
return string_id
def spawn_process(red, executable, arguments):
# Create session
session_id = check_error(*red.create_session(60))
# Prepare spawn-process call
executable_id = allocate_string(red, executable, session_id)
arguments_id = check_error(*red.allocate_list(1, session_id))
for argument in arguments:
argument_id = allocate_string(red, argument, session_id)
check_error(red.append_to_list(arguments_id, argument_id))
check_error(red.release_object(argument_id, session_id))
environment_id = check_error(*red.allocate_list(0, session_id))
working_directory_id = allocate_string(red, '/', session_id)
dev_zero_id = allocate_string(red, '/dev/zero', session_id)
stdin_id = check_error(*red.open_file(dev_zero_id,
BrickRED.FILE_FLAG_READ_ONLY |
BrickRED.FILE_FLAG_NON_BLOCKING,
0, 1000, 1000, session_id))
check_error(red.release_object(dev_zero_id, session_id))
dev_null_id = allocate_string(red, '/dev/null', session_id)
stdout_id = check_error(*red.open_file(dev_null_id,
BrickRED.FILE_FLAG_WRITE_ONLY |
BrickRED.FILE_FLAG_NON_BLOCKING,
0, 1000, 1000, session_id))
check_error(red.release_object(dev_null_id, session_id))
# Spawn rm process to remove remote file
process_id = check_error(*red.spawn_process(executable_id,
arguments_id,
environment_id,
working_directory_id,
1000, 1000,
stdin_id,
stdout_id,
stdout_id,
session_id))
check_error(red.release_object(executable_id, session_id))
check_error(red.release_object(arguments_id, session_id))
check_error(red.release_object(environment_id, session_id))
check_error(red.release_object(working_directory_id, session_id))
check_error(red.release_object(stdin_id, session_id))
check_error(red.release_object(stdout_id, session_id))
# Busy wait for rm process to finish
# FIXME: Could use CALLBACK_PROCESS_STATE_CHANGED instead
state, timestamp, exit_code = check_error(*red.get_process_state(process_id))
while state in [BrickRED.PROCESS_STATE_UNKNOWN, BrickRED.PROCESS_STATE_RUNNING]:
time.sleep(0.1)
state, timestamp, exit_code = check_error(*red.get_process_state(process_id))
check_error(red.keep_session_alive(session_id, 10))
check_error(red.release_object(process_id, session_id))
# Expire session
check_error(red.expire_session(session_id))
# Report result
if state == BrickRED.PROCESS_STATE_ERROR:
print('Executing {0} failed with an internal error'.format(executable))
elif state == BrickRED.PROCESS_STATE_EXITED:
if exit_code == 0:
print('Executed {0}'.format(executable))
else:
# FIXME: Could report stdout/stderr from executable here
print('Executing {0} failed with exit code {1}'.format(executable, exit_code))
elif state == BrickRED.PROCESS_STATE_KILLED:
print('Executing {0} was killed by signal {1}'.format(executable, exit_code))
elif state == BrickRED.PROCESS_STATE_STOPPED:
print('Executing {0} was stopped'.format(executable))
else:
print('Executing {0} failed with an unknown error'.format(executable))
if __name__ == '__main__':
ipcon = IPConnection() # Create IP connection
red = BrickRED(UID, ipcon) # Create device object
ipcon.connect(HOST, PORT) # Connect to brickd
# Don't use device before ipcon is connected
spawn_process(red, 'touch', ['/tmp/foobar'])
input("Press key to exit\n") # Use raw_input() in Python 2
ipcon.disconnect()
|
Generally, every function of the Python bindings can throw an
tinkerforge.ip_connection.Error
exception that has a value
and a
description
property. value
can have different values:
All functions listed below are thread-safe.
Note
The API documentation for the RED Brick is currently incomplete.
The RED Brick API is meant to be used by the Brick Viewer to implement the offered functionality (getting status information, managing programs etc.). Normal users will not need to use this API, it may only be interesting for power users.
FIXME: explain sessions
The RED Brick API operates on reference counted objects (strings, lists, files,
directories, processes and programs) that are identified by their 16bit object
ID. Functions that allocate or return an object ID (e.g. allocate_string()
and get_next_directory_entry()
) increase the reference count of the returned
object. If the object is no longer needed then release_object()
has to
be called to decrease the reference count of the object again. In contrast to
allocation and getter functions, the reference count for an object returned by
a callback is not increased and release_object()
must not be called for
such an object in response to a callback.
There are functions (e.g. get_file_info()
) that only return valid objects
under certain conditions. This conditions are documented for the specific
functions. For invalid objects release_object()
must not be called.
There are also function (e.g. set_program_stdio_redirection()
) that have
conditionally unused object parameters. Under which conditions an object
parameter is unused is documented for the specific functions. For unused
object parameters 0 has to be passed as object ID.
The RED Brick API is more complex then the typical Brick API and requires more elaborate error reporting than the TCP/IP protocol can provide with its 2bit error code. Therefore, almost all functions of the RED Brick API return an 8bit error code. Possible error codes are:
If a function returns an error code other than Success then its other return values (if any) are invalid and must not be used.
The error code InvalidOperation is returned if the requested operation cannot be performed because the current state of the object does not allow it. For example, trying to append an item to a full list object or trying to undefine an already undefined program.
The error code NotSupported is returned if the requested operation can never be performed. For example, trying to append a list object to itself, trying to get the name of a file object with type Pipe or trying to create a directory non-recursively with more than the last part of the directory name referring to non-existing directories.
String objects store UTF-8 encoded data.
BrickRED.
create_session
(lifetime)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
expire_session
(session_id)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For error_code:
BrickRED.
expire_session_unchecked
(session_id)¶Parameters: |
|
---|---|
Returns: |
|
BrickRED.
keep_session_alive
(session_id, lifetime)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For error_code:
BrickRED.
release_object
(object_id, session_id)¶Parameters: |
|
---|---|
Returns: |
|
Decreases the reference count of an object by one and returns the resulting error code. If the reference count reaches zero the object gets destroyed.
The following constants are available for this function:
For error_code:
BrickRED.
release_object_unchecked
(object_id, session_id)¶Parameters: |
|
---|---|
Returns: |
|
BrickRED.
allocate_string
(length_to_reserve, buffer, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Allocates a new string object, reserves length_to_reserve
bytes memory
for it and sets up to the first 60 bytes. Set length_to_reserve
to the
length of the string that should be stored in the string object.
Returns the object ID of the new string object and the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
truncate_string
(string_id, length)¶Parameters: |
|
---|---|
Returns: |
|
Truncates a string object to length
bytes and returns the resulting
error code.
The following constants are available for this function:
For error_code:
BrickRED.
get_string_length
(string_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the length of a string object and the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
set_string_chunk
(string_id, offset, buffer)¶Parameters: |
|
---|---|
Returns: |
|
Sets a chunk of up to 58 bytes in a string object beginning at offset
.
Returns the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
get_string_chunk
(string_id, offset)¶Parameters: |
|
---|---|
Return Object: |
|
Returns a chunk up to 63 bytes from a string object beginning at offset
and
returns the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
allocate_list
(length_to_reserve, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Allocates a new list object and reserves memory for length_to_reserve
items. Set length_to_reserve
to the number of items that should be stored
in the list object.
Returns the object ID of the new list object and the resulting error code.
When a list object gets destroyed then the reference count of each object in the list object is decreased by one.
The following constants are available for this function:
For error_code:
BrickRED.
get_list_length
(list_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the length of a list object in items and the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
get_list_item
(list_id, index, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the object ID and type of the object stored at index
in a list
object and returns the resulting error code.
Possible object types are:
The following constants are available for this function:
For error_code:
For type:
BrickRED.
append_to_list
(list_id, item_object_id)¶Parameters: |
|
---|---|
Returns: |
|
Appends an object to a list object and increases the reference count of the appended object by one.
Returns the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
remove_from_list
(list_id, index)¶Parameters: |
|
---|---|
Returns: |
|
Removes the object stored at index
from a list object and decreases the
reference count of the removed object by one.
Returns the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
open_file
(name_string_id, flags, permissions, uid, gid, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Opens an existing file or creates a new file and allocates a new file object for it.
FIXME: name has to be absolute
The reference count of the name string object is increased by one. When the file object gets destroyed then the reference count of the name string object is decreased by one. Also the name string object is locked and cannot be modified while the file object holds a reference to it.
The flags
parameter takes a ORed combination of the following possible file
flags (in hexadecimal notation):
FIXME: explain Temporary and Replace flag
The permissions
parameter takes a ORed combination of the following
possible file permissions (in octal notation) that match the common UNIX
permission bits:
Returns the object ID of the new file object and the resulting error code.
The following constants are available for this function:
For flags:
For permissions:
For error_code:
BrickRED.
create_pipe
(flags, length, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Creates a new pipe and allocates a new file object for it.
The flags
parameter takes a ORed combination of the following possible
pipe flags (in hexadecimal notation):
The length of the pipe buffer can be specified with the length
parameter
in bytes. If length is set to zero, then the default pipe buffer length is used.
Returns the object ID of the new file object and the resulting error code.
The following constants are available for this function:
For flags:
For error_code:
BrickRED.
get_file_info
(file_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns various information about a file and the resulting error code.
Possible file types are:
If the file type is Pipe then the returned name string object is invalid,
because a pipe has no name. Otherwise the returned name string object was used
to open or create the file object, as passed to open_file()
.
The returned flags were used to open or create the file object, as passed to
open_file()
or create_pipe()
. See the respective function for a list
of possible file and pipe flags.
FIXME: everything except flags and length is invalid if file type is Pipe
The following constants are available for this function:
For error_code:
For type:
For flags:
For permissions:
BrickRED.
read_file
(file_id, length_to_read)¶Parameters: |
|
---|---|
Return Object: |
|
Reads up to 62 bytes from a file object.
Returns the bytes read, the actual number of bytes read and the resulting error code.
If there is not data to be read, either because the file position reached end-of-file or because there is not data in the pipe, then zero bytes are returned.
If the file object was created by open_file()
without the NonBlocking
flag or by create_pipe()
without the NonBlockingRead flag then the
error code NotSupported is returned.
The following constants are available for this function:
For error_code:
BrickRED.
read_file_async
(file_id, length_to_read)¶Parameters: |
|
---|---|
Returns: |
|
Reads up to 263 - 1 bytes from a file object asynchronously.
Reports the bytes read (in 60 byte chunks), the actual number of bytes read and
the resulting error code via the CALLBACK_ASYNC_FILE_READ
callback.
If there is not data to be read, either because the file position reached end-of-file or because there is not data in the pipe, then zero bytes are reported.
If the file object was created by open_file()
without the NonBlocking
flag or by create_pipe()
without the NonBlockingRead flag then the error
code NotSupported is reported via the CALLBACK_ASYNC_FILE_READ
callback.
BrickRED.
abort_async_file_read
(file_id)¶Parameters: |
|
---|---|
Returns: |
|
Aborts a read_file_async()
operation in progress.
Returns the resulting error code.
On success the CALLBACK_ASYNC_FILE_READ
callback will report OperationAborted.
The following constants are available for this function:
For error_code:
BrickRED.
write_file
(file_id, buffer, length_to_write)¶Parameters: |
|
---|---|
Return Object: |
|
Writes up to 61 bytes to a file object.
Returns the actual number of bytes written and the resulting error code.
If the file object was created by open_file()
without the NonBlocking
flag or by create_pipe()
without the NonBlockingWrite flag then the
error code NotSupported is returned.
The following constants are available for this function:
For error_code:
BrickRED.
write_file_unchecked
(file_id, buffer, length_to_write)¶Parameters: |
|
---|---|
Returns: |
|
Writes up to 61 bytes to a file object.
Does neither report the actual number of bytes written nor the resulting error code.
If the file object was created by open_file()
without the NonBlocking
flag or by create_pipe()
without the NonBlockingWrite flag then the
write operation will fail silently.
BrickRED.
write_file_async
(file_id, buffer, length_to_write)¶Parameters: |
|
---|---|
Returns: |
|
Writes up to 61 bytes to a file object.
Reports the actual number of bytes written and the resulting error code via the
CALLBACK_ASYNC_FILE_WRITE
callback.
If the file object was created by open_file()
without the NonBlocking
flag or by create_pipe()
without the NonBlockingWrite flag then the
error code NotSupported is reported via the CALLBACK_ASYNC_FILE_WRITE
callback.
BrickRED.
set_file_position
(file_id, offset, origin)¶Parameters: |
|
---|---|
Return Object: |
|
Set the current seek position of a file object relative to origin
.
Possible file origins are:
Returns the resulting absolute seek position and error code.
If the file object was created by create_pipe()
then it has no seek
position and the error code InvalidSeek is returned.
The following constants are available for this function:
For origin:
For error_code:
BrickRED.
get_file_position
(file_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the current seek position of a file object and returns the resulting error code.
If the file object was created by create_pipe()
then it has no seek
position and the error code InvalidSeek is returned.
The following constants are available for this function:
For error_code:
BrickRED.
set_file_events
(file_id, events)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For events:
For error_code:
BrickRED.
get_file_events
(file_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
For events:
BrickRED.
open_directory
(name_string_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Opens an existing directory and allocates a new directory object for it.
FIXME: name has to be absolute
The reference count of the name string object is increased by one. When the directory object is destroyed then the reference count of the name string object is decreased by one. Also the name string object is locked and cannot be modified while the directory object holds a reference to it.
Returns the object ID of the new directory object and the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
get_directory_name
(directory_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the name of a directory object, as passed to open_directory()
, and
the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
get_next_directory_entry
(directory_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the next entry in a directory object and the resulting error code.
If there is not next entry then error code NoMoreData is returned. To rewind
a directory object call rewind_directory()
.
Possible directory entry types are:
The following constants are available for this function:
For error_code:
For type:
BrickRED.
rewind_directory
(directory_id)¶Parameters: |
|
---|---|
Returns: |
|
Rewinds a directory object and returns the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
create_directory
(name_string_id, flags, permissions, uid, gid)¶Parameters: |
|
---|---|
Returns: |
|
FIXME: name has to be absolute
The following constants are available for this function:
For flags:
For permissions:
For error_code:
BrickRED.
get_processes
(session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
spawn_process
(executable_string_id, arguments_list_id, environment_list_id, working_directory_string_id, uid, gid, stdin_file_id, stdout_file_id, stderr_file_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
kill_process
(process_id, signal)¶Parameters: |
|
---|---|
Returns: |
|
Sends a UNIX signal to a process object and returns the resulting error code.
Possible UNIX signals are:
The following constants are available for this function:
For signal:
For error_code:
BrickRED.
get_process_command
(process_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the executable, arguments, environment and working directory used to
spawn a process object, as passed to spawn_process()
, and the resulting
error code.
The following constants are available for this function:
For error_code:
BrickRED.
get_process_identity
(process_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the process ID and the user and group ID used to spawn a process object,
as passed to spawn_process()
, and the resulting error code.
The process ID is only valid if the state is Running or Stopped, see
get_process_state()
.
The following constants are available for this function:
For error_code:
BrickRED.
get_process_stdio
(process_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the stdin, stdout and stderr files used to spawn a process object, as
passed to spawn_process()
, and the resulting error code.
The following constants are available for this function:
For error_code:
BrickRED.
get_process_state
(process_id)¶Parameters: |
|
---|---|
Return Object: |
|
Returns the current state, timestamp and exit code of a process object, and the resulting error code.
Possible process states are:
The timestamp represents the UNIX time since when the process is in its current state.
The exit code is only valid if the state is Error, Exited, Killed or Stopped and has different meanings depending on the state:
Possible exit/error codes in Error state are:
The CannotExecute error can be caused by the executable being opened for writing.
The following constants are available for this function:
For error_code:
For state:
BrickRED.
get_programs
(session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
define_program
(identifier_string_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
purge_program
(program_id, cookie)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For error_code:
BrickRED.
get_program_identifier
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
get_program_root_directory
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
FIXME: root directory is absolute: <home>/programs/<identifier>
The following constants are available for this function:
For error_code:
BrickRED.
set_program_command
(program_id, executable_string_id, arguments_list_id, environment_list_id, working_directory_string_id)¶Parameters: |
|
---|---|
Returns: |
|
FIXME: working directory is relative to <home>/programs/<identifier>/bin
The following constants are available for this function:
For error_code:
BrickRED.
get_program_command
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
FIXME: working directory is relative to <home>/programs/<identifier>/bin
The following constants are available for this function:
For error_code:
BrickRED.
set_program_stdio_redirection
(program_id, stdin_redirection, stdin_file_name_string_id, stdout_redirection, stdout_file_name_string_id, stderr_redirection, stderr_file_name_string_id)¶Parameters: |
|
---|---|
Returns: |
|
FIXME: stdio file names are relative to <home>/programs/<identifier>/bin
The following constants are available for this function:
For stdin_redirection:
For stdout_redirection:
For stderr_redirection:
For error_code:
BrickRED.
get_program_stdio_redirection
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
FIXME: stdio file names are relative to <home>/programs/<identifier>/bin
The following constants are available for this function:
For error_code:
For stdin_redirection:
For stdout_redirection:
For stderr_redirection:
BrickRED.
set_program_schedule
(program_id, start_mode, continue_after_error, start_interval, start_fields_string_id)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For start_mode:
For error_code:
BrickRED.
get_program_schedule
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
For start_mode:
BrickRED.
get_program_scheduler_state
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
FIXME: message is currently valid in error-occurred state only
The following constants are available for this function:
For error_code:
For state:
BrickRED.
continue_program_schedule
(program_id)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For error_code:
BrickRED.
start_program
(program_id)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For error_code:
BrickRED.
get_last_spawned_program_process
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
get_custom_program_option_names
(program_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
set_custom_program_option_value
(program_id, name_string_id, value_string_id)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For error_code:
BrickRED.
get_custom_program_option_value
(program_id, name_string_id, session_id)¶Parameters: |
|
---|---|
Return Object: |
|
The following constants are available for this function:
For error_code:
BrickRED.
remove_custom_program_option
(program_id, name_string_id)¶Parameters: |
|
---|---|
Returns: |
|
The following constants are available for this function:
For error_code:
BrickRED.
get_identity
()¶Return Object: |
|
---|
Returns the UID, the UID where the Brick is connected to, the position, the hardware and firmware version as well as the device identifier.
The position is the position in the stack from '0' (bottom) to '8' (top).
The device identifier numbers can be found here. There is also a constant for the device identifier of this Brick.
Callbacks can be registered to receive
time critical or recurring data from the device. The registration is done
with the register_callback()
function of
the device object. The first parameter is the callback ID and the second
parameter the callback function:
def my_callback(param):
print(param)
red.register_callback(BrickRED.CALLBACK_EXAMPLE, my_callback)
The available constants with inherent number and type of parameters are described below.
Note
Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.
BrickRED.
CALLBACK_ASYNC_FILE_READ
¶Callback Parameters: |
|
---|
This callback reports the result of a call to the read_file_async()
function.
The following constants are available for this function:
For error_code:
BrickRED.
CALLBACK_ASYNC_FILE_WRITE
¶Callback Parameters: |
|
---|
This callback reports the result of a call to the write_file_async()
function.
The following constants are available for this function:
For error_code:
BrickRED.
CALLBACK_FILE_EVENTS_OCCURRED
¶Callback Parameters: |
|
---|
The following constants are available for this function:
For events:
BrickRED.
CALLBACK_PROCESS_STATE_CHANGED
¶Callback Parameters: |
|
---|
The following constants are available for this function:
For state:
BrickRED.
CALLBACK_PROGRAM_SCHEDULER_STATE_CHANGED
¶Callback Parameters: |
|
---|
BrickRED.
CALLBACK_PROGRAM_PROCESS_SPAWNED
¶Callback Parameters: |
|
---|
Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.
BrickRED.
get_api_version
()¶Return Object: |
|
---|
Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.
BrickRED.
get_response_expected
(function_id)¶Parameters: |
|
---|---|
Returns: |
|
Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.
For getter functions this is enabled by default and cannot be disabled,
because those functions will always send a response. For callback configuration
functions it is enabled by default too, but can be disabled by
set_response_expected()
. For setter functions it is disabled by default
and can be enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For function_id:
BrickRED.
set_response_expected
(function_id, response_expected)¶Parameters: |
|
---|---|
Returns: |
|
Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.
Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.
The following constants are available for this function:
For function_id:
BrickRED.
set_response_expected_all
(response_expected)¶Parameters: |
|
---|---|
Returns: |
|
Changes the response expected flag for all setter and callback configuration functions of this device at once.
BrickRED.
DEVICE_IDENTIFIER
¶This constant is used to identify a RED Brick.
The get_identity()
function and the
IPConnection.CALLBACK_ENUMERATE
callback of the IP Connection have a device_identifier
parameter to specify
the Brick's or Bricklet's type.
BrickRED.
DEVICE_DISPLAY_NAME
¶This constant represents the human readable name of a RED Brick.