Asked
Resolved Resolved by DJ Sures!

Running Pycharmt To Connect To EZB And Call A Script?

How to write python on pyCharm to connect to EZM using socket and then run a command, voice, script etc.?

I do not get any errors, just nothing happens lol

import socket

Define ARC system's IP address and port

host = '192.168.1.22' # Replace with the ARC system's IP address port = 23 # Replace with the ARC system's port (usually 23)

try: # Create a socket connection arc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) arc_socket.connect((host, port))

# Specify the path to the ARC script
arc_script_path = "C:\\Users\\your_username\\Documents\\ARC\\My Projects\\test.ezb"  # Replace with the correct path

# Send the ARC script execution command
arc_script = f"RunScript({arc_script_path})"
arc_socket.send(arc_script.encode())

# You may not receive a response immediately, but you can add a delay or loop to check for a response if needed

# Close the socket connection
arc_socket.close()

except socket.error as e: print(f"Socket error: {e}") except Exception as e: print(f"An error occurred: {e}")


Related Hardware EZ-B v3

ARC Pro

Upgrade to ARC Pro

Subscribe to ARC Pro, and your robot will become a canvas for your imagination, limited only by your creativity.

PRO
Synthiam
#1  

I’m sorry but I can’t quite understand what you’re asking. Can you provide information as to what it is you’re trying to do?

#2  

Thanks DJ Sures... i use PyCharm to create applications, for there you can do lots of importing of packages and call APIs... but on the ARC side you can't (unless I don't know how to import new modules) so I was wondering if I can run my Python script from PyCharm and have it connect to my EZ Robot and run scripts it finds there, this way I can have both systems talk to each other...

PRO
Synthiam
#3  

You have a python program for a robot that uses some framework for interacting with hardware. You wish to integrate that existing python program with ARC.

Because I do not know what your program is or what framework you're using in python, I cannot guess on how to integrate the two. What I can recommend is persueing one of these two options...

  1. Convert your existing python program into a library and add it to arc. The My Documents\ARC\Python Modules folder contains user added python scripts. Documented here: https://synthiam.com/Support/python-api/python-overview

  2. Enable the connection control's TCP server and interact with hardware via that. https://synthiam.com/Support/Skills/Communication/EZ-B-Connection?id=16041

There's a few other robot skills that allow connecting to third party stuff for servos and camera. Specifically the unity robot skill. You can look into those as well.

#4  

thanks, i will check that out

PRO
Synthiam
#5   — Edited

If you enable the TCP Server for connection #0 in the connection control robot skill, you can run this in Python...

User-inserted image

PS, this will also run in an ARC python script control because ARC runs Python within itself.

This Python code establishes a network connection to ARC TCP Server, sends a command, receives a response, and then closes the connection. Here's a breakdown of what the code does step by step:

  1. Import the socket module: This code begins by importing the Python socket module, which provides functions and classes for working with network sockets.

  2. Define server IP, server port, and command: The IP address of the server you want to connect to is defined as '127.0.0.1' (localhost), and the server's port number is defined as 6666. Additionally, a command string is defined as "SayEZB("I am connected")\n". This EZ-Script command is intended to be sent to the ARC Telnet Server and it will be executed. It will speak out of the connected EZB with the phrase.

  3. Create a socket: A socket is created using socket.socket(socket.AF_INET, socket.SOCK_STREAM). This line initializes a TCP socket for communication with the specified IP address and port.

  4. Try to establish a connection to the server:

    • sock.connect((server_ip, server_port)): This line attempts to establish a connection to the server using the IP address and port specified earlier.
  5. Receive and print the server's version:

    • version is initialized as an empty bytes object.
    • The code enters a loop that receives data from the server in chunks of up to 1024 bytes.
    • It appends the received data to the version variable.
    • It checks for the presence of a newline character (\n) in the received data. If found, it breaks out of the loop.
    • Finally, it prints the version information received from the server.
  6. Send a command to the server:

    • sock.sendall(command.encode()): This line sends the command to the server after encoding it to bytes. This is necessary because network communication typically works with bytes, not strings.
  7. Receive and print the server's response:

    • response is initialized as an empty bytes object.
    • Similar to the version receiving step, it enters a loop to receive and append data from the server.
    • It checks for the newline character in the received data and breaks out of the loop when found.
    • Finally, it prints the response received from the server.
  8. Close the socket: The code uses a finally block to ensure that the socket is closed, regardless of whether the operations succeed or fail. This is good practice to release resources properly.

