inspy.transport

Top level module for transport.

class inspy.transport.PyVisaTransport(visa_address)[source]

Bases: TransportBase

Transport layer for using pyvisa.

CHUNK_SIZE = 102400
__init__(visa_address)[source]

Initialize PyVisaTransport.

Parameters:

visa_address (str) – The VISA address.

clear()[source]

Clear buffers.

Return type:

None

command(command)

Send a command to the instrument.

The command (as a string) will be encoded to bytes using self.encoding and a termination (self.termination) will be added.

Parameters:

command (str) – The command

Return type:

None

command_with_block_data(command, items, data_type='B', is_big_endian=False)[source]

Send a command with encoded data.

Parameters:
  • command (str) – The command

  • items (Sequence[int | float]) – Items of data to be encoded

  • data_type (BINARY_DATATYPES) – struct style format used for items.

  • is_big_endian (bool) – Endian.

Return type:

None

connect()[source]

Connect to the instrument.

Return type:

None

disconnect()[source]

Disconnect from the instrument.

Return type:

None

encoding = 'ascii'
get_timeout()[source]

Get PyVisa timeout value.

Return type:

float

Returns:

Timeout value in seconds.

query(command)

Send a command and receive a result.

The command will be sent using command (i.e. with encoding and termination) and then a read performed. The read bytes will be decoded (using self.encoding) and then stripped of leading and trailing whitespace.

Parameters:

command (str) – The command

Return type:

str

query_for_block_data(command, data_type='B', container=<class 'bytes'>, is_big_endian=False)[source]

Query for encoded data.

Parameters:
  • command (str) – The command

  • data_type (BINARY_DATATYPES) –

    struct style format used received items.

  • container (Any) – The type used to hold the items. Defaults to bytes. Pass a callable such as list to get a Sequence[int | float].

  • is_big_endian (bool) – Endian.

Return type:

bytes | Sequence[int | float]

query_with_block_data_for_block_data(command, items, command_data_type='B', result_data_type='B', result_container=<class 'bytes'>, is_big_endian=False)[source]

Query with encoded data and also receive encoded data.

Parameters:
  • command (str) – The command.

  • items (Sequence[int | float]) – Items of data to be encoded for sending.

  • command_data_type (BINARY_DATATYPES) – struct style format used for items to send. See struct format characters for details.

  • result_data_type (BINARY_DATATYPES) –

    struct style format used for received items. See struct format characters for details.

  • result_container (Any) – The type used to hold received items (e.g., list).

  • is_big_endian (bool) – Endian.

Return type:

Sequence[int | float]

read()

Read the output from the instrument output buffer.

The read bytes will be decoded (using self.encoding) and then stripped of leading and trailing whitespace.

Return type:

str

Returns:

Instrument Output.

read_bytes()[source]

Read raw bytes without parsing or decoding.

Return type:

bytes

resource: MessageBasedResource
set_encoding(encoding)

Set transport encoding.

Parameters:

encoding (str) – The encoding to use.

Return type:

None

set_termination(termination)

Set termination character.

Parameters:

termination (str) – The termination character.

Return type:

None

set_timeout(timeout)[source]

Set PyVisa timeout.

Parameters:

timeout (float) – Timeout in seconds.

Return type:

None

termination = b'\r\n'
write_bytes(data)[source]

Write raw bytes without any formatting or encoding.

Return type:

None

class inspy.transport.SerialTransport(visa_resource_name=None, port=None, vid=None, pid=None, serial_number=None, baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=2.0, command_delay=0.0, dtr=None, rts=None, dsrdtr=False, rtscts=False, xonxoff=False)[source]

Bases: TransportBase

Transport layer for using serial communication.

__init__(visa_resource_name=None, port=None, vid=None, pid=None, serial_number=None, baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=2.0, command_delay=0.0, dtr=None, rts=None, dsrdtr=False, rtscts=False, xonxoff=False)[source]

Initialize SerialTransport with serial port settings.

Either the port can be specified, or the USB VID and PID. Optionally if the USB VID and PID are provided then the serial_number can also be specified.

