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)[source]

A class implementing a serial port input and output.

Methods

__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)[source]

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

port to use

baudrate : int, optional

bytesize : int, optional

parity : str, optional

parity:’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)[source]

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()[source]

Close the serial port.

dsrdtr

Getter for dsrdtr.

static get_available_ports()[source]

Return an 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()[source]

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()[source]

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)[source]

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

Parameters:

duration : int, optional

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

Returns:

line : str

See also

design.experiment.register_wait_callback_function

Notes

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

rtscts

Getter for rtscts.

send(data)[source]

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.