3.7.0 3.6.0 3.5.0 3.4.0 3.3.0 3.2.2 3.2.1 3.2.0 Korean English Chinese Czech Dutch French German Hungarian Italian Japanese Polish Portuguese Spanish
3.7.0 3.6.0 3.5.0 3.4.0 3.3.0 3.2.2 3.2.1 3.2.0 Korean English Chinese Czech Dutch French German Hungarian Italian Japanese Polish Portuguese Spanish

Integrated example (Tcp/Client)

Assume that server IP = 192.168.137,200 and open port =20002

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

Example 1 : Example of a default TCP client

Python
# Assume server IP = 192.168.137,200 and open port =20002. 
g_sock = client_socket_open("192.168.137.200", 20002)
 
tp_popup("connect O.K!",DR_PM_MESSAGE)
while 1: 
 client_socket_write(g_sock, b"abcd") # The string "abcd" is sent in a byte type.
 wait(0.1)   
 res, rx_data = client_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 connects to the server and sends the string "abcd". (b converts the string to a byte type.)

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

res = 4 and rx_data=b"abcd" since the server 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

Python
g_sock = client_socket_open("192.168.137.100", 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') 
  
client_socket_write(g_sock, send_data) 
 
wait(0.1)   

res, rx_data = client_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) 
 
client_socket_close(g_sock)

The example connects to the server and sends a byte type send_data.

res = 18 and rx_data=send_data since the server 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

Python
def fn_reconnect():
 global g_sock
 client_socket_close(g_sock)
 g_sock = client_socket_open("192.168.137.200", 20002)
 return   
 
g_sock = client_socket_open("192.168.137.200", 20002)
tp_popup("connect O.K!",DR_PM_MESSAGE)
 
client_socket_write(g_sock, b"abcd") 
wait(0.1)   
 
while 1:   
 res, rx_data = client_socket_read(g_sock)
 if res < 0:
     fn_reconnect()
 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 client_socket_read() command.

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

The function reconnect() is called to attempt a reconnection if a negative value is returned.

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