【ECEN003】How to Use Elibot SDK in C#


Introduction

User can use socket to send message in specified format to Port 8055 to control robot.

Message be sent to robot format: JSON(refer to Elibot SDK socket manual)

Elibot SDK socket manual download link: https://www.elibot.cn/upload/elibotEn/Download/20220210130149.pdf

Visual Studio Environment Configuration

It will be useful to use Newtonsoft.Json to handle string in JSON format

User can add Newtonsoft.Json in NuGet Package Manager


Add References: System.Runtime.Serialization


Add Using


Example

Example 1 Connect to robot controller and read current joint position

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Text;
namespace json_Csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String IP = "192.168.1.200";
            int port = 8055;
            Json.connectETController(IP, port);
            //Get robot state
            Json.Json_str getState = Json.SendCMD("getRobotState");
            if (getState.result.ToString() == "4")
            {
                //clear alarm
                Json.Json_str clearAlarm = Json.SendCMD("clearAlarm");
            }
            Json.Json_str getMotorStatus = Json.SendCMD("getMotorStatus");
            if (getMotorStatus.result.ToString() == "false")
            {
                //Sync encoder data
                Json.Json_str set_servo_status = Json.SendCMD("syncMotorStatus");
            }
            //Get robot Cartesian position
            Json.Json_str getpose = Json.SendCMD("getRobotPose");
            Console.WriteLine("getpose:{0}", string.Join(",", getpose.robot_array));
            //Get robot joint position
            Json.Json_str getpos = Json.SendCMD("getRobotPos");
            Console.WriteLine("getpos:{0}", string.Join(",", getpos.robot_array));
        }
    }
    [DataContract]
