Commit f4e2f67e 顾剑亮

修改客户端协议

1 个父辈 3f892a62
......@@ -31,8 +31,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net">
<HintPath>..\..\..\..\DLL\log4net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
......@@ -42,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Client.cs" />
<Compile Include="CodeFile1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
......
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Asa
{
/// <summary>
/// AGV客户端
/// </summary>
public class AgvClient
{
private bool loop;
private string _ip; //远程IP地址
private Socket client; //客户端
private Thread tSend; //发送
private Thread tListen; //监听网络
private Thread tReceive; //接收事件
private int countRecon;
private bool loopRecon;
private Thread tRecon;
private List<ClientNode> _node;
private System.Collections.Concurrent.ConcurrentQueue<ClientNode> _receive;
private const int PORT = 12000; //端口
/// <summary>
/// 小车动作事件
/// </summary>
public delegate void ActionEvent(string name, string rfid);
/// <summary>
/// 日志事件
/// </summary>
/// <param name="s"></param>
public delegate void LogEvent(string s);
/// <summary>
/// 小车到达,仅包装料仓
/// </summary>
public event ActionEvent Arrive;
/// <summary>
/// 小车已准备,对接完成
/// </summary>
public event ActionEvent Ready;
/// <summary>
/// 关门,仅包装料仓
/// </summary>
public event ActionEvent CloseDoor;
/// <summary>
/// 准备进入料架不能出料,仅包装料仓
/// </summary>
public event ActionEvent EnterShelf;
/// <summary>
/// 日志
/// </summary>
public event LogEvent Log;
/// <summary>
/// AGV客户端
/// </summary>
/// <param name="serverIP">服务器IP地址</param>
public AgvClient(string serverIP)
{
_ip = serverIP;
_node = new List<ClientNode>();
_receive = new System.Collections.Concurrent.ConcurrentQueue<ClientNode>();
}
/// <summary>
/// 是否连接服务器
/// </summary>
public bool IsConn { private set; get; } = false;
/// <summary>
/// 发送命令的日志是否打印,不影响其他日志
/// </summary>
public bool SendLog { set; get; } = false;
/// <summary>
/// 发送命令的时间间隔,不能大于10s(单位:秒)
/// </summary>
public int SendSleep { set; get; } = 3;
/// <summary>
/// 取消状态,true发送none,false发送实际状态
/// </summary>
public bool CancelState { set; get; } = false;
/// <summary>
/// 连接
/// </summary>
public void Connect()
{
countRecon = 0;
loopRecon = true;
IsConn = false;
tRecon = new Thread(new ThreadStart(Reconnect));
tRecon.Start();
}
/// <summary>
/// 关闭
/// </summary>
public void Close()
{
loop = false;
loopRecon = false;
if (client != null)
{
client.Close();
client = null;
}
Log?.Invoke("客户端关闭");
}
/// <summary>
/// 设置状态
/// </summary>
/// <param name="name">节点名称</param>
/// <param name="mark">节点标记</param>
/// <param name="rfid">架子RFID</param>
/// <param name="action"></param>
/// <param name="level"></param>
public void SetStatus(string name, string mark = "", string rfid = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low)
{
int idx = _node.FindIndex(s => s.Name.Equals(name));
if (idx == -1)
{
ClientNode node = new ClientNode(name, mark, rfid, action, level);
_node.Add(node);
Log?.Invoke("SetStatus " + node.ToText());
}
else
{
_node[idx].Mark = mark;
_node[idx].RFID = rfid;
_node[idx].Action = action;
_node[idx].Level = level;
Log?.Invoke("SetStatus " + _node[idx].ToText());
}
}
/// <summary>
/// 重连线程
/// </summary>
private void Reconnect()
{
while (loopRecon)
{
if (!IsConn)
{
Open();
if (IsConn)
{
countRecon = 0;
Log?.Invoke("连接服务器成功");
}
else
{
Log?.Invoke("连接服务器失败" + ++countRecon + "次");
}
}
Thread.Sleep(5000);
}
}
/// <summary>
/// 打开,连接到服务器
/// </summary>
private void Open()
{
try
{
if (CheckIP(_ip))
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2000);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
client.Connect(IPAddress.Parse(_ip), PORT);
if (!loopRecon) return;
IsConn = true;
loop = true;
tListen = new Thread(new ThreadStart(ListenNet));
tListen.Start();
tReceive = new Thread(new ThreadStart(Resolve));
tReceive.Start();
tSend = new Thread(new ThreadStart(SendStatus));
tSend.Start();
}
}
catch (Exception ex)
{
IsConn = false;
Log?.Invoke(ex.Message);
}
}
/// <summary>
/// 监听线程
/// </summary>
private void ListenNet()
{
byte[] temp = new byte[200];
int time = 100;
while (loop)
{
try
{
if (client.Available > 0)
{
int count = client.Receive(temp);
byte[] _buffer = new byte[count];
Array.Copy(temp, 0, _buffer, 0, count);
ClientNode node = Decode(_buffer);
if (node == null)
{
Log?.Invoke("命令解析失败: " + HexBuff(_buffer));
}
else
{
_receive.Enqueue(node);
Log?.Invoke("From Server: " + node.ToServerText());
}
}
}
catch (Exception ex)
{
IsConn = false;
Log?.Invoke(ex.Message);
}
Thread.Sleep(time);
}
}
/// <summary>
/// 分析数据包
/// </summary>
private void Resolve()
{
int time = 100;
while (loop)
{
Thread.Sleep(time);
try
{
if (!_receive.TryDequeue(out ClientNode result))
continue;
switch (result.Action)
{
case ClientAction.Arrive:
Log?.Invoke("触发Arrive事件");
Arrive?.Invoke(result.Name, result.RFID);
break;
case ClientAction.Ready:
Log?.Invoke("触发Ready事件");
Ready?.Invoke(result.Name, result.RFID);
break;
case ClientAction.CloseDoor:
Log?.Invoke("触发CloseDoor事件");
CloseDoor?.Invoke(result.Name, result.RFID);
break;
case ClientAction.EnterShelf:
Log?.Invoke("触发EnterShelf事件");
EnterShelf?.Invoke(result.Name, result.RFID);
break;
}
}
catch (Exception ex)
{
Log?.Invoke(ex.Message);
}
}
}
/// <summary>
/// 连续发送状态线程
/// </summary>
private void SendStatus()
{
while (loop)
{
//Socket没有建立连接
if (!IsConn)
{
Thread.Sleep(1000);
continue;
}
if (_node.Count == 0)
{
Thread.Sleep(5000);
continue;
}
for (int i = 0; i < _node.Count; i++)
{
if (!loop) break;
Thread.Sleep(100);
byte[] buff = Encode(_node[i]);
bool bln = Send(buff);
if (!bln)
{
IsConn = false;
loop = false;
break;
}
}
Thread.Sleep(SendSleep * 1000);
}
}
/// <summary>
/// 编码
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private byte[] Encode(ClientNode node)
{
byte[] name = System.Text.Encoding.UTF8.GetBytes(node.Name);
byte[] mark = System.Text.Encoding.UTF8.GetBytes(node.Mark);
byte[] rfid = System.Text.Encoding.UTF8.GetBytes(node.RFID);
int count = name.Length + mark.Length + rfid.Length + 7;
int idx = 0;
byte[] buff = new byte[count];
buff[idx++] = 0xAB;
buff[idx++] = Convert.ToByte(name.Length);
Array.Copy(name, 0, buff, idx, name.Length);
idx += name.Length;
buff[idx++] = Convert.ToByte(mark.Length);
Array.Copy(mark, 0, buff, idx, mark.Length);
idx += mark.Length;
buff[idx++] = Convert.ToByte(rfid.Length);
Array.Copy(rfid, 0, buff, idx, rfid.Length);
idx += rfid.Length;
if (CancelState)
buff[idx++] = (byte)ClientAction.None;
else
buff[idx++] = (byte)node.Action;
buff[idx++] = Convert.ToByte(node.Level);
buff[idx++] = 0xBA;
return buff;
}
/// <summary>
/// 发送命令
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private bool Send(byte[] buff)
{
if (!IsConn)
{
Log?.Invoke("Send 服务器没有连接");
return false;
}
try
{
if (!loopRecon) return false;
if (SendLog) Log?.Invoke("Send: " + HexBuff(buff));
client.Send(buff);
return true;
}
catch (Exception ex)
{
Log?.Invoke(ex.Message);
IsConn = false;
return false;
}
}
/// <summary>
/// 解码
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private ClientNode Decode(byte[] buff)
{
int idx = 0;
if (buff[idx++] != 0xAB) return null;
byte[] temp1 = new byte[buff[idx++]];
Array.Copy(buff, idx, temp1, 0, temp1.Length);
string name = System.Text.Encoding.UTF8.GetString(temp1);
idx += temp1.Length;
temp1 = new byte[buff[idx++]];
Array.Copy(buff, idx, temp1, 0, temp1.Length);
string mark = System.Text.Encoding.UTF8.GetString(temp1);
idx += temp1.Length;
temp1 = new byte[buff[idx++]];
Array.Copy(buff, idx, temp1, 0, temp1.Length);
string rfid = System.Text.Encoding.UTF8.GetString(temp1);
idx += temp1.Length;
ClientAction action = (ClientAction)buff[idx++];
ClientLevel level = (ClientLevel)buff[idx++];
ClientNode node = new ClientNode(name, mark, rfid, action, level);
if (buff[idx] != 0xBA)
return null;
return node;
}
/// <summary>
/// 16进制
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private string HexBuff(byte[] buff)
{
string s = "";
if (buff == null) return s;
for (int i = 0; i < buff.Length; i++)
s += buff[i].ToString("X2") + " ";
return s;
}
/// <summary>
/// 检查IP地址
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
private bool CheckIP(string ip)
{
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(ip, pattern);
if (!rtn)
{
Log?.Invoke("非法的IP地址" + ip);
return false;
}
//Ping服务端
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(ip, 2000);
ping.Dispose();
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
Log?.Invoke("Ping " + ip + " 请求没有响应");
return false;
}
return true;
}
catch (Exception ex)
{
Log?.Invoke(ex.Message);
return false;
}
}
}
/// <summary>
/// 客户端的节点
/// </summary>
public class ClientNode
{
/// <summary>
/// 节点名称
/// </summary>
public string Name { set; get; }
/// <summary>
/// 标记,用于包装料仓
/// </summary>
public string Mark { set; get; }
/// <summary>
/// 当前架子的RFID
/// </summary>
public string RFID { set; get; }
/// <summary>
/// 动作
/// </summary>
public ClientAction Action { set; get; }
/// <summary>
/// 优先级
/// </summary>
public ClientLevel Level { set; get; }
/// <summary>
/// 客户端节点
/// </summary>
/// <param name="name"></param>
/// <param name="mark"></param>
/// <param name="rfid"></param>
/// <param name="action"></param>
/// <param name="level"></param>
public ClientNode(string name, string mark, string rfid, ClientAction action, ClientLevel level)
{
Name = name;
Mark = mark;
RFID = rfid;
Action = action;
Level = level;
}
/// <summary>
/// 所有属性的文本形式
/// </summary>
/// <returns></returns>
public string ToText()
{
string s = string.Format("Name={0}, Action={1}, Level={2}, Mark={3}, RFID={4}", Name, Action, Level, Mark, RFID);
return s;
}
/// <summary>
/// 服务端的命令
/// </summary>
/// <returns></returns>
public string ToServerText()
{
string s = string.Format("Name={0}, Action={1}, RFID={2}", Name, Action, RFID);
return s;
}
}
/// <summary>
/// 客户端的动作
/// </summary>
public enum ClientAction : byte
{
/// <summary>
/// 没有动作
/// </summary>
None = 0,
/// <summary>
/// 包装料仓关门
/// </summary>
CloseDoor = 1,
/// <summary>
/// 可以进入料架,Arrive事件使用,让小车开始对接
/// </summary>
MayEnter = 2,
/// <summary>
/// 可以出去料架,Arrive事件使用,让小车开始对接
/// </summary>
MayLeave = 3,
/// <summary>
/// 需要进入料架
/// </summary>
NeedEnter = 4,
/// <summary>
/// 需要出去料架
/// </summary>
NeedLeave = 5,
/// <summary>
/// 完成进入料架
/// </summary>
FinishEnter = 6,
/// <summary>
/// 完成出去料架
/// </summary>
FinishLeave = 7,
/// <summary>
/// 小车到达,到达包装料仓门口,等待开门
/// </summary>
Arrive = 8,
/// <summary>
/// 小车已准备,已对接上流水线
/// </summary>
Ready = 9,
/// <summary>
/// 包装料仓只能入料不能出料
/// </summary>
EnterShelf = 10
}
/// <summary>
/// 客户端的优先级
/// </summary>
public enum ClientLevel : byte
{
/// <summary>
/// 低
/// </summary>
Low = 0,
/// <summary>
/// 中等
/// </summary>
Middle = 1,
/// <summary>
/// 高
/// </summary>
High = 2
}
}
//using System;
//using System.Collections.Generic;
//using System.Net;
//using System.Net.Sockets;
//using System.Threading;
//namespace Asa
//{
// /// <summary>
// /// AGV客户端
// /// </summary>
// public class AgvClient
// {
// private bool loop;
// private string _ip; //远程IP地址
// private Socket client; //客户端
// private Thread tSend; //发送
// private Thread tListen; //监听网络
// private Thread tReceive; //接收事件
// private int countRecon;
// private bool loopRecon;
// private Thread tRecon;
// private List<ClientNode> _node;
// private System.Collections.Concurrent.ConcurrentQueue<ClientNode> _receive;
// private const int PORT = 12000; //端口
// /// <summary>
// /// 小车动作事件
// /// </summary>
// public delegate void ActionEvent(string name, string rfid);
// /// <summary>
// /// 日志事件
// /// </summary>
// /// <param name="s"></param>
// public delegate void LogEvent(string s);
// /// <summary>
// /// 小车到达,仅包装料仓
// /// </summary>
// public event ActionEvent Arrive;
// /// <summary>
// /// 小车已准备,对接完成
// /// </summary>
// public event ActionEvent Ready;
// /// <summary>
// /// 关门,仅包装料仓
// /// </summary>
// public event ActionEvent CloseDoor;
// /// <summary>
// /// 准备进入料架不能出料,仅包装料仓
// /// </summary>
// public event ActionEvent EnterShelf;
// /// <summary>
// /// 日志
// /// </summary>
// public event LogEvent Log;
// /// <summary>
// /// AGV客户端
// /// </summary>
// /// <param name="serverIP">服务器IP地址</param>
// public AgvClient(string serverIP)
// {
// _ip = serverIP;
// _node = new List<ClientNode>();
// _receive = new System.Collections.Concurrent.ConcurrentQueue<ClientNode>();
// }
// /// <summary>
// /// 是否连接服务器
// /// </summary>
// public bool IsConn { private set; get; } = false;
// /// <summary>
// /// 发送命令的日志是否打印,不影响其他日志
// /// </summary>
// public bool SendLog { set; get; } = false;
// /// <summary>
// /// 发送命令的时间间隔,不能大于10s(单位:秒)
// /// </summary>
// public int SendSleep { set; get; } = 3;
// /// <summary>
// /// 取消状态,true发送none,false发送实际状态
// /// </summary>
// public bool CancelState { set; get; } = false;
// /// <summary>
// /// 连接
// /// </summary>
// public void Connect()
// {
// countRecon = 0;
// loopRecon = true;
// IsConn = false;
// tRecon = new Thread(new ThreadStart(Reconnect));
// tRecon.Start();
// }
// /// <summary>
// /// 关闭
// /// </summary>
// public void Close()
// {
// loop = false;
// loopRecon = false;
// if (client != null)
// {
// client.Close();
// client = null;
// }
// Log?.Invoke("客户端关闭");
// }
// /// <summary>
// /// 设置状态
// /// </summary>
// /// <param name="name">节点名称</param>
// /// <param name="mark">节点标记</param>
// /// <param name="rfid">架子RFID</param>
// /// <param name="action"></param>
// /// <param name="level"></param>
// public void SetStatus(string name, string mark = "", string rfid = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.Low)
// {
// int idx = _node.FindIndex(s => s.Name.Equals(name));
// if (idx == -1)
// {
// ClientNode node = new ClientNode(name, mark, rfid, action, level);
// _node.Add(node);
// Log?.Invoke("SetStatus " + node.ToText());
// }
// else
// {
// _node[idx].Mark = mark;
// _node[idx].RFID = rfid;
// _node[idx].Action = action;
// _node[idx].Level = level;
// Log?.Invoke("SetStatus " + _node[idx].ToText());
// }
// }
// /// <summary>
// /// 重连线程
// /// </summary>
// private void Reconnect()
// {
// while (loopRecon)
// {
// if (!IsConn)
// {
// Open();
// if (IsConn)
// {
// countRecon = 0;
// Log?.Invoke("连接服务器成功");
// }
// else
// {
// Log?.Invoke("连接服务器失败" + ++countRecon + "次");
// }
// }
// Thread.Sleep(5000);
// }
// }
// /// <summary>
// /// 打开,连接到服务器
// /// </summary>
// private void Open()
// {
// try
// {
// if (CheckIP(_ip))
// {
// client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2000);
// client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
// client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
// client.Connect(IPAddress.Parse(_ip), PORT);
// if (!loopRecon) return;
// IsConn = true;
// loop = true;
// tListen = new Thread(new ThreadStart(ListenNet));
// tListen.Start();
// tReceive = new Thread(new ThreadStart(Resolve));
// tReceive.Start();
// tSend = new Thread(new ThreadStart(SendStatus));
// tSend.Start();
// }
// }
// catch (Exception ex)
// {
// IsConn = false;
// Log?.Invoke(ex.Message);
// }
// }
// /// <summary>
// /// 监听线程
// /// </summary>
// private void ListenNet()
// {
// byte[] temp = new byte[200];
// int time = 100;
// while (loop)
// {
// try
// {
// if (client.Available > 0)
// {
// int count = client.Receive(temp);
// byte[] _buffer = new byte[count];
// Array.Copy(temp, 0, _buffer, 0, count);
// ClientNode node = Decode(_buffer);
// if (node == null)
// {
// Log?.Invoke("命令解析失败: " + HexBuff(_buffer));
// }
// else
// {
// _receive.Enqueue(node);
// Log?.Invoke("From Server: " + node.ToServerText());
// }
// }
// }
// catch (Exception ex)
// {
// IsConn = false;
// Log?.Invoke(ex.Message);
// }
// Thread.Sleep(time);
// }
// }
// /// <summary>
// /// 分析数据包
// /// </summary>
// private void Resolve()
// {
// int time = 100;
// while (loop)
// {
// Thread.Sleep(time);
// try
// {
// if (!_receive.TryDequeue(out ClientNode result))
// continue;
// switch (result.Action)
// {
// case ClientAction.Arrive:
// Log?.Invoke("触发Arrive事件");
// Arrive?.Invoke(result.Name, result.RFID);
// break;
// case ClientAction.Ready:
// Log?.Invoke("触发Ready事件");
// Ready?.Invoke(result.Name, result.RFID);
// break;
// case ClientAction.CloseDoor:
// Log?.Invoke("触发CloseDoor事件");
// CloseDoor?.Invoke(result.Name, result.RFID);
// break;
// case ClientAction.EnterShelf:
// Log?.Invoke("触发EnterShelf事件");
// EnterShelf?.Invoke(result.Name, result.RFID);
// break;
// }
// }
// catch (Exception ex)
// {
// Log?.Invoke(ex.Message);
// }
// }
// }
// /// <summary>
// /// 连续发送状态线程
// /// </summary>
// private void SendStatus()
// {
// while (loop)
// {
// //Socket没有建立连接
// if (!IsConn)
// {
// Thread.Sleep(1000);
// continue;
// }
// if (_node.Count == 0)
// {
// Thread.Sleep(5000);
// continue;
// }
// for (int i = 0; i < _node.Count; i++)
// {
// if (!loop) break;
// Thread.Sleep(100);
// byte[] buff = Encode(_node[i]);
// bool bln = Send(buff);
// if (!bln)
// {
// IsConn = false;
// loop = false;
// break;
// }
// }
// Thread.Sleep(SendSleep * 1000);
// }
// }
// /// <summary>
// /// 编码
// /// </summary>
// /// <param name="node"></param>
// /// <returns></returns>
// private byte[] Encode(ClientNode node)
// {
// byte[] name = System.Text.Encoding.UTF8.GetBytes(node.Name);
// byte[] mark = System.Text.Encoding.UTF8.GetBytes(node.Mark);
// byte[] rfid = System.Text.Encoding.UTF8.GetBytes(node.RFID);
// int count = name.Length + mark.Length + rfid.Length + 7;
// int idx = 0;
// byte[] buff = new byte[count];
// buff[idx++] = 0xAB;
// buff[idx++] = Convert.ToByte(name.Length);
// Array.Copy(name, 0, buff, idx, name.Length);
// idx += name.Length;
// buff[idx++] = Convert.ToByte(mark.Length);
// Array.Copy(mark, 0, buff, idx, mark.Length);
// idx += mark.Length;
// buff[idx++] = Convert.ToByte(rfid.Length);
// Array.Copy(rfid, 0, buff, idx, rfid.Length);
// idx += rfid.Length;
// if (CancelState)
// buff[idx++] = (byte)ClientAction.None;
// else
// buff[idx++] = (byte)node.Action;
// buff[idx++] = Convert.ToByte(node.Level);
// buff[idx++] = 0xBA;
// return buff;
// }
// /// <summary>
// /// 发送命令
// /// </summary>
// /// <param name="buff"></param>
// /// <returns></returns>
// private bool Send(byte[] buff)
// {
// if (!IsConn)
// {
// Log?.Invoke("Send 服务器没有连接");
// return false;
// }
// try
// {
// if (!loopRecon) return false;
// if (SendLog) Log?.Invoke("Send: " + HexBuff(buff));
// client.Send(buff);
// return true;
// }
// catch (Exception ex)
// {
// Log?.Invoke(ex.Message);
// IsConn = false;
// return false;
// }
// }
// /// <summary>
// /// 解码
// /// </summary>
// /// <param name="buff"></param>
// /// <returns></returns>
// private ClientNode Decode(byte[] buff)
// {
// int idx = 0;
// if (buff[idx++] != 0xAB) return null;
// byte[] temp1 = new byte[buff[idx++]];
// Array.Copy(buff, idx, temp1, 0, temp1.Length);
// string name = System.Text.Encoding.UTF8.GetString(temp1);
// idx += temp1.Length;
// temp1 = new byte[buff[idx++]];
// Array.Copy(buff, idx, temp1, 0, temp1.Length);
// string mark = System.Text.Encoding.UTF8.GetString(temp1);
// idx += temp1.Length;
// temp1 = new byte[buff[idx++]];
// Array.Copy(buff, idx, temp1, 0, temp1.Length);
// string rfid = System.Text.Encoding.UTF8.GetString(temp1);
// idx += temp1.Length;
// ClientAction action = (ClientAction)buff[idx++];
// ClientLevel level = (ClientLevel)buff[idx++];
// ClientNode node = new ClientNode(name, mark, rfid, action, level);
// if (buff[idx] != 0xBA)
// return null;
// return node;
// }
// /// <summary>
// /// 16进制
// /// </summary>
// /// <param name="buff"></param>
// /// <returns></returns>
// private string HexBuff(byte[] buff)
// {
// string s = "";
// if (buff == null) return s;
// for (int i = 0; i < buff.Length; i++)
// s += buff[i].ToString("X2") + " ";
// return s;
// }
// /// <summary>
// /// 检查IP地址
// /// </summary>
// /// <param name="ip"></param>
// /// <returns></returns>
// private bool CheckIP(string ip)
// {
// //IP合法
// string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
// bool rtn = System.Text.RegularExpressions.Regex.IsMatch(ip, pattern);
// if (!rtn)
// {
// Log?.Invoke("非法的IP地址" + ip);
// return false;
// }
// //Ping服务端
// try
// {
// System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
// System.Net.NetworkInformation.PingReply result = ping.Send(ip, 2000);
// ping.Dispose();
// if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
// {
// Log?.Invoke("Ping " + ip + " 请求没有响应");
// return false;
// }
// return true;
// }
// catch (Exception ex)
// {
// Log?.Invoke(ex.Message);
// return false;
// }
// }
// }
// /// <summary>
// /// 客户端的节点
// /// </summary>
// public class ClientNode
// {
// /// <summary>
// /// 节点名称
// /// </summary>
// public string Name { set; get; }
// /// <summary>
// /// 标记,用于包装料仓
// /// </summary>
// public string Mark { set; get; }
// /// <summary>
// /// 当前架子的RFID
// /// </summary>
// public string RFID { set; get; }
// /// <summary>
// /// 动作
// /// </summary>
// public ClientAction Action { set; get; }
// /// <summary>
// /// 优先级
// /// </summary>
// public ClientLevel Level { set; get; }
// /// <summary>
// /// 客户端节点
// /// </summary>
// /// <param name="name"></param>
// /// <param name="mark"></param>
// /// <param name="rfid"></param>
// /// <param name="action"></param>
// /// <param name="level"></param>
// public ClientNode(string name, string mark, string rfid, ClientAction action, ClientLevel level)
// {
// Name = name;
// Mark = mark;
// RFID = rfid;
// Action = action;
// Level = level;
// }
// /// <summary>
// /// 所有属性的文本形式
// /// </summary>
// /// <returns></returns>
// public string ToText()
// {
// string s = string.Format("Name={0}, Action={1}, Level={2}, Mark={3}, RFID={4}", Name, Action, Level, Mark, RFID);
// return s;
// }
// /// <summary>
// /// 服务端的命令
// /// </summary>
// /// <returns></returns>
// public string ToServerText()
// {
// string s = string.Format("Name={0}, Action={1}, RFID={2}", Name, Action, RFID);
// return s;
// }
// }
// /// <summary>
// /// 客户端的动作
// /// </summary>
// public enum ClientAction : byte
// {
// /// <summary>
// /// 没有动作
// /// </summary>
// None = 0,
// /// <summary>
// /// 包装料仓关门
// /// </summary>
// CloseDoor = 1,
// /// <summary>
// /// 可以进入料架,Arrive事件使用,让小车开始对接
// /// </summary>
// MayEnter = 2,
// /// <summary>
// /// 可以出去料架,Arrive事件使用,让小车开始对接
// /// </summary>
// MayLeave = 3,
// /// <summary>
// /// 需要进入料架
// /// </summary>
// NeedEnter = 4,
// /// <summary>
// /// 需要出去料架
// /// </summary>
// NeedLeave = 5,
// /// <summary>
// /// 完成进入料架
// /// </summary>
// FinishEnter = 6,
// /// <summary>
// /// 完成出去料架
// /// </summary>
// FinishLeave = 7,
// /// <summary>
// /// 小车到达,到达包装料仓门口,等待开门
// /// </summary>
// Arrive = 8,
// /// <summary>
// /// 小车已准备,已对接上流水线
// /// </summary>
// Ready = 9,
// /// <summary>
// /// 包装料仓只能入料不能出料
// /// </summary>
// EnterShelf = 10
// }
// /// <summary>
// /// 客户端的优先级
// /// </summary>
// public enum ClientLevel : byte
// {
// /// <summary>
// /// 低
// /// </summary>
// Low = 0,
// /// <summary>
// /// 中等
// /// </summary>
// Middle = 1,
// /// <summary>
// /// 高
// /// </summary>
// High = 2
// }
//}
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Web.Script.Serialization;
namespace Asa
{
/// <summary>
/// AGV客户端
/// </summary>
public class AgvClient
{
private bool loop;
private Socket client; //客户端
private Thread tSend; //发送
private Thread tListen; //监听网络
private int countRecon;
private bool loopRecon;
private Thread tRecon;
private List<ClientNode> _node;
private readonly log4net.ILog LOG;
private readonly string IP; //远程IP地址
private const int PORT = 12000; //端口
/// <summary>
/// 小车接收事件
/// </summary>
/// <param name="name">名称</param>
/// <param name="realRfid">真实RFID</param>
/// <param name="virtualRfid">虚拟RFID</param>
/// <param name="action">动作</param>
/// <param name="level">优先级</param>
/// <param name="shelf">料架</param>
/// <param name="place">放置地点</param>
public delegate void ReceiveEvent(string name, string realRfid, string virtualRfid, ClientAction action, ClientLevel level, ClientShelf shelf, ClientPlace place);
/// <summary>
/// 服务器连接事件
/// </summary>
/// <param name="status"></param>
public delegate void ConnectEvent(bool status);
/// <summary>
/// 小车接收事件
/// </summary>
public event ReceiveEvent Received;
/// <summary>
/// 服务器连接事件
/// </summary>
public event ConnectEvent Connected;
/// <summary>
/// AGV客户端
/// </summary>
/// <param name="serverIP">服务器IP地址</param>
/// <param name="logName"></param>
public AgvClient(string serverIP, string logName = "AgvClient")
{
IP = serverIP;
_node = new List<ClientNode>();
LOG = log4net.LogManager.GetLogger(logName);
}
/// <summary>
/// 是否连接服务器
/// </summary>
public bool IsConn { private set; get; } = false;
/// <summary>
/// 发送命令的时间间隔,不能大于10s(单位:秒)
/// </summary>
public int SendSleep { set; get; } = 3;
/// <summary>
/// 连接
/// </summary>
public void Connect()
{
LOG.Info("Connect Function");
countRecon = 0;
loopRecon = true;
IsConn = false;
loop = true;
tListen = new Thread(new ThreadStart(ListenNet));
tSend = new Thread(new ThreadStart(SendStatus));
tRecon = new Thread(new ThreadStart(Reconnect));
tRecon.Start();
tListen.Start();
tSend.Start();
}
/// <summary>
/// 关闭
/// </summary>
public void Close()
{
loop = false;
loopRecon = false;
if (client != null)
{
client.Close();
client = null;
}
LOG.Info("客户端关闭");
}
/// <summary>
/// 设置状态
/// </summary>
/// <param name="name"></param>
/// <param name="realRfid"></param>
/// <param name="virtualRfid"></param>
/// <param name="action"></param>
/// <param name="level"></param>
/// <param name="shelf"></param>
/// <param name="place"></param>
public void SetStatus(string name, string realRfid = "", string virtualRfid = "", ClientAction action = ClientAction.None, ClientLevel level = ClientLevel.None, ClientShelf shelf = ClientShelf.None, ClientPlace place = ClientPlace.None)
{
int index = _node.FindIndex(s => s.Name == name);
if (index == -1) //没有找到
{
ClientNode node = new ClientNode
{
Name = name,
RealRfid = realRfid,
VirtualRfid = virtualRfid,
Action = action.ToString(),
Level = level.ToString(),
Shelf = shelf.ToString(),
Place = place.ToString()
};
_node.Add(node);
LOG.Info("SetStatus " + node.ToText());
}
else
{
_node[index].RealRfid = realRfid;
_node[index].VirtualRfid = virtualRfid;
_node[index].Action = action.ToString();
_node[index].Level = level.ToString();
_node[index].Shelf = shelf.ToString();
_node[index].Place = place.ToString();
LOG.Info("SetStatus " + _node[index].ToText());
}
}
/// <summary>
/// 重连线程
/// </summary>
private void Reconnect()
{
while (loopRecon)
{
if (!IsConn)
{
Open();
if (IsConn)
{
countRecon = 0;
LOG.Info("连接服务器成功");
Connected?.Invoke(IsConn);
}
else
{
LOG.Info("连接服务器失败" + ++countRecon + "次");
}
}
Thread.Sleep(2000);
}
}
/// <summary>
/// 打开,连接到服务器
/// </summary>
private void Open()
{
try
{
if (CheckIP(IP))
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2000);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000);
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
client.Connect(IPAddress.Parse(IP), PORT);
if (!loopRecon) return;
IsConn = true;
}
}
catch (Exception ex)
{
IsConn = false;
LOG.Error("Open Error", ex);
}
}
/// <summary>
/// 监听线程
/// </summary>
private void ListenNet()
{
byte[] temp = new byte[200];
int time = 100;
while (loop)
{
Thread.Sleep(time);
if (!IsConn) continue;
if (client == null) continue;
try
{
if (!loop) break;
if (client.Available > 0)
{
int count = client.Receive(temp);
byte[] _buffer = new byte[count];
Array.Copy(temp, 0, _buffer, 0, count);
ClientNode node = Decode(_buffer);
if (node == null)
LOG.Info("命令解析失败");
else
Resolve(node);
}
}
catch (Exception ex)
{
IsConn = false;
LOG.Error("ListenNet Error", ex);
Connected?.Invoke(IsConn);
}
}
}
/// <summary>
/// 分析数据包
/// </summary>
private void Resolve(ClientNode result)
{
try
{
ClientAction action = (ClientAction)Enum.Parse(typeof(ClientAction), result.Action);
ClientLevel level = (ClientLevel)Enum.Parse(typeof(ClientLevel), result.Level);
ClientShelf shelf = (ClientShelf)Enum.Parse(typeof(ClientShelf), result.Shelf);
ClientPlace place = (ClientPlace)Enum.Parse(typeof(ClientPlace), result.Place);
LOG.Info("触发Received事件," + result.ToText());
Received?.Invoke(result.Name, result.RealRfid, result.VirtualRfid, action, level, shelf, place);
}
catch (Exception ex)
{
LOG.Error("Resolve Error", ex);
}
}
/// <summary>
/// 连续发送状态线程
/// </summary>
private void SendStatus()
{
while (loop)
{
Thread.Sleep(SendSleep * 1000);
if (!IsConn) continue;
if (client == null) continue;
for (int i = 0; i < _node.Count; i++)
{
if (!loop) break;
Thread.Sleep(100);
byte[] buff = Encode(_node[i]);
bool bln = Send(buff);
if (!bln) continue;
}
}
}
/// <summary>
/// 编码
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private byte[] Encode(ClientNode node)
{
try
{
System.Reflection.PropertyInfo[] info = node.GetType().GetProperties();
string[] arr = new string[info.Length];
for (int i = 0; i < info.Length; i++)
arr[i] = string.Format("\"{0}\":\"{1}\"", info[i].Name, info[i].GetValue(node));
string json = "{" + string.Join(",", arr) + "}";
byte[] buff = System.Text.Encoding.UTF8.GetBytes(json);
return buff;
}
catch (Exception ex)
{
LOG.Error("Encode Error", ex);
return null;
}
}
/// <summary>
/// 解码
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private ClientNode Decode(byte[] buff)
{
try
{
string json = System.Text.Encoding.UTF8.GetString(buff);
LOG.Info("收到JSON " + json);
string[] arr = json.Trim(new char[] { '{', '}' }).Split(',');
ClientNode node = new ClientNode();
System.Reflection.PropertyInfo[] info = node.GetType().GetProperties();
for (int i = 0; i < info.Length; i++)
{
string name = string.Format("\"{0}\":", info[i].Name);
int index = Array.FindIndex(arr, s => s.StartsWith(name));
if (index == -1) continue;
info[i].SetValue(node, arr[index].Replace(name, "").Trim('\"'));
}
return node;
}
catch (Exception ex)
{
LOG.Error("Decode Error", ex);
return null;
}
}
/// <summary>
/// 发送命令
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private bool Send(byte[] buff)
{
if (!IsConn)
{
LOG.Info("Send 服务器没有连接");
return false;
}
try
{
if (!loopRecon) return false;
if (buff == null) return false;
LOG.Debug("Send: " + HexBuff(buff));
client.Send(buff);
return true;
}
catch (Exception ex)
{
LOG.Error("Send Error", ex);
IsConn = false;
Connected?.Invoke(IsConn);
return false;
}
}
/// <summary>
/// 16进制
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private string HexBuff(byte[] buff)
{
string s = "";
if (buff == null) return s;
for (int i = 0; i < buff.Length; i++)
s += buff[i].ToString("X2") + " ";
return s;
}
/// <summary>
/// 检查IP地址
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
private bool CheckIP(string ip)
{
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(ip, pattern);
if (!rtn)
{
LOG.Info("非法的IP地址" + ip);
return false;
}
//Ping服务端
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(ip, 2000);
ping.Dispose();
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
LOG.Info("Ping " + ip + " 请求没有响应");
return false;
}
return true;
}
catch (Exception ex)
{
LOG.Error("CheckIP Error", ex);
return false;
}
}
}
/// <summary>
/// 客户端的节点
/// </summary>
public class ClientNode
{
/// <summary>
/// 节点名称
/// </summary>
public string Name { set; get; } = "";
/// <summary>
/// 真实的料架号
/// </summary>
public string RealRfid { set; get; } = "";
/// <summary>
/// 虚拟的料架号
/// </summary>
public string VirtualRfid { set; get; } = "";
/// <summary>
/// 动作
/// </summary>
public string Action { set; get; } = "";
/// <summary>
/// 优先级
/// </summary>
public string Level { set; get; } = "";
/// <summary>
/// 放置的地方
/// </summary>
public string Place { set; get; } = "";
/// <summary>
/// 料架
/// </summary>
public string Shelf { set; get; } = "";
/// <summary>
/// 客户端的节点
/// </summary>
public ClientNode()
{
}
/// <summary>
/// 所有属性的文本形式
/// </summary>
/// <returns></returns>
public string ToText()
{
string s = string.Format("Name={0}, Action={1}, Level={2}, Place={3}, RealRfid={4}, VirtualRfid={5}, Shelf={6}", Name, Action, Level, Place, RealRfid, VirtualRfid, Shelf);
return s;
}
}
/// <summary>
/// 客户端的动作
/// </summary>
public enum ClientAction
{
/// <summary>
/// 没有动作
/// </summary>
None,
/// <summary>
/// 需要进入A料架
/// </summary>
NeedEnterA,
/// <summary>
/// 需要进入B料架
/// </summary>
NeedEnterB,
/// <summary>
/// 需要进入C料架
/// </summary>
NeedEnterC,
/// <summary>
/// 需要进入D料架
/// </summary>
NeedEnterD,
/// <summary>
/// 需要进入料架
/// </summary>
NeedEnter,
/// <summary>
/// 需要流出料架
/// </summary>
NeedLeave,
/// <summary>
/// 需要进入流出料架
/// </summary>
NeedEnterLeave,
/// <summary>
/// 可以进入料架
/// </summary>
MayEnter,
/// <summary>
/// 可以流出料架
/// </summary>
MayLeave,
/// <summary>
/// 可以停靠
/// </summary>
MayDock,
/// <summary>
/// 完成进入料架
/// </summary>
FinishEnter,
/// <summary>
/// 完成流出料架
/// </summary>
FinishLeave,
/// <summary>
/// 小车准备好(服务器发送)
/// </summary>
Ready,
/// <summary>
/// 到达包装料仓门口(服务器发送)
/// </summary>
Arrive,
/// <summary>
/// 完成(服务器发送)
/// </summary>
Complete
}
/// <summary>
/// 客户端的优先级
/// </summary>
public enum ClientLevel
{
None,
/// <summary>
/// 低
/// </summary>
Low,
/// <summary>
/// 中等
/// </summary>
Middle,
/// <summary>
/// 高
/// </summary>
High
}
/// <summary>
/// 客户端的料架
/// </summary>
public enum ClientShelf
{
/// <summary>
/// 没有架子
/// </summary>
None,
/// <summary>
/// 空架子
/// </summary>
Empty,
/// <summary>
/// 满料架子
/// </summary>
Full
}
/// <summary>
/// 放置地点
/// </summary>
public enum ClientPlace
{
/// <summary>
/// 无
/// </summary>
None,
/// <summary>
/// 紧急料
/// </summary>
Urgent,
/// <summary>
/// 包装料
/// </summary>
Pack,
/// <summary>
/// 分盘料
/// </summary>
Cut
}
}
cb561d0b559b2b210a73908bfa1b19f722a475ae
a0778d1eda99bd700643c1ae6955f161aa53e149
......@@ -10,3 +10,11 @@ D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClient\obj\Deb
D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClient\obj\Debug\AgvClient.csproj.CoreCompileInputs.cache
D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClient\obj\Debug\Client.dll
D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClient\obj\Debug\Client.pdb
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\bin\Debug\Client.dll
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\bin\Debug\Client.pdb
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\bin\Debug\log4net.dll
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\obj\Debug\AgvClient.csproj.CoreCompileInputs.cache
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\obj\Debug\AgvClient.csproj.CopyComplete
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\obj\Debug\Client.dll
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\obj\Debug\Client.pdb
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClient\obj\Debug\AgvClient.csprojAssemblyReference.cache
......@@ -28,455 +28,23 @@
/// </summary>
private void InitializeComponent()
{
this.TxtName1 = new System.Windows.Forms.TextBox();
this.BtnSend1 = new System.Windows.Forms.Button();
this.CboAction1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.TxtMark1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.TxtRFID1 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.CboLevel1 = new System.Windows.Forms.ComboBox();
this.TxtLog = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.CboLevel2 = new System.Windows.Forms.ComboBox();
this.TxtRFID2 = new System.Windows.Forms.TextBox();
this.TxtMark2 = new System.Windows.Forms.TextBox();
this.CboAction2 = new System.Windows.Forms.ComboBox();
this.BtnSend2 = new System.Windows.Forms.Button();
this.TxtName2 = new System.Windows.Forms.TextBox();
this.CboLevel4 = new System.Windows.Forms.ComboBox();
this.TxtRFID4 = new System.Windows.Forms.TextBox();
this.TxtMark4 = new System.Windows.Forms.TextBox();
this.CboAction4 = new System.Windows.Forms.ComboBox();
this.BtnSend4 = new System.Windows.Forms.Button();
this.TxtName4 = new System.Windows.Forms.TextBox();
this.CboLevel3 = new System.Windows.Forms.ComboBox();
this.TxtRFID3 = new System.Windows.Forms.TextBox();
this.TxtMark3 = new System.Windows.Forms.TextBox();
this.CboAction3 = new System.Windows.Forms.ComboBox();
this.BtnSend3 = new System.Windows.Forms.Button();
this.TxtName3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// TxtName1
//
this.TxtName1.Location = new System.Drawing.Point(12, 24);
this.TxtName1.Name = "TxtName1";
this.TxtName1.Size = new System.Drawing.Size(100, 21);
this.TxtName1.TabIndex = 0;
//
// BtnSend1
//
this.BtnSend1.Location = new System.Drawing.Point(542, 24);
this.BtnSend1.Name = "BtnSend1";
this.BtnSend1.Size = new System.Drawing.Size(100, 21);
this.BtnSend1.TabIndex = 1;
this.BtnSend1.Text = "Send";
this.BtnSend1.UseVisualStyleBackColor = true;
this.BtnSend1.Click += new System.EventHandler(this.BtnSend_Click);
//
// CboAction1
//
this.CboAction1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboAction1.FormattingEnabled = true;
this.CboAction1.Items.AddRange(new object[] {
"None=0",
"CloseDoor=1",
"MayEnter=2",
"MayLeave=3",
"NeedEnter=4",
"NeedLeave=5",
"FinishEnter=6",
"FinishLeave=7",
"Arrive=8",
"Ready=9",
"EnterShelf=10"});
this.CboAction1.Location = new System.Drawing.Point(330, 24);
this.CboAction1.Name = "CboAction1";
this.CboAction1.Size = new System.Drawing.Size(100, 20);
this.CboAction1.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 5;
this.label1.Text = "Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(118, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 7;
this.label2.Text = "Mark";
//
// TxtMark1
//
this.TxtMark1.Location = new System.Drawing.Point(118, 24);
this.TxtMark1.Name = "TxtMark1";
this.TxtMark1.Size = new System.Drawing.Size(100, 21);
this.TxtMark1.TabIndex = 6;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(224, 9);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 9;
this.label3.Text = "RFID";
//
// TxtRFID1
//
this.TxtRFID1.Location = new System.Drawing.Point(224, 24);
this.TxtRFID1.Name = "TxtRFID1";
this.TxtRFID1.Size = new System.Drawing.Size(100, 21);
this.TxtRFID1.TabIndex = 8;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(330, 9);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(41, 12);
this.label4.TabIndex = 10;
this.label4.Text = "Action";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(436, 9);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(35, 12);
this.label5.TabIndex = 12;
this.label5.Text = "Level";
//
// CboLevel1
//
this.CboLevel1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboLevel1.FormattingEnabled = true;
this.CboLevel1.Items.AddRange(new object[] {
"Low=0",
"Middle=1",
"High=2"});
this.CboLevel1.Location = new System.Drawing.Point(436, 24);
this.CboLevel1.Name = "CboLevel1";
this.CboLevel1.Size = new System.Drawing.Size(100, 20);
this.CboLevel1.TabIndex = 11;
//
// TxtLog
//
this.TxtLog.Location = new System.Drawing.Point(12, 132);
this.TxtLog.Multiline = true;
this.TxtLog.Name = "TxtLog";
this.TxtLog.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.TxtLog.Size = new System.Drawing.Size(524, 144);
this.TxtLog.TabIndex = 13;
//
// button2
//
this.button2.Location = new System.Drawing.Point(542, 225);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(100, 21);
this.button2.TabIndex = 14;
this.button2.Text = "打开";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(542, 254);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(100, 21);
this.button3.TabIndex = 15;
this.button3.Text = "关闭";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// CboLevel2
//
this.CboLevel2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboLevel2.FormattingEnabled = true;
this.CboLevel2.Items.AddRange(new object[] {
"Low=0",
"Middle=1",
"High=2"});
this.CboLevel2.Location = new System.Drawing.Point(436, 51);
this.CboLevel2.Name = "CboLevel2";
this.CboLevel2.Size = new System.Drawing.Size(100, 20);
this.CboLevel2.TabIndex = 21;
//
// TxtRFID2
//
this.TxtRFID2.Location = new System.Drawing.Point(224, 51);
this.TxtRFID2.Name = "TxtRFID2";
this.TxtRFID2.Size = new System.Drawing.Size(100, 21);
this.TxtRFID2.TabIndex = 20;
//
// TxtMark2
//
this.TxtMark2.Location = new System.Drawing.Point(118, 51);
this.TxtMark2.Name = "TxtMark2";
this.TxtMark2.Size = new System.Drawing.Size(100, 21);
this.TxtMark2.TabIndex = 19;
//
// CboAction2
//
this.CboAction2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboAction2.FormattingEnabled = true;
this.CboAction2.Items.AddRange(new object[] {
"None=0",
"CloseDoor=1",
"MayEnter=2",
"MayLeave=3",
"NeedEnter=4",
"NeedLeave=5",
"FinishEnter=6",
"FinishLeave=7",
"Arrive=8",
"Ready=9",
"EnterShelf=10"});
this.CboAction2.Location = new System.Drawing.Point(330, 51);
this.CboAction2.Name = "CboAction2";
this.CboAction2.Size = new System.Drawing.Size(100, 20);
this.CboAction2.TabIndex = 18;
//
// BtnSend2
//
this.BtnSend2.Location = new System.Drawing.Point(542, 50);
this.BtnSend2.Name = "BtnSend2";
this.BtnSend2.Size = new System.Drawing.Size(100, 21);
this.BtnSend2.TabIndex = 17;
this.BtnSend2.Text = "Send";
this.BtnSend2.UseVisualStyleBackColor = true;
this.BtnSend2.Click += new System.EventHandler(this.BtnSend_Click);
//
// TxtName2
//
this.TxtName2.Location = new System.Drawing.Point(12, 51);
this.TxtName2.Name = "TxtName2";
this.TxtName2.Size = new System.Drawing.Size(100, 21);
this.TxtName2.TabIndex = 16;
//
// CboLevel4
//
this.CboLevel4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboLevel4.FormattingEnabled = true;
this.CboLevel4.Items.AddRange(new object[] {
"Low=0",
"Middle=1",
"High=2"});
this.CboLevel4.Location = new System.Drawing.Point(436, 105);
this.CboLevel4.Name = "CboLevel4";
this.CboLevel4.Size = new System.Drawing.Size(100, 20);
this.CboLevel4.TabIndex = 33;
//
// TxtRFID4
//
this.TxtRFID4.Location = new System.Drawing.Point(224, 105);
this.TxtRFID4.Name = "TxtRFID4";
this.TxtRFID4.Size = new System.Drawing.Size(100, 21);
this.TxtRFID4.TabIndex = 32;
//
// TxtMark4
//
this.TxtMark4.Location = new System.Drawing.Point(118, 105);
this.TxtMark4.Name = "TxtMark4";
this.TxtMark4.Size = new System.Drawing.Size(100, 21);
this.TxtMark4.TabIndex = 31;
//
// CboAction4
//
this.CboAction4.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboAction4.FormattingEnabled = true;
this.CboAction4.Items.AddRange(new object[] {
"None=0",
"CloseDoor=1",
"MayEnter=2",
"MayLeave=3",
"NeedEnter=4",
"NeedLeave=5",
"FinishEnter=6",
"FinishLeave=7",
"Arrive=8",
"Ready=9",
"EnterShelf=10"});
this.CboAction4.Location = new System.Drawing.Point(330, 105);
this.CboAction4.Name = "CboAction4";
this.CboAction4.Size = new System.Drawing.Size(100, 20);
this.CboAction4.TabIndex = 30;
//
// BtnSend4
//
this.BtnSend4.Location = new System.Drawing.Point(542, 104);
this.BtnSend4.Name = "BtnSend4";
this.BtnSend4.Size = new System.Drawing.Size(100, 21);
this.BtnSend4.TabIndex = 29;
this.BtnSend4.Text = "Send";
this.BtnSend4.UseVisualStyleBackColor = true;
this.BtnSend4.Click += new System.EventHandler(this.BtnSend_Click);
//
// TxtName4
//
this.TxtName4.Location = new System.Drawing.Point(12, 105);
this.TxtName4.Name = "TxtName4";
this.TxtName4.Size = new System.Drawing.Size(100, 21);
this.TxtName4.TabIndex = 28;
//
// CboLevel3
//
this.CboLevel3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboLevel3.FormattingEnabled = true;
this.CboLevel3.Items.AddRange(new object[] {
"Low=0",
"Middle=1",
"High=2"});
this.CboLevel3.Location = new System.Drawing.Point(436, 78);
this.CboLevel3.Name = "CboLevel3";
this.CboLevel3.Size = new System.Drawing.Size(100, 20);
this.CboLevel3.TabIndex = 27;
//
// TxtRFID3
//
this.TxtRFID3.Location = new System.Drawing.Point(224, 78);
this.TxtRFID3.Name = "TxtRFID3";
this.TxtRFID3.Size = new System.Drawing.Size(100, 21);
this.TxtRFID3.TabIndex = 26;
//
// TxtMark3
//
this.TxtMark3.Location = new System.Drawing.Point(118, 78);
this.TxtMark3.Name = "TxtMark3";
this.TxtMark3.Size = new System.Drawing.Size(100, 21);
this.TxtMark3.TabIndex = 25;
//
// CboAction3
//
this.CboAction3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.CboAction3.FormattingEnabled = true;
this.CboAction3.Items.AddRange(new object[] {
"None=0",
"CloseDoor=1",
"MayEnter=2",
"MayLeave=3",
"NeedEnter=4",
"NeedLeave=5",
"FinishEnter=6",
"FinishLeave=7",
"Arrive=8",
"Ready=9",
"EnterShelf=10"});
this.CboAction3.Location = new System.Drawing.Point(330, 78);
this.CboAction3.Name = "CboAction3";
this.CboAction3.Size = new System.Drawing.Size(100, 20);
this.CboAction3.TabIndex = 24;
//
// BtnSend3
//
this.BtnSend3.Location = new System.Drawing.Point(542, 78);
this.BtnSend3.Name = "BtnSend3";
this.BtnSend3.Size = new System.Drawing.Size(100, 21);
this.BtnSend3.TabIndex = 23;
this.BtnSend3.Text = "Send";
this.BtnSend3.UseVisualStyleBackColor = true;
this.BtnSend3.Click += new System.EventHandler(this.BtnSend_Click);
//
// TxtName3
//
this.TxtName3.Location = new System.Drawing.Point(12, 78);
this.TxtName3.Name = "TxtName3";
this.TxtName3.Size = new System.Drawing.Size(100, 21);
this.TxtName3.TabIndex = 22;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(654, 287);
this.Controls.Add(this.CboLevel4);
this.Controls.Add(this.TxtRFID4);
this.Controls.Add(this.TxtMark4);
this.Controls.Add(this.CboAction4);
this.Controls.Add(this.BtnSend4);
this.Controls.Add(this.TxtName4);
this.Controls.Add(this.CboLevel3);
this.Controls.Add(this.TxtRFID3);
this.Controls.Add(this.TxtMark3);
this.Controls.Add(this.CboAction3);
this.Controls.Add(this.BtnSend3);
this.Controls.Add(this.TxtName3);
this.Controls.Add(this.CboLevel2);
this.Controls.Add(this.TxtRFID2);
this.Controls.Add(this.TxtMark2);
this.Controls.Add(this.CboAction2);
this.Controls.Add(this.BtnSend2);
this.Controls.Add(this.TxtName2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.TxtLog);
this.Controls.Add(this.label5);
this.Controls.Add(this.CboLevel1);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.TxtRFID1);
this.Controls.Add(this.label2);
this.Controls.Add(this.TxtMark1);
this.Controls.Add(this.label1);
this.Controls.Add(this.CboAction1);
this.Controls.Add(this.BtnSend1);
this.Controls.Add(this.TxtName1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox TxtName1;
private System.Windows.Forms.Button BtnSend1;
private System.Windows.Forms.ComboBox CboAction1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox TxtMark1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox TxtRFID1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ComboBox CboLevel1;
private System.Windows.Forms.TextBox TxtLog;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ComboBox CboLevel2;
private System.Windows.Forms.TextBox TxtRFID2;
private System.Windows.Forms.TextBox TxtMark2;
private System.Windows.Forms.ComboBox CboAction2;
private System.Windows.Forms.Button BtnSend2;
private System.Windows.Forms.TextBox TxtName2;
private System.Windows.Forms.ComboBox CboLevel4;
private System.Windows.Forms.TextBox TxtRFID4;
private System.Windows.Forms.TextBox TxtMark4;
private System.Windows.Forms.ComboBox CboAction4;
private System.Windows.Forms.Button BtnSend4;
private System.Windows.Forms.TextBox TxtName4;
private System.Windows.Forms.ComboBox CboLevel3;
private System.Windows.Forms.TextBox TxtRFID3;
private System.Windows.Forms.TextBox TxtMark3;
private System.Windows.Forms.ComboBox CboAction3;
private System.Windows.Forms.Button BtnSend3;
private System.Windows.Forms.TextBox TxtName3;
}
}
......@@ -21,52 +21,18 @@ namespace AgvClientTest
private void Form1_Load(object sender, EventArgs e)
{
CboAction1.SelectedIndex = 0;
CboLevel1.SelectedIndex = 0;
CboAction2.SelectedIndex = 0;
CboLevel2.SelectedIndex = 0;
CboAction3.SelectedIndex = 0;
CboLevel3.SelectedIndex = 0;
CboAction4.SelectedIndex = 0;
CboLevel4.SelectedIndex = 0;
client = new Asa.AgvClient("127.0.0.1");
client.SetStatus("A1");
client.SetStatus("B1");
client.Log += Client_Log;
Asa.ClientNode node = new Asa.ClientNode();
node.Name = "abc";
}
private void Client_Log(string s)
{
Invoke(new Action(() => { TxtLog.AppendText(s + "\r\n"); }));
client = new Asa.AgvClient("");
client.Decode(new byte[] { 1 });
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(client.IsConn)
client.Close();
}
private void BtnSend_Click(object sender, EventArgs e)
{
string id = (sender as Button).Name.Replace("BtnSend", "");
string name = Controls["TxtName" + id].Text;
string mark = Controls["TxtMark" + id].Text;
string rfid = Controls["TxtRFID" + id].Text;
int action = ((ComboBox)Controls["CboAction" + id]).SelectedIndex;
int level = ((ComboBox)Controls["CboLevel" + id]).SelectedIndex;
client.SetStatus(name, mark, rfid, (Asa.ClientAction)action, (Asa.ClientLevel)level);
}
private void button2_Click(object sender, EventArgs e)
{
client.Connect();
}
private void button3_Click(object sender, EventArgs e)
{
client.Close();
}
}
}
e9f6f33ab4bd66774ad14211cfa560527e0a7c54
04752e378157393bb9ae825befb853bbaba9c5f7
......@@ -36,3 +36,17 @@ D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClientTest\obj
D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClientTest\obj\Debug\AgvClientTest.csproj.CopyComplete
D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClientTest\obj\Debug\AgvClientTest.exe
D:\OneDrive - 上海挚锦科技有限公司\SMD\ControlCenter\AgvClientTest\obj\Debug\AgvClientTest.pdb
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.csprojAssemblyReference.cache
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.Form1.resources
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.Properties.Resources.resources
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.csproj.GenerateResource.cache
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.csproj.CoreCompileInputs.cache
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.exe
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.pdb
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\bin\Debug\AgvClientTest.exe.config
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\bin\Debug\AgvClientTest.exe
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\bin\Debug\AgvClientTest.pdb
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\bin\Debug\Client.dll
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\bin\Debug\log4net.dll
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\bin\Debug\Client.pdb
D:\OneDrive - 上海挚锦科技有限公司\SMD\AGVControl\AgvClientTest\obj\Debug\AgvClientTest.csproj.CopyComplete
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!