Skip to main content
Skip table of contents

Integrated example - Tcp/Server

The example assumes that the client connects to the controller with IP = 192,168,137.100 and port = 20002

and that the received packets are sent to the server as they are (mirroring).

Example 1 : Default TCP server example

PY
g_sock = server_socket_open(20002)
tp_popup("connect O.K!",DR_PM_MESSAGE)
 
while 1:
 server_socket_write(g_sock, b"abcd") # The string "abcd" is sent in a byte type.
 wait(0.1)   
 res, rx_data = server_socket_read(g_sock) # Waits for the data from the server.
 tp_popup("res={0}, rx_data ={1}".format(res, rx_data), DR_PM_MESSAGE) 
 wait(0.1)

The example opens the port 20002 and waits until the client connects.

It connects to the client and sends the string "abcd".

The message received from the client is output to the TP.

res = 4 and rx_data=b"abcd" since the client transmits the received data as is.

Example 2 : Examples of a packet transfer

Transmission packet: “MEAS_START" +data1[4byte]+data2[4byte]

           data1: Conversion of the integer to 4 byte. ex) 1 → 00000001

           data2: Conversion of the integer to 4 byte. ex) 2 → 00000002

ex) data1=1 and data2=2: “MEAS_START"+00000001+00000002        

                 Actual packet: 4D4541535F53544152540000000100000002

Received packet: res=18, rx_data=“MEAS_START"+00000001+00000002

         rxd1 extraction: Conversion of 10th - 14th bytes to an integer

         rxd2 extraction: Conversion of 14th - 18th bytes to an integer

PY
g_sock = server_socket_open(20002)
tp_popup("connect O.K!",DR_PM_MESSAGE)
 
send_data = b"MEAS_START"
data1 =1
data2 =2 
send_data += (data1).to_bytes(4, byteorder='big') 
send_data += (data2).to_bytes(4, byteorder='big') 
  
server_socket_write(g_sock, send_data) 
 
wait(0.1)   
 
res, rx_data = server_socket_read(g_sock)
tp_popup("res={0}, rx_data ={1}".format(res, rx_data), DR_PM_MESSAGE) 
 
rxd1 = int.from_bytes(rx_data[10:10+4], byteorder='big', signed=True)
rxd2 = int.from_bytes(rx_data[14:14+4], byteorder='big', signed=True)
 
tp_popup("res={0}, rxd1={1}, rxd2={2}".format(res, rxd1, rxd2), DR_PM_MESSAGE) 

server_socket_close(g_sock)

The example sends the byte type send_data.

res = 18 and rx_data=send_data since the client transmits the received data as is.

rxd1 extraction: Conversion of 10th - 14th bytes to an integer

rxd2 extraction: Conversion of 14th - 18th bytes to an integer

The final result is res=18, rxd1=1, and rxd2=2.

Example 3 : Reconnection

PY
def fn_reopen():
 global g_sock
 server_socket_close(g_sock)
 g_sock = server_socket_open(20002)
 return   
 
g_sock = server_socket_open(20002)
tp_popup("connect O.K!",DR_PM_MESSAGE)
 
server_socket_write(g_sock, b"abcd") 
wait(0.1)   
 
while 1:   
 res, rx_data = server_socket_read(g_sock)
 if res < 0:
     fn_reopen()
 else:     
     tp_popup("res={0}, rx_data ={1}".format(res, rx_data), DR_PM_MESSAGE) 
 wait(0.1)

The example checks the return value of the server_socket_read() command.

A negative value is returned if the connection to the client is terminated or there is a communication problem.

The function reopen() is called to wait for the client connection if a negative value is returned.

Note that the opened socket is closed when a reconnection is attempted.


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.