PyMoDAQ Logo

Supported instruments

  • Supported instruments

Documentation

  • 1. Quick Start
  • 2. User’s Guide
  • 3. Tutorials
  • 4. Data Management
  • 5. Developer’s Guide
  • 6. Glossary Terms
  • 7. Library Reference
    • 7.1. Control modules
    • 7.2. Extensions
    • 7.3. Utility Modules
    • 7.4. Utility Libraries
      • 7.4.1. Utility Classes
      • 7.4.2. TCP/IP related methods
        • 7.4.2.1. Serializing object to bytes and back
        • 7.4.2.2. Custom Sockets to implement PyMoDAQ protocol
        • 7.4.2.3. Base classes as TCP server and client
      • 7.4.3. Units conversion
      • 7.4.4. Mathematical utilities
      • 7.4.5. Scan utilities
      • 7.4.6. File management
      • 7.4.7. Data Management
      • 7.4.8. parameter
  • 8. About

Related projects

  • PyMoDAQ Femto
  • PyMoDAQ Plugins DAQmx
PyMoDAQ
  • 7. Library Reference
  • 7.4. Utility Libraries
  • 7.4.2. TCP/IP related methods
  • View page source

7.4.2. TCP/IP related methods

7.4.2.1. Serializing object to bytes and back

Created the 20/10/2023

@author: Sebastien Weber

class pymodaq.utils.tcp_ip.serializer.DeSerializer(bytes_string: bytes | Socket = None)[source]

Used to DeSerialize bytes to python objects, numpy arrays and PyMoDAQ Axis, DataWithAxes and DataToExport objects

Parameters:

bytes_string (bytes or Socket) – the bytes string to deserialize into an object: int, float, string, arrays, list, Axis, DataWithAxes… Could also be a Socket object reading bytes from the network having a get_first_nbytes method

See also

SocketString, Socket

Methods

axis_deserialization()

Convert bytes into an Axis object

boolean_deserialization()

Convert bytes into a boolean object

bytes_to_int(bytes_string)

Convert a bytes of length 4 into an integer

bytes_to_nd_array(data, dtype, shape)

Convert bytes to a ndarray given a certain numpy dtype and shape

bytes_to_scalar(data, dtype)

Convert bytes to a scalar given a certain numpy dtype

dte_deserialization()

Convert bytes into a DataToExport object

dwa_deserialization()

Convert bytes into a DataWithAxes object

list_deserialization()

Convert bytes into a list of homogeneous objects

ndarray_deserialization()

Convert bytes into a numpy ndarray object

scalar_deserialization()

Convert bytes into a python number object

string_deserialization()

Convert bytes into a str object

type_and_object_deserialization()

Deserialize specific objects from their binary representation (inverse of Serializer.type_and_object_serialization).

bytes_deserialization

bytes_to_string

from_b64_string

parameter_deserialization

axis_deserialization() → Axis[source]

Convert bytes into an Axis object

Convert the first bytes into an Axis reading first information about the Axis

Returns:

Axis

Return type:

the decoded Axis

boolean_deserialization() → bool[source]

Convert bytes into a boolean object

Get first the data type from a string deserialization, then the data length and finally convert this length of bytes into a number (float, int) and cast it to a bool

Returns:

bool

Return type:

the decoded boolean

static bytes_to_int(bytes_string: bytes) → int[source]

Convert a bytes of length 4 into an integer

static bytes_to_nd_array(data: bytes, dtype: dtype, shape: Tuple[int]) → ndarray[source]

Convert bytes to a ndarray given a certain numpy dtype and shape

Parameters:
  • data (bytes) –

  • dtype (np.dtype) –

  • shape (tuple of int) –

Return type:

np.ndarray

static bytes_to_scalar(data: bytes, dtype: dtype) → complex[source]

Convert bytes to a scalar given a certain numpy dtype

Parameters:
  • data (bytes) –

  • dtype (np.dtype) –

Return type:

numbers.Number

dte_deserialization() → DataToExport[source]

Convert bytes into a DataToExport object

Convert the first bytes into a DataToExport reading first information about the underlying data

