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:
portint or str

the port name

baudrateint, optional
bytesizeint, optional
paritystr, optional

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

stopbitsint, optional
timeoutint, optional

the timeout for read(): -1=block

xonxoffint, optional
rtsctsint, optional
dsrdtrint, optional
input_historybool, optional

True if an input_history should be used

os_buffer_sizeint, optional

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

clockmisc.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 transferred to the input_history.

property baudrate

Getter for baudrate.

property bytesize

Getter for bytesize.

clear(skip_input_history=False)

Clear the serial port.

Parameters:
skip_input_historybool, 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()).

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.

property clock

Getter for clock.

close()

Close the serial port.

property dsrdtr

Getter for dsrdtr.

static get_available_ports()

Return a list of strings representing the available serial ports.

Returns:
arrlist

a list of strings representing the available serial ports

Notes

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

property has_input_history

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

property input_history

Getter for input_history.

property logging

Getter for logging.

property os_buffer_size

Getter for os_buffer_size.

property parity

Getter for parity.

poll()

Poll the serial port.

If a input_history is used, it will be added.

Returns:
outint

a single integer value of the received byte

read_input()

Read all input from serial port.

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

Returns:
outlist of int

a list of integer values of the received bytes

read_line(duration=None, callback_function=None, process_control_events=True)

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

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

Parameters:
durationint, optional

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

callback_functionfunction, optional

function to repeatedly execute during waiting loop

process_control_eventsbool, optional

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

Returns:
linebytes or str

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!

property rtscts

Getter for rtscts.

send(data)

Send data via the serial port.

Parameters:
dataint

the data to be sent

send_line(data, carriage_return=False, line_feed=True)

Send a line of data via the serial port.

Parameters:
databytes or str

the data to be sent

carriage_returnbool, optional

whether to add a carriage return at end of data (default=True)

line_feedbool, optional

whether to add a line feed at end of data (default=True)

property serial

Getter for serial.

set_logging(onoff)

Set logging of this object on or off

Parameters:
onoffbool

set logging on (True) or off (False)

property stopbits

Getter for stopbits.

property timeout

Getter for timeout.

property xonxoff

Getter for xonxoff.