class Json
    {
public static Socket ClientSocket;
[DataMember]
public string jsonrpc { get; set; }
[DataMember]
public string method { get; set; }
[DataMember]
public object param { get; set; }
[DataMember]
public int id { get; set; }
[DataMember]
public object error { get; set; }
[DataMember]
public object result { get; set; }
[DataMember]
public double[] robot_array { get; set; }
public static bool connectETController(string IP, int port)
{
try
{
IPAddress ip = IPAddress.Parse(IP); //transfer IP string to IPAddress

ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(ip, port); 
ClientSocket.Connect(endPoint);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return false;
throw;
}
}
public static Json_str SendCMD(string cmd, object param = null)
{
Json_str descJson = null;
    ArrayList d_array = new ArrayList();
string rec = "";
try
{
List<Json> send_json = new List<Json>(){
new Json(){jsonrpc="2.0",method=cmd,param=param,id=1 } };
string jsonData = JsonConvert.SerializeObject(send_json);
jsonData = jsonData.Substring(1, jsonData.Length - 2);
jsonData = jsonData.Replace("param", "params");
jsonData = jsonData.Replace("null", "[]");
jsonData += "\n";
byte[] message = Encoding.ASCII.GetBytes(jsonData);
    //transfer string to bytes array
ClientSocket.Send(message);
    //send byte array
byte[] receive = new byte[1024];
    //define receive data length
int length = ClientSocket.Receive(receive);
    // receive data length
rec = Encoding.ASCII.GetString(receive);
descJson = JsonConvert.DeserializeObject<Json_str>(rec);
if (descJson.result != null && descJson.result.ToString().IndexOf("["
) != -1)
{
string ret_string = descJson.result.ToString().Substring(descJson
.result.ToString().IndexOf("["), descJson.result.ToString().LastIndexOf("]"));
ret_string = ret_string.Replace("[", "");
ret_string = ret_string.Replace("]", "");
string[] s = ret_string.Split(',');
for (int i1 = 0; i1 < s.Length; i1++)
{
d_array.Add(Double.Parse(s[i1]));
}
descJson.robot_array = (double[])d_array.ToArray(typeof(double));
}
return descJson;
}
catch (Exception ex)
{
Console.Write(ex);
descJson = null;
return descJson;
}
}
public class Json_str
{
[DataMember]
    public string jsonrpc { get; set; }
[DataMember]
public object error { get; set; }
[DataMember]
public object result { get; set; }
[DataMember]
public int id { get; set; }
[DataMember]
pub
}   

Example 2 Make robot move

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.Text;
namespace json_Csharp
{
class Program
{
static void Main(string[] args)
{
String IP = "192.168.1.200";
int port = 8055;
Json.connectETController(IP, port);
//Get robot state
Json.Json_str getState = Json.SendCMD("getRobotState");
if ((string)getState.result == "4")
{
//clear alarm
Json.Json_str clearAlarm = Json.SendCMD("clearAlarm");
    }
Json.Json_str getMotorStatus = Json.SendCMD("getMotorStatus");
if ((string)getMotorStatus.result == "false")
{
//sync encoder data
Json.Json_str set_servo_status = Json.SendCMD("syncMotorStatus");
}
double[] ready = { -90,-90,90,-90,90,-90,0,0};
Json.Json_str move_J = Json.SendCMD("moveByJoint", new { targetPos =
ready, speed = 20, acc = 50, dec = 50 });
  // make robot move to ready joint position

getState = Json.SendCMD("getRobotState");
while ((string)getState.result!="0")
{
getState = Json.SendCMD("getRobotState");
}
//Get robot Cartesian current pose
Json.Json_str getpose = Json.SendCMD("getRobotPose");
Console.WriteLine("getpose:{0}", string.Join(",", getpose.robot_array
));
double[] run_pose = getpose.robot_array;
run_pose[0] = run_pose[0] + 100;
    // robot current pose[x] data +100
Json.Json_str run_pos = Json.SendCMD("inverseKinematic", new { target
Pose = run_pose });
Json.Json_str move_L = Json.SendCMD("moveByLine", new { targetPos = r
un_pos.robot_array, speed=100, acc=50,dec=50 });
//Get robot state
getState = Json.SendCMD("getRobotState");
while ((string)getState.result != "0")
{
getState = Json.SendCMD("getRobotState");
}
}
}
[DataContract]
class Json
{
public static Socket ClientSocket;
[DataMember]
public string jsonrpc { get; set; }
[DataMember]
public string method { get; set; }
[DataMember]
    public object param { get; set; }
[DataMember]
public int id { get; set; }
[DataMember]
public object error { get; set; }
[DataMember]
public object result { get; set; }
[DataMember]
public double[] robot_array { get; set; }
public static bool connectETController(string IP, int port)
{
try
{
IPAddress ip = IPAddress.Parse(IP); 
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.
Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(ip, port); 
ClientSocket.Connect(endPoint); 
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return false;
throw;
}
}
public static Json_str SendCMD(string cmd, object param = null)
{
Json_str descJson = null;
ArrayList d_array = new ArrayList();
string rec = "";
try
{
List<Json> send_json = new List<Json>(){
new Json(){jsonrpc="2.0",method=cmd,param=param,id=1 } };
string jsonData = JsonConvert.SerializeObject(send_json);
jsonData = jsonData.Substring(1, jsonData.Length - 2);
jsonData = jsonData.Replace("param", "params");
    jsonData = jsonData.Replace("null", "[]");
jsonData += "\n";
byte[] message = Encoding.ASCII.GetBytes(jsonData); 
ClientSocket.Send(message);
byte[] receive = new byte[1024];
int length = ClientSocket.Receive(receive); 
rec = Encoding.ASCII.GetString(receive);
descJson = JsonConvert.DeserializeObject<Json_str>(rec);
if (descJson.result != null && descJson.result.ToString().IndexOf
("[") != -1)
{
string ret_string = descJson.result.ToString().Substring(desc
Json.result.ToString().IndexOf("["), descJson.result.ToString().LastIndexOf("]"))
;
ret_string = ret_string.Replace("[", "");
ret_string = ret_string.Replace("]", "");
string[] s = ret_string.Split(',');
for (int i1 = 0; i1 < s.Length; i1++)
{
d_array.Add(Double.Parse(s[i1]));
}
descJson.robot_array = (double[])d_array.ToArray(typeof(doubl
e));
}
return descJson;
}
catch (Exception ex)
{
Console.Write(ex);
descJson = null;
return descJson;
}
}
public class Json_str
{
[DataMember]
public string jsonrpc { get; set; }
[DataMember]
public object error { get; set; }
[DataMember]
public object result { get; set; }
    [DataMember]
public int id { get; set; }
[DataMember]
public double[] robot_array { get; set; }
}
}
}
       
点击显示全文
赞同0
发表评论
分享

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