ER7BC10.cs
4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;
using System;
using System.Text;
namespace HslCommunication.Robot.EFORT
{
/// <summary>
/// 埃夫特机器人对应型号为ER7B-C10,此协议为定制版,使用前请测试
/// </summary>
public class ER7BC10 : NetworkDoubleBase<EFORTMessage, RegularByteTransform>, IRobotNet
{
#region Constructor
/// <summary>
/// 实例化一个默认的对象,并指定IP地址和端口号,端口号通常为8008
/// </summary>
/// <param name="ipAddress">Ip地址</param>
/// <param name="port">端口号</param>
public ER7BC10(string ipAddress, int port)
{
IpAddress = ipAddress;
Port = port;
softIncrementCount = new SoftIncrementCount(ushort.MaxValue);
}
#endregion
#region Request Create
/// <summary>
/// 获取发送的消息的命令
/// </summary>
/// <returns>字节数组命令</returns>
public byte[] GetReadCommand()
{
byte[] command = new byte[38];
Encoding.ASCII.GetBytes("MessageHead").CopyTo(command, 0);
BitConverter.GetBytes((ushort)command.Length).CopyTo(command, 16);
BitConverter.GetBytes((ushort)1001).CopyTo(command, 18);
BitConverter.GetBytes((ushort)softIncrementCount.GetCurrentValue()).CopyTo(command, 20);
Encoding.ASCII.GetBytes("MessageTail").CopyTo(command, 22);
return command;
}
#endregion
#region IRobotNet Support
/// <summary>
/// 读取埃夫特机器人的原始的字节数据信息,该地址参数是没有任何作用的,随便填什么
/// </summary>
/// <param name="address">无效参数</param>
/// <returns>带有成功标识的byte[]数组</returns>
public OperateResult<byte[]> Read(string address)
{
return ReadFromCoreServer(GetReadCommand());
}
/// <summary>
/// 读取机器人的所有的数据信息,返回JSON格式的数据对象,地址参数无效
/// </summary>
/// <param name="address">地址信息</param>
/// <returns>带有成功标识的字符串数据</returns>
public OperateResult<string> ReadString(string address)
{
OperateResult<EfortData> read = ReadEfortData();
if (!read.IsSuccess) return OperateResult.CreateFailedResult<string>(read);
return OperateResult.CreateSuccessResult(Newtonsoft.Json.JsonConvert.SerializeObject(read.Content, Newtonsoft.Json.Formatting.Indented));
}
/// <summary>
/// 本机器人不支持该方法操作,将永远返回失败,无效的操作
/// </summary>
/// <param name="address">指定的地址信息,有些机器人可能不支持</param>
/// <param name="value">原始的字节数据信息</param>
/// <returns>是否成功的写入</returns>
public OperateResult Write(string address, byte[] value)
{
return new OperateResult(StringResources.Language.NotSupportedFunction);
}
/// <summary>
/// 本机器人不支持该方法操作,将永远返回失败,无效的操作
/// </summary>
/// <param name="address">指定的地址信息,有些机器人可能不支持</param>
/// <param name="value">字符串的数据信息</param>
/// <returns>是否成功的写入</returns>
public OperateResult Write(string address, string value)
{
return new OperateResult(StringResources.Language.NotSupportedFunction);
}
/// <summary>
/// 读取机器人的详细信息,返回解析后的数据类型
/// </summary>
/// <returns>结果数据信息</returns>
public OperateResult<EfortData> ReadEfortData()
{
OperateResult<byte[]> read = Read("");
if (!read.IsSuccess) return OperateResult.CreateFailedResult<EfortData>(read);
return EfortData.PraseFrom(read.Content);
}
#endregion
#region Private Member
private SoftIncrementCount softIncrementCount; // 自增消息的对象
#endregion
#region Object Override
/// <summary>
/// 返回表示当前对象的字符串
/// </summary>
/// <returns>字符串</returns>
public override string ToString()
{
return $"ER7BC10 Robot[{IpAddress}:{Port}]";
}
#endregion
}
}