Parameters:
  • visa_resource_name (Optional[str]) – https://pyvisa.readthedocs.io/en/latest/introduction/names.html#visa-resource-syntax-and-examples

  • port (Optional[str]) – COM port name.

  • vid (Optional[int]) – USB vendor ID.

  • pid (Optional[int]) – USB product ID.

  • serial_number (Optional[str]) – USB serial number.

  • baudrate (int) – Communication speed in bits per second.

  • bytesize (int) – Number of data bits.

  • parity ('N' | 'E' | 'O' | 'S' | 'M') – Parity setting.

  • stopbits (int) – Number of stop bits.

  • timeout (float) – Read timeout in seconds.

  • command_delay (float) – Seconds to wait after each write. Defaults to 0.0 (no delay). Some unframed serial instruments (e.g. Tenma/Korad power supplies) drop commands or return stale responses if commands are sent back-to-back; a small delay paces them.

  • dtr (Optional[bool]) – If not None, explicitly drive the DTR line to this state after opening the port. Defaults to None (leave pyserial’s default behaviour).

  • rts (Optional[bool]) – If not None, explicitly drive the RTS line to this state after opening the port. Defaults to None.

  • dsrdtr (bool) – Enable DTR/DSR handshaking. Defaults to False.

  • rtscts (bool) – Enable RTS/CTS hardware flow control. Defaults to False.

  • xonxoff (bool) – Enable XON/XOFF software flow control. Defaults to False.

Raises:

SerialException – If serial port could not be found.

clear()[source]

Clear input and output buffers.

Raises:

PortNotOpenError – If not connected.

Return type:

None

command(command)

Send a command to the instrument.

The command (as a string) will be encoded to bytes using self.encoding and a termination (self.termination) will be added.

Parameters:

command (str) – The command

Return type:

None

connect()[source]

Connect to the serial device.

Raises:

SerialException – If serial port could not be found.

Return type:

None

disconnect()[source]

Disconnect from the serial device.

Return type:

None

encoding = 'ascii'
get_timeout()[source]

Get the timeout for serial communication.

Returns:

Timeout in seconds.

Return type:

float

query(command)

Send a command and receive a result.

The command will be sent using command (i.e. with encoding and termination) and then a read performed. The read bytes will be decoded (using self.encoding) and then stripped of leading and trailing whitespace.

Parameters:

command (str) – The command

Return type:

str

read()

Read the output from the instrument output buffer.

The read bytes will be decoded (using self.encoding) and then stripped of leading and trailing whitespace.

Return type:

str

Returns:

Instrument Output.

read_bytes()[source]

Read raw bytes from the serial device.

Parameters:

data – Data to send as raw bytes.

Returns:

Response from the device as raw bytes.

Return type:

bytes

Raises:

PortNotOpenError – If not connected.

set_encoding(encoding)

Set transport encoding.

Parameters:

encoding (str) – The encoding to use.

Return type:

None

set_termination(termination)

Set termination character.

Parameters:

termination (str) – The termination character.

Return type:

None

set_timeout(timeout)[source]

Set the timeout for serial communication.

Parameters:

timeout (float) – Timeout in seconds.

Return type:

None

termination = b'\n'
write_bytes(data)[source]

Write raw bytes to the serial device.

Parameters:

data (bytes) – Data to send as raw bytes.

Raises:

PortNotOpenError – If not connected.

Return type:

None

class inspy.transport.TransportBase(*args, **kwargs)[source]

Bases: ABC

An abstract base class defining a contract for transport mechanisms.

It requires send and receive methods to be implemented by any concrete subclass.

__init__(*args, **kwargs)[source]
abstract clear()[source]

Clear buffers.

Return type:

None

command(command)[source]

Send a command to the instrument.

The command (as a string) will be encoded to bytes using self.encoding and a termination (self.termination) will be added.

Parameters:

command (str) – The command

Return type:

None

abstract connect()[source]

Establish connection.

Return type:

None

abstract disconnect()[source]

Release the connection.

Return type:

None

encoding = 'ascii'
abstract get_timeout()[source]

Get transport timeout.

Return type:

float

Returns:

Timeout in seconds.

query(command)[source]

Send a command and receive a result.

The command will be sent using command (i.e. with encoding and termination) and then a read performed. The read bytes will be decoded (using self.encoding) and then stripped of leading and trailing whitespace.

Parameters:

command (str) – The command

Return type:

str

read()[source]

Read the output from the instrument output buffer.

The read bytes will be decoded (using self.encoding) and then stripped of leading and trailing whitespace.

Return type:

str

Returns:

Instrument Output.

abstract read_bytes()[source]

Read raw bytes without parsing or decoding.

Return type:

bytes

set_encoding(encoding)[source]

Set transport encoding.

Parameters:

encoding (str) – The encoding to use.

Return type:

None

set_termination(termination)[source]

Set termination character.

Parameters:

termination (str) – The termination character.

Return type:

None

abstract set_timeout(timeout)[source]

Set transport timeout.

Parameters:

timeout (float) – Timeout in seconds.

Return type:

None

termination = b'\n'
abstract write_bytes(data)[source]

Write raw bytes without any formatting or encoding.

Return type:

None