Returns:

DataToExport

Return type:

the decoded DataToExport

dwa_deserialization() → DataWithAxes[source]

Convert bytes into a DataWithAxes object

Convert the first bytes into a DataWithAxes reading first information about the underlying data

Returns:

DataWithAxes

Return type:

the decoded DataWithAxes

list_deserialization() → list[source]

Convert bytes into a list of homogeneous objects

Convert the first bytes into a list reading first information about the list elt types, length …

Returns:

list

Return type:

the decoded list

ndarray_deserialization() → ndarray[source]

Convert bytes into a numpy ndarray object

Convert the first bytes into a ndarray reading first information about the array’s data

Returns:

ndarray

Return type:

the decoded numpy array

scalar_deserialization() → complex[source]

Convert bytes into a python number object

Get first the data type from a string deserialization, then the data length and finally convert this length of bytes into a number (float, int)

Returns:

numbers.Number

Return type:

the decoded number

string_deserialization() → str[source]

Convert bytes into a str object

Convert first the fourth first bytes into an int encoding the length of the string to decode

Returns:

str

Return type:

the decoded string

type_and_object_deserialization() → bool | bytes | str | complex | float | int | list | ndarray | Axis | DataWithAxes | DataToExport | ParameterWithPath[source]

Deserialize specific objects from their binary representation (inverse of Serializer.type_and_object_serialization).

See also

Serializer.dwa_serialization, Serializer.dte_serialization

class pymodaq.utils.tcp_ip.serializer.SerializableTypes(value)[source]

Type names of serializable types

class pymodaq.utils.tcp_ip.serializer.Serializer(obj: bool | bytes | str | complex | float | int | list | ndarray | Axis | DataWithAxes | DataToExport | ParameterWithPath | None = None)[source]

Used to Serialize to bytes python objects, numpy arrays and PyMoDAQ DataWithAxes and DataToExport objects

Methods

axis_serialization(axis)

Convert an Axis object into a bytes message together with the info to convert it back

dte_serialization(dte)

Convert a DataToExport into a bytes string

dwa_serialization(dwa)

Convert a DataWithAxes into a bytes string

int_to_bytes(an_integer)

Convert an unsigned integer into a byte array of length 4 in big endian

list_serialization(list_object)

Convert a list of objects into a bytes message together with the info to convert it back

ndarray_serialization(array)

Convert a ndarray into a bytes message together with the info to convert it back

object_type_serialization(obj)

Convert an object type into a bytes message as a string together with the info to convert it back

scalar_serialization(scalar)

Convert a scalar into a bytes message together with the info to convert it back

str_len_to_bytes(message)

Convert a string and its length to two bytes :param message: the message to convert :type message: str

string_serialization(string)

Convert a string into a bytes message together with the info to convert it back

to_bytes()

Generic method to obtain the bytes string from various objects

type_and_object_serialization([obj])

Serialize an object with its type, such that it can be retrieved by DeSerializer.type_and_object_deserialization.

bytes_serialization

str_to_bytes

to_b64_string

axis_serialization(axis: Axis) → bytes[source]

Convert an Axis object into a bytes message together with the info to convert it back

Parameters:

axis (Axis) –

Returns:

bytes

Return type:

the total bytes message to serialize the Axis

Notes

The bytes sequence is constructed as:

  • serialize the type: ‘Axis’

  • serialize the axis label

  • serialize the axis units

  • serialize the axis array

  • serialize the axis

  • serialize the axis spread_order

dte_serialization(dte: DataToExport) → bytes[source]

Convert a DataToExport into a bytes string

Parameters:

dte (DataToExport) –

Returns:

bytes

Return type:

the total bytes message to serialize the DataToExport

Notes

The bytes sequence is constructed as:

  • serialize the string type: ‘DataToExport’

  • serialize the timestamp: float

  • serialize the name

  • serialize the list of DataWithAxes

dwa_serialization(dwa: DataWithAxes) → bytes[source]

Convert a DataWithAxes into a bytes string

Parameters:

dwa (DataWithAxes) –

Returns:

bytes

