TCP Script Server Raw

Control Thumbnail

How To Download and Use This Control

  1. Make sure you have the latest version of EZ-Builder installed.
  2. Select the Install button below to download the file.
  3. Double click the downloaded file to open installer.
  4. The installer will add this control to EZ-Builder.

This TCP server will bind to a port and listen for incoming EZ-Script commands. This is a useful plugin for connecting third part controllers and products to EZ-Builder. You may send any EZ-Script command, terminated by a new line (\r\n). The response is received which is also terminated by a new line (\r\n).

User-inserted image


Control Command
There are also ControlCommand() for starting, stopping and controlling debug settings. This is useful if you wish to control this plugin from another script in the project, or when a connection is established to the EZ-B.

Be Socket Friendly
Although you can establish a new connection for each request, it is recommended that you maintain one connection and reuse it throughout your program. This will use the nature of the TCP (transmission controlled protocol). Every time a TCP connection is established, a bunch of back and forth occurs, which takes time and resources. If you reuse the connection, your program will run much faster. If your program is using multi-threads and therefore executes commands asynchronously, we recommend using a TCP connection per thread.

New Line Terminated Response
Every command that you send will return a new line terminated response, even if it is an empty response. Empty new line terminated responses would be for commands that do not return anything, such as SetServo(). This means it is important for your program to read the response of every command to ensure it is always caught up with the input stream.

Python
To send and receive from python...

Code:


import socket

# create an ipv4 (AF_INET) socket object using the tcp protocol (SOCK_STREAM)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect the client, in this case localhost
client.connect(('127.0.0.1', 8080))

# -------------------------------------------------------------------------------------------

# send some EZ-Script (in this case do some simple math)
client.send('print(23+65)\r\n')

# receive the response data
response = client.recv(2048)

print response

# -------------------------------------------------------------------------------------------

# send some more EZ-Script (in this case print a string)
client.send('print("Hello World!")\r\n')

# receive the response data
response = client.recv(2048)

print response

# -------------------------------------------------------------------------------------------

# send some more EZ-Script (in this case move a servo)
client.send('Servo(d0, 90)\r\n')

# receive the response data
response = client.recv(2048)

print response

# -------------------------------------------------------------------------------------------

# send some more EZ-Script (in this case get servo position)
client.send('GetServo(d0)\r\n')

# receive the response data
response = client.recv(2048)

print response

# -------------------------------------------------------------------------------------------

# close the tcp connection
client.close()