1.初始化项目。保险起见bin目录完整提交 2.基于现场代码为调整过的初始项目(此次提交还未解决代码差异问题)
This commit is contained in:
@@ -0,0 +1,534 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Net;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
|
||||
namespace AGVSystem
|
||||
{
|
||||
class AGVClientCls
|
||||
{
|
||||
public string IPAdress;
|
||||
public bool connected = false;
|
||||
public Socket clientSocket;
|
||||
private IPEndPoint hostEndPoint;
|
||||
private Byte[] SendDataPro;
|
||||
private Byte[] RecvDataPro;
|
||||
private Byte[] SendData;
|
||||
private Byte[] RecvData;
|
||||
private Byte SendOrRecv;
|
||||
private AutoResetEvent autoConnectEvent = new AutoResetEvent(false);
|
||||
private SocketAsyncEventArgs lisnterSocketAsyncEventArgs;
|
||||
|
||||
public delegate void StartListeHandler();
|
||||
public event StartListeHandler StartListen;
|
||||
|
||||
public delegate void ReceiveMsgHandler(byte[] info);
|
||||
public event ReceiveMsgHandler OnMsgReceived;
|
||||
|
||||
private List<SocketAsyncEventArgs> s_lst = new List<SocketAsyncEventArgs>();
|
||||
|
||||
public AGVClientCls(string hostName, int port)
|
||||
{
|
||||
IPAdress = hostName;
|
||||
IPAddress[] hostAddresses = Dns.GetHostAddresses(hostName);
|
||||
this.hostEndPoint = new IPEndPoint(hostAddresses[hostAddresses.Length - 1], port);
|
||||
this.clientSocket = new Socket(this.hostEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接服务端
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private bool Connect()
|
||||
{
|
||||
using (SocketAsyncEventArgs args = new SocketAsyncEventArgs())
|
||||
{
|
||||
args.UserToken = this.clientSocket;
|
||||
args.RemoteEndPoint = this.hostEndPoint;
|
||||
args.Completed += new EventHandler<SocketAsyncEventArgs>(this.OnConnect);
|
||||
this.clientSocket.ConnectAsync(args);
|
||||
bool flag = autoConnectEvent.WaitOne(1000);
|
||||
//SocketError err = args.SocketError;
|
||||
if (this.connected)
|
||||
{
|
||||
this.lisnterSocketAsyncEventArgs = new SocketAsyncEventArgs();
|
||||
byte[] buffer = new byte[50];
|
||||
this.lisnterSocketAsyncEventArgs.UserToken = this.clientSocket;
|
||||
this.lisnterSocketAsyncEventArgs.SetBuffer(buffer, 0, buffer.Length);
|
||||
this.lisnterSocketAsyncEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(this.OnReceive);
|
||||
this.StartListen();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断有没有连接上
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnConnect(object sender, SocketAsyncEventArgs e)
|
||||
{
|
||||
this.connected = (e.SocketError == SocketError.Success);
|
||||
autoConnectEvent.Set();
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送
|
||||
/// </summary>
|
||||
/// <param name="mes"></param>
|
||||
public void Send(Byte[] mes)
|
||||
{
|
||||
if (this.connected)
|
||||
{
|
||||
EventHandler<SocketAsyncEventArgs> handler = null;
|
||||
byte[] buffer = mes;
|
||||
SocketAsyncEventArgs senderSocketAsyncEventArgs = null;
|
||||
lock (s_lst)
|
||||
{
|
||||
if (s_lst.Count > 0)
|
||||
{
|
||||
senderSocketAsyncEventArgs = s_lst[s_lst.Count - 1];
|
||||
s_lst.RemoveAt(s_lst.Count - 1);
|
||||
}
|
||||
}
|
||||
if (senderSocketAsyncEventArgs == null)
|
||||
{
|
||||
senderSocketAsyncEventArgs = new SocketAsyncEventArgs();
|
||||
senderSocketAsyncEventArgs.UserToken = this.clientSocket;
|
||||
senderSocketAsyncEventArgs.RemoteEndPoint = this.clientSocket.RemoteEndPoint;
|
||||
if (handler == null)
|
||||
{
|
||||
handler = delegate(object sender, SocketAsyncEventArgs _e)
|
||||
{
|
||||
lock (s_lst)
|
||||
{
|
||||
s_lst.Add(senderSocketAsyncEventArgs);
|
||||
}
|
||||
};
|
||||
}
|
||||
senderSocketAsyncEventArgs.Completed += handler;
|
||||
}
|
||||
senderSocketAsyncEventArgs.SetBuffer(buffer, 0, buffer.Length);
|
||||
this.clientSocket.SendAsync(senderSocketAsyncEventArgs);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.connected = false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 监听服务端
|
||||
/// </summary>
|
||||
public void Listen()
|
||||
{
|
||||
if (this.connected && this.clientSocket != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
(lisnterSocketAsyncEventArgs.UserToken as Socket).ReceiveAsync(lisnterSocketAsyncEventArgs);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private int Disconnect()
|
||||
{
|
||||
int res = 0;
|
||||
try
|
||||
{
|
||||
this.clientSocket.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
try
|
||||
{
|
||||
this.clientSocket.Close();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
this.connected = false;
|
||||
return res;
|
||||
}
|
||||
/// <summary>
|
||||
/// 数据接受
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnReceive(object sender, SocketAsyncEventArgs e)
|
||||
{
|
||||
if (e.BytesTransferred == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
this.clientSocket.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (this.clientSocket.Connected)
|
||||
{
|
||||
this.clientSocket.Close();
|
||||
}
|
||||
}
|
||||
byte[] info = new Byte[] { 0 };
|
||||
this.OnMsgReceived(info);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] buffer = new byte[e.BytesTransferred];
|
||||
for (int i = 0; i < e.BytesTransferred; i++)
|
||||
{
|
||||
buffer[i] = e.Buffer[i];
|
||||
}
|
||||
this.OnMsgReceived(buffer);
|
||||
Listen();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 接受完成
|
||||
/// </summary>
|
||||
/// <param name="info"></param>
|
||||
private void SimaticSocketClient_OnMsgReceived(byte[] info)
|
||||
{
|
||||
if (info[0] != 0)
|
||||
{
|
||||
if (this.SendOrRecv == 1)
|
||||
{
|
||||
this.SendDataPro = info;
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
else if (this.SendOrRecv == 2)
|
||||
{
|
||||
this.SendData = info;
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
else if (this.SendOrRecv == 3)
|
||||
{
|
||||
this.RecvDataPro = info;
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
else if (this.SendOrRecv == 4)
|
||||
{
|
||||
this.RecvData = info;
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.SendOrRecv == 1)
|
||||
{
|
||||
this.SendDataPro = new Byte[] { 0 };
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
else if (this.SendOrRecv == 2)
|
||||
{
|
||||
this.SendData = new Byte[] { 0 };
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
else if (this.SendOrRecv == 3)
|
||||
{
|
||||
this.RecvDataPro = new Byte[] { 0 };
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
else if (this.SendOrRecv == 4)
|
||||
{
|
||||
this.RecvData = new Byte[] { 0 };
|
||||
this.SendOrRecv = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 建立连接的方法
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool OpenLinkPLC()
|
||||
{
|
||||
bool flag = false;
|
||||
this.StartListen += new StartListeHandler(SimaticSocketClient_StartListen);
|
||||
this.OnMsgReceived += new ReceiveMsgHandler(SimaticSocketClient_OnMsgReceived);
|
||||
flag = this.Connect();
|
||||
if (!flag)
|
||||
{
|
||||
return flag;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭连接的方法
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int CloseLinkPLC()
|
||||
{
|
||||
return this.Disconnect();
|
||||
}
|
||||
/// <summary>
|
||||
/// 监听的方法
|
||||
/// </summary>
|
||||
private void SimaticSocketClient_StartListen()
|
||||
{
|
||||
this.Listen();
|
||||
}
|
||||
|
||||
#region 写入PLC数据(VW)
|
||||
/// <summary>
|
||||
/// 写一个VW数据
|
||||
/// </summary>
|
||||
/// <param name="paddr"></param>
|
||||
/// <param name="waddr"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public int WriteVM(int stationNO, int address, int value)
|
||||
{
|
||||
int flag = -1;
|
||||
Byte[] sendValue = new Byte[6];
|
||||
byte[] data = new byte[39];
|
||||
data[0] = 0x68;
|
||||
data[1] = 0x21;
|
||||
data[2] = 0x21;
|
||||
data[3] = 0x68;
|
||||
data[4] = (byte)stationNO;
|
||||
data[5] = 0x00;
|
||||
data[6] = 0x6C;
|
||||
data[7] = 0x32;
|
||||
data[8] = 0x01;
|
||||
data[9] = 0x00;
|
||||
data[10] = 0x00;
|
||||
data[11] = 0x00;
|
||||
data[12] = 0x00;
|
||||
data[13] = 0x00;
|
||||
data[14] = 0x0E;
|
||||
data[15] = 0x00;
|
||||
data[16] = 0x06;
|
||||
data[17] = 0x05;
|
||||
data[18] = 0x01;
|
||||
data[19] = 0x12;
|
||||
data[20] = 0x0A;
|
||||
data[21] = 0x10;
|
||||
data[22] = 0x04;
|
||||
data[23] = 0x00;
|
||||
data[24] = 0x01;
|
||||
data[25] = 0x00;
|
||||
data[26] = 0x01;
|
||||
data[27] = 0x84;
|
||||
data[28] = 0x00;
|
||||
data[29] = Convert.ToByte(address * 8 / 256);
|
||||
data[30] = Convert.ToByte(address * 8 % 256);
|
||||
data[31] = 0x00;
|
||||
data[32] = 0x04;
|
||||
data[33] = 0x00;
|
||||
data[34] = 0x10;
|
||||
data[35] = Convert.ToByte(value / 256);
|
||||
data[36] = Convert.ToByte(value % 256);
|
||||
int j = 0;
|
||||
for (int i = 4; i <= 36; i++)
|
||||
{
|
||||
j = j + data[i];
|
||||
}
|
||||
data[37] = Convert.ToByte(j % 256);
|
||||
data[38] = 0x16;
|
||||
sendValue[0] = 0x10;
|
||||
sendValue[1] = 0x02;
|
||||
sendValue[2] = 0x00;
|
||||
sendValue[3] = 0x5C;
|
||||
sendValue[4] = 0x5E;
|
||||
sendValue[5] = 0x16;
|
||||
Thread.Sleep(100);
|
||||
this.SendOrRecv = 1;
|
||||
int numPro = 0;
|
||||
this.Send(data);
|
||||
while (this.SendOrRecv != 0 && numPro < 500)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
numPro++;
|
||||
}
|
||||
if (numPro < 500)
|
||||
{
|
||||
if (this.SendDataPro[0] == 0xE5)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
this.SendOrRecv = 2;
|
||||
int num = 0;
|
||||
this.Send(sendValue);
|
||||
while (this.SendOrRecv != 0 && num < 500)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
num++;
|
||||
}
|
||||
if (num < 500)
|
||||
{
|
||||
if (this.SendData.Length == 24 && Check(this.SendData))
|
||||
{
|
||||
flag = 0;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.SendOrRecv = 0;
|
||||
return flag;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 读VW值
|
||||
/// <summary>
|
||||
/// 读值
|
||||
/// </summary>
|
||||
/// <param name="stationNO"></param>
|
||||
/// <param name="length"></param>
|
||||
/// <param name="address"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public int ReadVM(int stationNO, int length, int address, out int value)
|
||||
{
|
||||
int flag = -1;
|
||||
value = 0;
|
||||
Byte[] sendValue = new Byte[6];
|
||||
Byte[] data = new Byte[33];
|
||||
data[0] = 0x68;
|
||||
data[1] = 0x1B;
|
||||
data[2] = 0x1B;
|
||||
data[3] = 0x68;
|
||||
data[4] = (Byte)stationNO;
|
||||
data[5] = 0x00;
|
||||
data[6] = 0x6C;
|
||||
data[7] = 0x32;
|
||||
data[8] = 0x01;
|
||||
data[9] = 0x00;
|
||||
data[10] = 0x00;
|
||||
data[11] = 0x00;
|
||||
data[12] = 0x00;
|
||||
data[13] = 0x00;
|
||||
data[14] = 0x0E;
|
||||
data[15] = 0x00;
|
||||
data[16] = 0x00;
|
||||
data[17] = 0x04;
|
||||
data[18] = 0x01;
|
||||
data[19] = 0x12;
|
||||
data[20] = 0x0A;
|
||||
data[21] = 0x10;
|
||||
data[22] = 0x04;
|
||||
data[23] = 0x00;
|
||||
data[24] = Convert.ToByte(length);
|
||||
data[25] = 0x00;
|
||||
data[26] = 0x01;
|
||||
data[27] = 0x84;
|
||||
data[28] = 0x00;
|
||||
data[29] = Convert.ToByte(address * 8 / 256);
|
||||
data[30] = Convert.ToByte(address * 8 % 256);
|
||||
int j = 0;
|
||||
for (int i = 4; i <= 30; i++)
|
||||
{
|
||||
j = j + Convert.ToInt32(data[i]);
|
||||
}
|
||||
data[31] = Convert.ToByte(j % 256);
|
||||
data[32] = 0x16;
|
||||
sendValue[0] = 0x10;
|
||||
sendValue[1] = 0x02;
|
||||
sendValue[2] = 0x00;
|
||||
sendValue[3] = 0x5C;
|
||||
sendValue[4] = 0x5E;
|
||||
sendValue[5] = 0x16;
|
||||
Thread.Sleep(100);
|
||||
this.SendOrRecv = 3;
|
||||
int numPro = 0;
|
||||
this.Send(data);
|
||||
while (this.SendOrRecv != 0 && numPro < 500)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
numPro++;
|
||||
}
|
||||
if (numPro < 500)
|
||||
{
|
||||
if (this.RecvDataPro[0] == 0xE5)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
this.SendOrRecv = 4;
|
||||
int num = 0;
|
||||
this.Send(sendValue);
|
||||
while (this.SendOrRecv != 0 && num < 600)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
num++;
|
||||
}
|
||||
if (num < 600)
|
||||
{
|
||||
if (this.RecvData.Length == 1)
|
||||
{
|
||||
// FileControl.LogFile.SaveLog(this.clientSocket.RemoteEndPoint.ToString() + this.RecvData.Length.ToString());
|
||||
}
|
||||
if (this.RecvData.Length == 29)
|
||||
{
|
||||
flag = 0;
|
||||
if (this.RecvData[25] > 0)
|
||||
{
|
||||
value = this.RecvData[26] * 256 + this.RecvData[25];
|
||||
}
|
||||
else
|
||||
{
|
||||
value = this.RecvData[26];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// FileControl.LogFile.SaveLog(this.clientSocket.RemoteEndPoint.ToString() + " recv" + this.RecvData.Length.ToString());
|
||||
if (this.RecvData[0] == 0)
|
||||
{
|
||||
flag = -3;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// FileControl.LogFile.SaveLog(this.clientSocket.RemoteEndPoint.ToString() + " recvPro" + this.RecvDataPro.Length.ToString());
|
||||
if (this.RecvDataPro[0] == 0)
|
||||
{
|
||||
flag = -3;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.SendOrRecv = 0;
|
||||
return flag;
|
||||
}
|
||||
#endregion
|
||||
|
||||
private bool Check(Byte[] by)
|
||||
{
|
||||
Byte[] byt = by;
|
||||
if (byt[0] == 104 && byt[1] == 18 && byt[2] == 18 && byt[3] == 104 && byt[4] == 0 && byt[5] == 2 && byt[6] == 8 && byt[7] == 50 && byt[8] == 3 && byt[9] == 0 && byt[10] == 0 && byt[11] == 0 && byt[12] == 0 && byt[13] == 0 && byt[14] == 2 && byt[15] == 0 && byt[16] == 1 && byt[17] == 0 && byt[18] == 0 && byt[19] == 5 && byt[20] == 1 && byt[21] == 255 && byt[22] == 71 && byt[23] == 22)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#region IDispose member
|
||||
public void Dispose()
|
||||
{
|
||||
if (this.clientSocket.Connected)
|
||||
{
|
||||
this.clientSocket.Close();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user