123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using AGV_WPF.DLL;
- using AGV_WPF.Model;
- using AGV_WPF.Models;
- using AGV_WPF_Global;
- using WCS.API.WebApiClient;
- namespace AGV_WPF.Tools
- {
- public class DispatcherTool
- {
- public const int COM_FUNC = 1;
- public const int COM_ADDRESS = 3;
- public const int COM_ROUTE = 7;
- public const int COM_MARK = 8;
- public const int COM_AGV = 11;
- public const int COM_CHECK = 12;
- public static byte[] CRC16(byte[] data)
- {
- int len = data.Length;
- if (len > 0)
- {
- ushort crc = 0xFFFF;
- for (int i = 0; i < len; i++)
- {
- crc = (ushort)(crc ^ (data[i]));
- for (int j = 0; j < 8; j++)
- {
- crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
- }
- }
- byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
- byte lo = (byte)(crc & 0x00FF); //低位置
- return new byte[] { lo, hi };
- }
- return new byte[] { 0, 0 };
- }
- PLCSqlDLL PLCSql = new PLCSqlDLL();
- public AGVInfo GetStationInfo(string statiom)
- {
- string mark = "";
- AGVInfo Agvinfo = new AGVInfo();
- try
- {
- // while (AGVPosW)
- {
- List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
- List<AGVInfoModel> results = listModel.FindAll(o => o.MARKNUM.ToString() == statiom && o.ConnectStatus == true); //停止点有车
- if (results != null && results.Count > 0)
- {
- foreach (AGVInfoModel info in results)
- {
- if (info != null)
- {
- PLCSql.PLCMESSelect(info.AGVNUM.ToString(), "2", ref Agvinfo);
- }
- }
- }
- Thread.Sleep(500);
- } //AGVpos为false代表未叫车 才进循环
- }
- catch (Exception ex)
- {
- }
- return Agvinfo;
- }
- public static void AgvReadOnStop(int mark, int route,string ip)
- {
- byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
- buffer[COM_FUNC] = 0x35;
-
- int Route = 0x3030 + route;
- buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
- buffer[COM_ROUTE] = (byte)(Route & 0xFF);
- buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
- buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
- buffer[COM_CHECK] = XOR_Check(buffer, 12);
- IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), 8899) ;
- // if (!GlobalPara.SendBytesList.Contains(buffer))
- {
- for (int i = 0; i < 1; i++)
- {
- // GlobalPara.SendBytesList.Add(buffer);
- GlobalPara.communication.SendData(buffer, endPoint);
- System.Threading.Thread.Sleep(100);
- }
- }
- }
- public static void AgvReleasedOnStop(int mark, int route,string ip)
- {
- byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
- buffer[COM_FUNC] = 0x38;
-
- int Route = 0x3030 + route;
- buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
- buffer[COM_ROUTE] = (byte)(Route & 0xFF);
- buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
- buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
- buffer[COM_CHECK] = XOR_Check(buffer, 12);
- IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), 8899);
- // if (!GlobalPara.SendBytesList.Contains(buffer))
- {
- for (int i = 0; i < 1; i++)
- {
- // GlobalPara.SendBytesList.Add(buffer);
- GlobalPara.communication.SendData(buffer, endPoint);
- System.Threading.Thread.Sleep(100);
- }
- }
- }
- public static void AgvReadOnStop(int mark, int route, int targetPos = 0, string ip = null)
- {
- byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
- buffer[COM_FUNC] = 0x35;
- buffer[4] = (byte)((targetPos >> 8) & 0xFF);
- buffer[5] = (byte)(targetPos & 0xFF);
- int Route = 0x3030 + route;
- buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
- buffer[COM_ROUTE] = (byte)(Route & 0xFF);
- buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
- buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
- buffer[COM_CHECK] = XOR_Check(buffer, 12);
- // if (!GlobalPara.SendBytesList.Contains(buffer))
- {
- for (int i = 0; i < 1; i++)
- {
- // GlobalPara.SendBytesList.Add(buffer);
- if (ip == null)
- {
- GlobalPara.communication.SendData(buffer, null);
- }
- else
- {
- IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), 8899);
- GlobalPara.communication.SendData(buffer, iPEndPoint);
- }
- System.Threading.Thread.Sleep(100);
- }
- }
- }
- public static void AgvReleasedOnStop(int mark, int route, int targetPos = 0, string ip = null)
- {
- byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
- buffer[COM_FUNC] = 0x38;
- buffer[4] = (byte)((targetPos >> 8) & 0xFF);
- buffer[5] = (byte)(targetPos & 0xFF);
- int Route = 0x3030 + route;
- buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
- buffer[COM_ROUTE] = (byte)(Route & 0xFF);
- buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
- buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
- buffer[COM_CHECK] = XOR_Check(buffer, 12);
- // if (!GlobalPara.SendBytesList.Contains(buffer))
- {
- for (int i = 0; i < 1; i++)
- {
- // GlobalPara.SendBytesList.Add(buffer);
- if (ip == null)
- {
- GlobalPara.communication.SendData(buffer, null);
- }
- else
- {
- IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), 8899);
- GlobalPara.communication.SendData(buffer, iPEndPoint);
- }
- System.Threading.Thread.Sleep(100);
- }
- }
- }
- public static byte XOR_Check(byte[] buffer, int count, int index = 0)
- {
- byte b = 0;
- for (int i = 0; i < count; i++)
- {
- b ^= buffer[index + i];
- }
- return b;
- }
- public static string ByteToHexString(byte[] buffer)
- {
- StringBuilder builder = new StringBuilder(buffer.Length * 3);
- foreach (byte data in buffer)
- {
- builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
- }
- return builder.ToString().ToUpper();
- }
- public static void ReleaseAgvOnStop(int mark, int route, int targetPos = 0,int times=3)
- {
- byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
- buffer[COM_FUNC] = 0x37;
- buffer[4] = (byte)((targetPos >> 8) & 0xFF);
- buffer[5] = (byte)(targetPos & 0xFF);
-
- int Route = 0x3030 + route;
- buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
- buffer[COM_ROUTE] = (byte)(Route & 0xFF);
- buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
- buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
- buffer[COM_CHECK] = XOR_Check(buffer, 12);
- // if (!GlobalPara.SendBytesList.Contains(buffer))
- {
- for (int i = 0; i < times; i++)
- {
- GlobalPara.communication.SendData(buffer,null);
- System.Threading.Thread.Sleep(20);
- }
- }
- }
- /// <summary>
- /// 控制AGV暂停或者放行
- /// </summary>
- /// <param name="agvID">AGV编号</param>
- /// <param name="iOperation">0是启动,1是放行</param>
- public static void ControlAGVBy1071(int agvID, int iOperation,int times=1)
- {
- byte[] buffer = new byte[10] { 0x10,0x71,0, 0, 0, 0, 0,0,0 ,0x03};
- buffer[2] = (byte)(agvID);
- if (iOperation >= 0 && iOperation < 8)
- {
- buffer[3] = Convert.ToByte((1 << iOperation) | 0x00);
- }
- buffer[8] = XOR_Check(buffer, 8);
- // if (!GlobalPara.SendBytesList.Contains(buffer))
- {
- for (int i = 0; i < times; i++)
- {
- GlobalPara.communication.SendData(buffer, null);
- System.Threading.Thread.Sleep(20);
- }
- }
- }
- public static void AlarmStartOrStop(int flag, int number, string ipAddress = null)
- {
- byte[] buffer = new byte[16] { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- buffer[4] = (byte)(flag);
- buffer[14] = (byte)(number);
- buffer[15] = XOR_Check(buffer, 15);
- for (int i = 0; i < 1; i++)
- {
- if (string.IsNullOrEmpty(ipAddress))
- {
- GlobalPara.communication.SendData(buffer, null);
- }
- else
- {
- GlobalPara.communication.SendData(buffer, new IPEndPoint(IPAddress.Parse(ipAddress), 8899));
- }
- System.Threading.Thread.Sleep(20);
- }
- // for (int i = 0; i < 10; i++)
- {
- // port.Write(buffer, 0, buffer.Length);
- // Thread.Sleep(50);
- }
- if (true)
- {
- #region 写日志
- //string str = "结束点放行AGV(ReleaseAgv), 结束点地标: " + mark.ToString() + "路线: " + route.ToString() + "\n";
- string str = "";
- for (int i = 0; i < buffer.Length; i++)
- str += Convert.ToString(buffer[i], 16).ToString() + " ";
- Console.WriteLine(str);
- #endregion
- }
- }
- public static void AlarmStartOrStop(int flag, int number)
- {
- byte[] buffer = new byte[16] { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
- buffer[4] = (byte)(flag);
- buffer[14] = (byte)(number);
- buffer[15] = XOR_Check(buffer, 15);
- for (int i = 0; i < 1; i++)
- {
- GlobalPara.communication.SendData(buffer, null);
- System.Threading.Thread.Sleep(20);
- }
- // for (int i = 0; i < 10; i++)
- {
- // port.Write(buffer, 0, buffer.Length);
- // Thread.Sleep(50);
- }
- if (true)
- {
- #region 写日志
- //string str = "结束点放行AGV(ReleaseAgv), 结束点地标: " + mark.ToString() + "路线: " + route.ToString() + "\n";
- string str = "";
- for (int i = 0; i < buffer.Length; i++)
- str += Convert.ToString(buffer[i], 16).ToString() + " ";
- Console.WriteLine(str);
- #endregion
- }
- }
- /// <summary>
- /// 控制全部AGV休眠 唤醒 启动 暂停
- /// </summary>
- /// <param name="function">0x20休眠,0x30唤醒,0x40启动,0x50暂停</param>
- /// <param name="area"></param>
- /// <param name="agvnum"></param>
- /// <param name="isSingle">是否单台操作</param>
- /// <param name="agvmark"></param>
- /// <param name="agvroute"></param>
- public static void ControlAllAgv(int function, int area=0, int agvnum = 0,bool isSingle=false, int agvmark = 0, int agvroute = 0)
- {
- byte[] buffer = new byte[14] { 0xAA, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0, 0, 0xAB };
- buffer[2] = (byte)function;
- if (isSingle)
- {
- buffer[3] = (byte)(area);//|0x80 //对单台
- }
- else
- {
- buffer[3] = (byte)(area | 0x80);//对所有
- }
- if (agvnum != 0)
- {
- buffer[4] = (byte)((agvnum & 0xff00) >> 8);
- buffer[5] = (byte)(agvnum & 0x00ff);
- }
- if (agvmark != 0)
- {
- buffer[6] = (byte)((agvmark & 0xff00) >> 8);
- buffer[7] = (byte)(agvmark & 0x00ff);
- }
- if (agvroute != 0)
- {
- buffer[8] = (byte)((agvroute & 0xff00) >> 8);
- buffer[9] = (byte)(agvroute & 0x00ff);
- }
- byte checkSum = 0x00;
- for (int i = 0; i < 12; i++)//异或校验
- {
- checkSum^= buffer[i];
- }
- buffer[12] = checkSum;
- // if (!GlobalPara.SendBytesList.Contains(buffer))
- {
- for (int i = 0; i < 5; i++)
- {
- GlobalPara.communication.SendData(buffer, null);
- System.Threading.Thread.Sleep(20);
- }
- }
- }
- }
- }
|