expyriment.io.SerialPort

class expyriment.io.SerialPort(port, baudrate=None, bytesize=None, parity=None, stopbits=None, timeout=None, xonxoff=None, rtscts=None, dsrdtr=None, input_history=None, os_buffer_size=None, clock=None)

A class implementing a serial port input and output.

__init__(port, baudrate=None, bytesize=None, parity=None, stopbits=None, timeout=None, xonxoff=None, rtscts=None, dsrdtr=None, input_history=None, os_buffer_size=None, clock=None)

Create a serial port input and output.

The port argument will accept the number of the port (e.g. 0 for COM1) as well as a string describing the full port location (“COM1” or “/dev/ttyS0”).

Parameters:

port : int or str

the port name

baudrate : int, optional

bytesize : int, optional

parity : str, optional

‘E’=even, ‘O’=odd, ‘N’=none

stopbits : int, optional

timeout : int, optional

the timeout for read(): -1=block

xonxoff : int, optional

rtscts : int, optional

dsrdtr : int, optional

input_history : bool, optional

True if an input_history should be used

os_buffer_size : int, optional

the size of the receive input_history provided by the operating system in bytes

clock : misc.Clock, optional

an experimental clock

(optional)

Notes

An input_history can be used to overcome the size limitation of the receive buffer of the operating system. An input_history consists of a misc.ByteBuffer instance. In order to not miss any input, the serial port has to be updated regularly (i.e. calling read_input() or clear() before the receive buffer will be full). If the receive buffer size is set correctly, a warning will be given, when the input_history was not updated fast enough. Importantly, the fuller the receive buffer is, the longer clearing and polling will take (this can be more than 1 ms!), since all the bytes have to be transfered to the input_history.

baudrate

Getter for baudrate.

bytesize

Getter for bytesize.

clear(skip_input_history=False)

Clear the serial port.

Parameters:

skip_input_history : bool, optional

if True available data will not be copied to the input_history. (default = False)

Notes

If an input_history is used, all data in the receive buffer, will be added to the history before clearing (via read_input()). Note: The copy process might take a few milliseconds. If you need a very fast clearing of the device buffer, you should skip copying the data into the input_history using the skip_input_history parameter.

clock

Getter for clock.

close()

Close the serial port.

dsrdtr

Getter for dsrdtr.

static get_available_ports()

Return a list of strings representing the available serial ports.

Returns:

arr : list

list of strings representing the available serial ports

Notes

If pyserial is not installed, ‘None’ will be returned.

has_input_history

Returns if a input_history exists or not (True / False).

input_history

Getter for input_history.

logging

Getter for logging.

os_buffer_size

Getter for os_buffer_size.

parity

Getter for parity.

poll()

Poll the serial port.

If a input_history is used, it will be added.

Returns:

out : byte

one byte only will be returned

read_input()

Read all input from serial port.

If a input_history is used, all received data will be added.

Returns:out : list of bytes
read_line(duration=None, callback_function=None, process_control_events=True)

Read a line from serial port (until newline) and return string.

The function is waiting for input. Use the duration parameter to avoid too long program blocking.

Parameters:

duration : int, optional

try to read for given amount of time (default=None)

callback_function : function, optional

function to repeatedly execute during waiting loop

process_control_events : bool, optional

process io.Keyboard.process_control_keys() and io.Mouse.process_quit_event() (default = True)

Returns:

line : str

See also

design.experiment.register_wait_callback_function

Notes

This will also by default process control events (quit and pause). Thus, keyboard events will be cleared from the cue and cannot be received by a Keyboard().check() anymore!

rtscts

Getter for rtscts.

send(data)

Send data via the serial port.

Parameters:

data : int

data to be send (int)

serial

Getter for serial.

set_logging(onoff)

Set logging of this object on or off

Parameters:

onoff : bool

set logging on (True) or off (False)

Notes

See also design.experiment.set_log_level fur further information about event logging.

stopbits

Getter for stopbits.

timeout

Getter for timeout.

xonxoff

Getter for xonxoff.