Return type:

the total bytes message to serialize the DataWithAxes

Notes

The bytes sequence is constructed as:

  • serialize the string type: ‘DataWithAxes’

  • serialize the timestamp: float

  • serialize the name

  • serialize the units

  • serialize the source enum as a string

  • serialize the dim enum as a string

  • serialize the distribution enum as a string

  • serialize the list of numpy arrays

  • serialize the list of labels

  • serialize the origin

  • serialize the nav_index tuple as a list of int

  • serialize the list of axis

  • serialize the errors attributes (None or list(np.ndarray))

  • serialize the list of names of extra attributes

  • serialize the extra attributes

static int_to_bytes(an_integer: int) → bytes[source]

Convert an unsigned integer into a byte array of length 4 in big endian

Parameters:

an_integer (int) –

Return type:

bytearray

list_serialization(list_object: List) → bytes[source]

Convert a list of objects into a bytes message together with the info to convert it back

Parameters:

list_object (list) – the list could contains either scalars, strings or ndarrays or Axis objects or DataWithAxis objects module

Returns:

bytes

Return type:

the total bytes message to serialize the list of objects

Notes

The bytes sequence is constructed as: * the length of the list

Then for each object:

  • get data type as a string

  • use the serialization method adapted to each object in the list

ndarray_serialization(array: ndarray) → bytes[source]

Convert a ndarray into a bytes message together with the info to convert it back

Parameters:

array (np.ndarray) –

Returns:

bytes

Return type:

the total bytes message to serialize the scalar

Notes

The bytes sequence is constructed as:

  • get data type as a string

  • reshape array as 1D array and get the array dimensionality (len of array’s shape)

  • convert Data array as bytes

  • serialize data type

  • serialize data length

  • serialize data shape length

  • serialize all values of the shape as integers converted to bytes

  • serialize array as bytes

object_type_serialization(obj: Axis | DataToExport | DataWithAxes) → bytes[source]

Convert an object type into a bytes message as a string together with the info to convert it back

Applies to Data object from the pymodaq.utils.data module

scalar_serialization(scalar: complex) → bytes[source]

Convert a scalar into a bytes message together with the info to convert it back

Parameters:

scalar (A python number (complex or subtypes like float and int)) –

Returns:

bytes

Return type:

the total bytes message to serialize the scalar

classmethod str_len_to_bytes(message: str | bytes) → Tuple[bytes, bytes][source]

Convert a string and its length to two bytes :param message: the message to convert :type message: str

Returns:

  • bytes (message converted as a byte array)

  • bytes (length of the message byte array, itself as a byte array of length 4)

string_serialization(string: str) → bytes[source]

Convert a string into a bytes message together with the info to convert it back

Parameters:

string (str) –

Returns:

bytes

Return type:

the total bytes message to serialize the string

to_bytes()[source]

Generic method to obtain the bytes string from various objects

Compatible objects are:

  • bytes

  • numbers.Number

  • str

  • numpy.ndarray

  • Axis

  • DataWithAxes and sub-flavours

  • DataToExport

  • list of any objects above

type_and_object_serialization(obj: bool | bytes | str | complex | float | int | list | ndarray | Axis | DataWithAxes | DataToExport | ParameterWithPath | None = None) → bytes[source]

Serialize an object with its type, such that it can be retrieved by DeSerializer.type_and_object_deserialization.

class pymodaq.utils.tcp_ip.serializer.SocketString(bytes_string: bytes)[source]

Mimic the Socket object but actually using a bytes string not a socket connection

Implements a minimal interface of two methods

Parameters:

bytes_string (bytes) –

See also

Socket

Methods

check_received_length(length)

Make sure all bytes (length) that should be received are received through the socket.

get_first_nbytes(length)

Read the first N bytes from the socket

check_received_length(length: int) → bytes[source]

Make sure all bytes (length) that should be received are received through the socket.

Here just read the content of the underlying bytes string

Parameters:

length (int) – The number of bytes to be read from the socket

Return type:

bytes

get_first_nbytes(length: int) → bytes[source]

Read the first N bytes from the socket

