软件配置:Epson机械手软件,vs2012
网络配置:Tcp通信,IP地址及端口设置
硬件配置:Pc机,机械手,机械手供电器
一.环境准备
①pc,机械手,供电器上电。
②pc与机械手的两个网线插口连接,置供电器Power为On
③配置pc的ip地址与Epson软件同一个ip地址,Epson软件每一个端口都可以设置一个ip,可以同时开多个端口(设置位置:设置—系统配置—控制器—TCP/IP)
④窗口—电脑与控制器通信–增加,添加一个以太网连接p1,同时设置ip地址
⑤将软件状态从离线改为usb,在更改为名为p1的以太网连接。
⑥工具—机器人管理器,调试机械手,机械手运动则表示环境准备完成。
二. 编写Epson软件代码
①初始化Epson
Function MainInitPro
Reset
If Motor = Off Then
Motor On
SLock 1, 2, 3, 4, 5, 6
EndIf
Power High
Counts = 0
Accel 60, 60
Speed 10
AccelS 1000
SpeedS 1000
' Go P1
Fend
②网络配置
Function NetOpen
NetSts = ChkNet(202)
If (NetSts < 0) Then
CloseNet #202
Wait 0.5
SetNet #202, "192.168.0.1", 2002, CRLF, NONE, 0 '配置端口号,ip地址
OpenNet #202 As Server
WaitNet #202
Print "配置完成!"
EndIf
Fend
③扫描端口,获取端口数据,传输数据
Function ScanConnNet
OnErr GoTo errhandler
Do
NetSts = ChkNet(202)
If (NetSts > 0) Then
Input #202, RecvString$ '从202端口获取输入,并赋值到变量RecvString$
ParseStr RecvString$, RecvStr$(), "#"
ParseStr RecvString$, RecvStr$(), " "
Select RecvStr$(0)
Case "X"
Move CurPos +X(Val(RecvStr$(1)))
Case "-X"
Move CurPos -X(Val(RecvStr$(1)))
Case "RobotRun"
CX(P100) = Val(RecvStr$(1))
CY(P100) = Val(RecvStr$(2))
CZ(P100) = Val(RecvStr$(3))
CU(P100) = Val(RecvStr$(4))
CV(P100) = Val(RecvStr$(5))
CW(P100) = Val(RecvStr$(6))
Go LJM(P100)
'发送数据到202端口
Print #202, "#GoToPointok"
Send
ElseIf NetSts < 0 Then
Call NetOpen
ElseIf NetSts = 0 Then
EndIf
Print #202, "#RobotPos" + "," + Str$(CX(CurPos)) + "," + Str$(CY(CurPos)) + "," + Str$(CZ(CurPos)) + "," + Str$(CU(CurPos)) + "," + Str$(CV(CurPos)) + "," + Str$(CW(CurPos)) + "#"
Wait 0.2
Loop
errHandler:
RecvString$ = ""
EResume Next
Fend
三.c#编程
①Tcp连接核心代码
using System.Threading.Tasks;
using TestLTDMC;
using System.Threading;
using System.Windows.Forms;
namespace Test
{
class Robot
{
private Socket client;
public static object sendLock = new object();
private IPEndPoint ipep;
private byte[] buf = new byte[1024];
private static object SendLock = new object();
private static object Lock = new object();
public static Robot robot;
private bool RobotConnectSts = false;
public static Robot instance;
public static Robot Instance()
{
if (instance == null)
{
instance = new Robot();
}
return instance;
}
private Robot()
{
IPAddress ipad = IPAddress.Parse("192.168.0.1");
ipep = new IPEndPoint(ipad, 2001);
}
public void ConnectWithRobot()
{
if (client != null && client.Connected)
{
client.Close();
}
try
{
lock (sendLock)
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipep);
client.BeginReceive(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(Receive), client);
}
}
catch
{
}
}
public bool GetRobotConnectSts
{
get
{
if (client != null)
{
RobotConnectSts = client.Connected;
}
else
{
RobotConnectSts = false;
}
return RobotConnectSts;
}
}
public void DisconnectWithRobot()
{
if (client != null)
{
client.Close();
}
}
private void Receive(IAsyncResult ia)
{
try
{
client = ia.AsyncState as Socket;
int count = client.EndReceive(ia);
client.BeginReceive(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(Receive), client);
string context = Encoding.ASCII.GetString(buf, 0, count);
if (context.Length > 0)
{
ProcessData(context);
}
else
{
}
}
catch
{
}
}
private void ProcessData(string recvdata)
{
recvdata = recvdata.Trim();
string[] cmddata = recvdata.Split(new char[] { '#' });
//string[] singlecmddata1 = cmddata[1].Split(new char[] { '\r\n' });
string[] singlecmddata = cmddata[1].Split(new char[] { ',' });
if (singlecmddata[0].Trim() != "RobotPos")
{
RunLog.instance.SaveLog(1, "PC接收到Robot发来的去" + recvdata + "命令");
}
switch (singlecmddata[0].Trim())
{
case "RobotPos":
//相关处理
break;
default:
break;
}
}
public void SendCmdToRobot(string part1, double part2, string part3, string part4, string part5, string part6, string part7)
{
string sendcmd = "";
sendcmd = part1 + " " + part2 + " " + part3 + " " + part4 + " " + part5 + " " + part6 + " " + part7;
SendCmdData(sendcmd);
}
public void SendCmdData(string StrData)
{
RunLog.instance.SaveLog(1, "PC向Robot发送" + StrData + "命令");
lock (SendLock)
{
try
{
byte[] buf1;
buf1 = Encoding.ASCII.GetBytes(StrData + "\r\n");
client.Send(buf1, 0, buf1.Length, SocketFlags.None);
}
catch
{
}
}
}
}
}