socket_read_string(socket_name=’socket_0’, prefix =’’, suffix =’’, interpret_escape=’False’, timeout=2)

Reads all data from the socket and returns the data as a string.

Returns (for example) "reply from the server:\n Hello World". if there is a timeout or the reply is invalid, an empty string is returned (""). You can test if the string is empty with an if-statement.

Maxium length of received string including termination characters is limited to 1024 characters.

 

>>> if(string_from_server) :

>>>    popup("the string is not empty")

>>> end

The optional parameters "prefix" and "suffix", can be used to express what is extracted from the socket. The "prefix" specifies the start of the substring (message) extracted from the socket. The data up to the end of the "prefix" will be ignored and removed from the socket. The "suffix" specifies the end of the substring (message) extracted from the socket. Any remaining data on the socket, after the "suffix", will be preserved.

 

By using the "prefix" and "suffix" it is also possible send multiple string to the controller at once, because the suffix defines where the message ends. E.g. sending ">hello<>world<" and calling this script function with the prefix=">" and suffix="<".

Note that leading spaces in the prefix and suffix strings are ignored in the current software and may cause communication errors in future releases.

The optional parameter "interpret_escape" can be used to allow the use of escape sequences "\n", "\t" and "\r" as part of the prefix or suffix.

Parameters

socket_name: Name of socket (string)

prefix: Defines a prefix (string)

suffix: Defines a suffix (string)

interpret_escape: Enables the interpretation of escape sequences (bool)

timeout: The number of seconds until the read action times out (float). A timeout of 0 or negative number indicates that the function should not return until a read is completed.

Return Value

String

Example command: string_from_server = socket_read_string("socket_10",prefix=">",suffix="<")