In summary, this code establishes a connection to the ARC TCP Server, sends an EZ-Script command, receives a response, and closes the connection. It's a basic example of performing client-server TCP Server communication using sockets in Python to Synthiam ARC.


import socket

# ARC TCP Server IP
server_ip = '127.0.0.1'

# ARC TCP Server Port
server_port = 6666

# The EZScript command to send to ARC. This example will speak out of the connected EZB
command = "SayEZB(\"I am connected\")\n"

# Create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Connect to the server
    sock.connect((server_ip, server_port))

    # Receive the version and print the response
     # When first connecting to ARC TCP Server, it will print the version information before a command can be sent
    version = b''
    while True:
        data = sock.recv(1024)
        if not data:
            break
        version += data
        if b'\n' in data:
            break
    print("Version:", version.decode())


    # Send the command to ARC
    sock.sendall(command.encode())

    # Receive and print the response
    response = b''
    while True:
        data = sock.recv(1024)
        if not data:
            break
        response += data
        if b'\n' in data:
            break
    print("Received response:", response.decode())

finally:
    # Close the socket
    sock.close()

User-inserted image

d

Lastly, you can test the TCP Server by simply telneting to 127.0.0.1 port 6666 and typing commands with the keyboard. This will familiarize you with the syntax. All TCP Server commands are responded with a "True" or their console output. So moving a servo Servo(D0, 2) will result in a response of True. But, sending Print("Hello") will result in "Hello."

PRO
Synthiam
#6   — Edited

PS, there's a python module that you can use in your existing python program that connects to ARC via the tcp server. Find it here..

https://synthiam.com/Support/python-api/add-custom-python-modules

Name this ARCClient.py


import socket

class ARCClient:
    def __init__(self, server_ip, server_port):
        self.server_ip = server_ip
        self.server_port = server_port
        self.sock = None
        self.version = "unknown"

    def connect(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            self.sock.connect((self.server_ip, self.server_port))

            self.version = self._receive_response()
            print("Version:", self.version.decode())

        except Exception as e:
            print("Connection error:", str(e))

    def send_command(self, command):
        if self.sock:
            self.sock.sendall(command.encode())
            response = self._receive_response()
            return response.decode()

    def close(self):
        if self.sock:
            self.sock.close()
            self.sock = None

    def _receive_response(self):
        response = b''
        while True:
            data = self.sock.recv(1024)
            if not data:
                break
            response += data
            if b'\n' in data:
                break
        return response

    def version(self):
        return self.version
        
if __name__ == "__main__":
    server_ip = '127.0.0.1'
    server_port = 6666
    command = "SayEZB(\"I am connected\")\n"

    arc_client = ARCClient(server_ip, server_port)
    arc_client.connect()
    response = arc_client.send_command(command)
    print("Received response:", response)
    arc_client.close()

Here's how you can use it...


from ARCClient import ARCClient

server_ip = '127.0.0.1'
server_port = 6666
command = "SayEZB(\"I am connected\")\n"

arc_client = ARCClient(server_ip, server_port)
arc_client.connect()
response = arc_client.send_command(command)
print("Received response:", response)
arc_client.close()

#8  

that is amazing and it all worked like a charm esp the sample you sent... when i sway it out with a command = "controlCommand('Scripts', 'ScriptStart', 'Date')" it either says ControlCommand not defined or it just sits with a terminal prompt...

PRO
Synthiam
#9   — Edited

Control command requires double quotes

you escape a double quote with a \ backslash

#10  

You are brilliant.... and I figured it out thank you... this is what i have thus far... if there is a cleaner way let me know. this will now move the head of the robot, speak in it's voice, get information from my PC, pick a random bible verse, speak it and then move the head back to it's original place

from ARCClient import ARCClient import time import bible

server_ip = '127.0.0.1' server_port = 6666

command = 'SayEZB("Good day Terry, here is your bible quote for today")\n'

command1 = 'ServoSpeed(d0, 1)\n' command2 = 'Servo(d0, 1)\n' command3 = 'controlCommand("BingChat", "Track_1")\n'

vline = bible.line verse = bible.get_bible(vline) quoted_output = f'"{verse}"' quoted_output = quoted_output.replace('\n', ' ').replace('\r', '') print(quoted_output)

command4 = f'SayEZB({quoted_output})\n' command5 = 'Servo(d0, 180)\n'

arc_client = ARCClient(server_ip, server_port) arc_client.connect() response = arc_client.send_command(command1) response = arc_client.send_command(command2) response = arc_client.send_command(command3) time.sleep(3.5) response = arc_client.send_command(command4) time.sleep(3.5) response = arc_client.send_command(command5) print("Received response:", response) arc_client.close()