Parameters:

length (int) – The number of bytes to be read from the socket

Returns:

the read bytes string

Return type:

bytes

7.4.2.2. Custom Sockets to implement PyMoDAQ protocol

Created the 26/10/2023

@author: Sebastien Weber

class pymodaq.utils.tcp_ip.mysocket.Socket(socket: socket | None = None)[source]

Custom Socket wrapping the built-in one and added functionalities to make sure message have been sent and received entirely

Attributes:
socket

Methods

check_received_length(length)

Make sure all bytes (length) that should be received are received through the socket

check_sended(data_bytes)

Make sure all bytes are sent through the socket :param data_bytes: :type data_bytes: bytes

check_sended_with_serializer(obj)

Convenience function to convert permitted objects to bytes and then use the check_sended method

get_first_nbytes(length)

Read the first N bytes from the socket

accept

bind

close

connect

getsockname

listen

recv

send

sendall

check_received_length(length) → bytes[source]

Make sure all bytes (length) that should be received are received through the socket

Parameters:

length (int) – The number of bytes to be read from the socket

Return type:

bytes

check_sended(data_bytes: bytes)[source]

Make sure all bytes are sent through the socket :param data_bytes: :type data_bytes: bytes

check_sended_with_serializer(obj: object)[source]

Convenience function to convert permitted objects to bytes and then use the check_sended method

For a list of allowed objects, see Serializer.to_bytes()

get_first_nbytes(length: int) → bytes[source]

Read the first N bytes from the socket

Parameters:

length (int) – The number of bytes to be read from the socket

Returns:

bytes

Return type:

the read bytes string

7.4.2.3. Base classes as TCP server and client

Created on Fri Aug 30 12:21:56 2019

@author: Weber

class pymodaq.utils.tcp_ip.tcp_server_client.Grabber(parent: QObject | None = None)[source]

Methods

grab_data()

Do a grab session using 2 profile :

command_tcpip

connect_tcp_ip

process_tcpip_cmds

snapshot

grab_data()[source]
Do a grab session using 2 profile :
  • if grab pb checked do a continous save and send an “update_channels” thread command and a “grab” too.

  • if not send a “stop_grab” thread command with settings “main settings-naverage” node value as an attribute.

See also

daq_utils.ThreadCommand, set_enabled_Ini_buttons

class pymodaq.utils.tcp_ip.tcp_server_client.MockServer(client_type='GRABBER')[source]
class pymodaq.utils.tcp_ip.tcp_server_client.TCPClient(ipaddress='192.168.1.62', port=6341, params_state=None, client_type='GRABBER')[source]

PyQt5 object initializing a TCP socket client. Can be used by any module but is a builtin functionality of all actuators and detectors of PyMoDAQ

The module should init TCPClient, move it in a thread and communicate with it using a custom signal connected to TCPClient.queue_command slot. The module should also connect TCPClient.cmd_signal to one of its methods inorder to get info/data back from the client

The client itself communicate with a TCP server, it is best to use a server object subclassing the TCPServer class defined within this python module

Parameters:

params_state ((dict) state of the Parameter settings of the module instantiating this client and wishing to) – export its settings to the server. Obtained from param.saveState() where param is an instance of Parameter object, see pyqtgraph.parametertree::Parameter

Methods

get_data(message)

param message:

post_init([extra_commands])

To implement in a real object implementation

queue_command([command])

when this TCPClient object is within a thread, the corresponding module communicate with it with signal and slots from module to client: module_signal to queue_command slot from client to module: self.cmd_signal to a module slot

ready_to_read()

Do stuff (like read data) when messages arrive through the socket

ready_to_write()

Send stuff into the socket

ready_with_error()

Error in the socket communication

cmd_signal

data_ready

not_connected

process_error_in_polling

send_data

send_info_string

send_infos_xml

get_data(message: str)[source]
Parameters:

message –

post_init(extra_commands=[])[source]

To implement in a real object implementation

queue_command(command=<class 'pymodaq.utils.daq_utils.ThreadCommand'>)[source]

