【ECEN067】EC simple sdk template

Please refer to Elite EC sdk manual to add commands as need.

For example by replacing the "" part, we could modify the command in suc,result,id=sendCMD(sock,"syncMotorStatus")

Please be aware that robot need to be under remote mode and the quotation mark need to be the English one.

from math import sqrt
import socket
import json
import time

def connectETController(ip,port=8055):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.connect((ip,port))
        return (True,sock)
    except Exception as e:
        sock.close()
        return (False,)

def sendCMD(sock,cmd,params=None,id=1):
    if(not params):
        params=[]
    else:
        params=json.dumps(params)
    sendStr="{{\"method\":\"{0}\",\"params\":{1} ,\"jsonrpc\":\"2.0\",\"id\":{2}}}".format(cmd,params,id)+"\n"
    
    try:
        sock.sendall(bytes(sendStr,"utf-8"))
        ret=sock.recv(1024)
        jdata=json.loads(str(ret,"utf-8"))
        if("result" in jdata.keys()):
            return (True,json.loads(jdata["result"]),jdata["id"])
        elif("error" in jdata.keys()):
            return (False,jdata["error"],jdata["id"])
        else:
            return (False,None,None)
    except Exception as e:
        return (False,None,None)

if __name__ == "__main__":
    robot_ip="192.168.1.200"
    conSuc,sock=connectETController(robot_ip)
    if(conSuc):
        suc,result,id=sendCMD(sock,"syncMotorStatus")
        print("Motor Synchoronization is", result)
        time.sleep(0.1)
    
def disconnectETController(sock):
    if (sock):
        sock.close()
        sock = None
    else:
        sock = None
点击显示全文
赞同0
发表评论
分享

手机扫码分享
0
76
收藏
举报
收起
登录
  • 密码登录
  • 验证码登录
还没有账号,立即注册
还没有账号,立即注册
注册
已有账号,立即登录
选择发帖板块
举报
请选择举报理由
举报
举报说明