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
Methods
Convert bytes into an Axis object
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
Convert bytes into a DataToExport object
Convert bytes into a DataWithAxes object
Convert bytes into a list of homogeneous objects
Convert bytes into a numpy ndarray object
Convert bytes into a numbers.Number object
Convert bytes into a str object
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_nd_array(data: bytes, dtype: dtype, shape: Tuple[int]) → ndarray[source]
Convert bytes to a ndarray given a certain numpy dtype and shape
- static bytes_to_scalar(data: bytes, dtype: dtype) → Number[source]
Convert bytes to a scalar given a certain numpy dtype
- Parameters:
data (bytes) –
dtype (np.dtype) –
- Return type:
- 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() → Number[source]
Convert bytes into a numbers.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
- class pymodaq.utils.tcp_ip.serializer.Serializer(obj: int | str | Number | list | ndarray | Axis | DataWithAxes | DataToExport | 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
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
bytes_serialization
str_to_bytes
to_b64_string
type_and_object_serialization
- 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
- 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: Number) → bytes[source]
Convert a scalar into a bytes message together with the info to convert it back
- Parameters:
scalar (str) –
- Returns:
bytes
- Return type:
the total bytes message to serialize the scalar
- classmethod str_len_to_bytes(message: str | bytes) -> (<class 'bytes'>, <class '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
- 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
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
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
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
- check_sended(data_bytes: bytes)[source]
Make sure all bytes are sent through the socket :param data_bytes: :type data_bytes: bytes
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
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.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
Do stuff (like read data) when messages arrive through the socket
Send stuff into the socket
Error in the socket communication
cmd_signal
data_ready
not_connected
process_error_in_polling
send_data
send_info_string
send_infos_xml
- 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
- 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 the current opened server.
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.
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
- print_status(status)[source]
Print the given status.
Parameters
Type
Description
status
string list
a string list representing the status socket
- 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