when this TCPClient object is within a thread, the corresponding module communicate with it with signal and slots from module to client: module_signal to queue_command slot from client to module: self.cmd_signal to a module slot

ready_to_read()[source]

Do stuff (like read data) when messages arrive through the socket

ready_to_write()[source]

Send stuff into the socket

ready_with_error()[source]

Error in the socket communication

class pymodaq.utils.tcp_ip.tcp_server_client.TCPServer(client_type='GRABBER')[source]

Abstract class to be used as inherited by DAQ_Viewer_TCP or DAQ_Move_TCP

Methods

close_server()

close the current opened server.

find_socket_type_within_connected_clients(sock)

Find a socket type from a connected client with socket content corresponding.

find_socket_within_connected_clients(client_type)

Find a socket from a connected client with socket type corresponding.

listen_client()

Server function.

print_status(status)

Print the given status.

process_cmds(command[, command_sock])

Process the given command.

read_info([sock, test_info, test_value])

if the client is not from PyMoDAQ it can use this method to display some info into the server widget

select(rlist[, wlist, xlist, timeout])

Implements the select method, https://docs.python.org/3/library/select.html :param rlist: :type rlist: (list) wait until ready for reading :param wlist: :type wlist: (list) wait until ready for writing :param xlist: :type xlist: (list) wait for an “exceptional condition” :param timeout: When the timeout argument is omitted the function blocks until at least one file descriptor is ready. A time-out value of zero specifies a poll and never blocks. :type timeout: (float) optional timeout argument specifies a time-out as a floating point number in seconds.

send_command(sock[, command])

Send one of the message contained in self.message_list toward a socket with identity socket_type.

timerEvent(event)

Called by set timers.

command_done

command_to_from_client

emit_status

init_server

read_data

read_info_xml

read_infos

remove_client

send_data

set_connected_clients_table

close_server()[source]

close the current opened server. Update the settings tree consequently.

See also

set_connected_clients_table, daq_utils.ThreadCommand

find_socket_type_within_connected_clients(sock)[source]

Find a socket type from a connected client with socket content corresponding.

Parameters

Type

Description

sock

???

The socket content corresponding.

Returns:

the socket dictionnary

Return type:

dictionnary

find_socket_within_connected_clients(client_type) → Socket[source]

Find a socket from a connected client with socket type corresponding.

Parameters

Type

Description

client_type

string

The corresponding client type

Returns:

the socket dictionnary

Return type:

dictionnary

listen_client()[source]

Server function. Used to connect or listen incoming message from a client.

print_status(status)[source]

Print the given status.

Parameters

Type

Description

status

string list

a string list representing the status socket

process_cmds(command, command_sock=None)[source]

Process the given command.

read_info(sock: Socket | None = None, test_info='an_info', test_value='')[source]

if the client is not from PyMoDAQ it can use this method to display some info into the server widget

select(rlist, wlist=[], xlist=[], timeout=0)[source]

Implements the select method, https://docs.python.org/3/library/select.html :param rlist: :type rlist: (list) wait until ready for reading :param wlist: :type wlist: (list) wait until ready for writing :param xlist: :type xlist: (list) wait for an “exceptional condition” :param timeout: When the timeout argument is omitted the function blocks until at least one file descriptor is ready.

A time-out value of zero specifies a poll and never blocks.

Returns:

  • list (readable sockets)

  • list (writable sockets)

  • list (sockets with error pending)

send_command(sock: Socket, command='move_at')[source]

Send one of the message contained in self.message_list toward a socket with identity socket_type. First send the length of the command with 4bytes.

Parameters

Type

Description

sock

???

The current socket

command

string

The command as a string

See also

utility_classes.DAQ_Viewer_base.emit_status, daq_utils.ThreadCommand, message_to_bytes

set_connected_clients_table()[source]
timerEvent(event)[source]

Called by set timers. If the process is free, start the listen_client function.

Parameters

Type

Description

event

QTimerEvent object

Containing id from timer issuing this event

See also

listen_client

Previous Next

© Copyright 2025, Weber Sébastien.

Built with Sphinx using a theme provided by Read the Docs.