DispatcherTool.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Threading;
  7. using AGV_WPF.DLL;
  8. using AGV_WPF.Model;
  9. using AGV_WPF.Models;
  10. using AGV_WPF_Global;
  11. using WCS.API.WebApiClient;
  12. namespace AGV_WPF.Tools
  13. {
  14. public class DispatcherTool
  15. {
  16. public const int COM_FUNC = 1;
  17. public const int COM_ADDRESS = 3;
  18. public const int COM_ROUTE = 7;
  19. public const int COM_MARK = 8;
  20. public const int COM_AGV = 11;
  21. public const int COM_CHECK = 12;
  22. public static byte[] CRC16(byte[] data)
  23. {
  24. int len = data.Length;
  25. if (len > 0)
  26. {
  27. ushort crc = 0xFFFF;
  28. for (int i = 0; i < len; i++)
  29. {
  30. crc = (ushort)(crc ^ (data[i]));
  31. for (int j = 0; j < 8; j++)
  32. {
  33. crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
  34. }
  35. }
  36. byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
  37. byte lo = (byte)(crc & 0x00FF); //低位置
  38. return new byte[] { lo, hi };
  39. }
  40. return new byte[] { 0, 0 };
  41. }
  42. PLCSqlDLL PLCSql = new PLCSqlDLL();
  43. public AGVInfo GetStationInfo(string statiom)
  44. {
  45. string mark = "";
  46. AGVInfo Agvinfo = new AGVInfo();
  47. try
  48. {
  49. // while (AGVPosW)
  50. {
  51. List<AGVInfoModel> listModel = GlobalPara.AgvInfoDic.Values.ToList();
  52. List<AGVInfoModel> results = listModel.FindAll(o => o.MARKNUM.ToString() == statiom && o.ConnectStatus == true); //停止点有车
  53. if (results != null && results.Count > 0)
  54. {
  55. foreach (AGVInfoModel info in results)
  56. {
  57. if (info != null)
  58. {
  59. PLCSql.PLCMESSelect(info.AGVNUM.ToString(), "2", ref Agvinfo);
  60. }
  61. }
  62. }
  63. Thread.Sleep(500);
  64. } //AGVpos为false代表未叫车 才进循环
  65. }
  66. catch (Exception ex)
  67. {
  68. }
  69. return Agvinfo;
  70. }
  71. public static void AgvReadOnStop(int mark, int route,string ip)
  72. {
  73. byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
  74. buffer[COM_FUNC] = 0x35;
  75. int Route = 0x3030 + route;
  76. buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
  77. buffer[COM_ROUTE] = (byte)(Route & 0xFF);
  78. buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
  79. buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
  80. buffer[COM_CHECK] = XOR_Check(buffer, 12);
  81. IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), 8899) ;
  82. // if (!GlobalPara.SendBytesList.Contains(buffer))
  83. {
  84. for (int i = 0; i < 1; i++)
  85. {
  86. // GlobalPara.SendBytesList.Add(buffer);
  87. GlobalPara.communication.SendData(buffer, endPoint);
  88. System.Threading.Thread.Sleep(100);
  89. }
  90. }
  91. }
  92. public static void AgvReleasedOnStop(int mark, int route,string ip)
  93. {
  94. byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
  95. buffer[COM_FUNC] = 0x38;
  96. int Route = 0x3030 + route;
  97. buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
  98. buffer[COM_ROUTE] = (byte)(Route & 0xFF);
  99. buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
  100. buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
  101. buffer[COM_CHECK] = XOR_Check(buffer, 12);
  102. IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), 8899);
  103. // if (!GlobalPara.SendBytesList.Contains(buffer))
  104. {
  105. for (int i = 0; i < 1; i++)
  106. {
  107. // GlobalPara.SendBytesList.Add(buffer);
  108. GlobalPara.communication.SendData(buffer, endPoint);
  109. System.Threading.Thread.Sleep(100);
  110. }
  111. }
  112. }
  113. public static void AgvReadOnStop(int mark, int route, int targetPos = 0, string ip = null)
  114. {
  115. byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
  116. buffer[COM_FUNC] = 0x35;
  117. buffer[4] = (byte)((targetPos >> 8) & 0xFF);
  118. buffer[5] = (byte)(targetPos & 0xFF);
  119. int Route = 0x3030 + route;
  120. buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
  121. buffer[COM_ROUTE] = (byte)(Route & 0xFF);
  122. buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
  123. buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
  124. buffer[COM_CHECK] = XOR_Check(buffer, 12);
  125. // if (!GlobalPara.SendBytesList.Contains(buffer))
  126. {
  127. for (int i = 0; i < 1; i++)
  128. {
  129. // GlobalPara.SendBytesList.Add(buffer);
  130. if (ip == null)
  131. {
  132. GlobalPara.communication.SendData(buffer, null);
  133. }
  134. else
  135. {
  136. IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), 8899);
  137. GlobalPara.communication.SendData(buffer, iPEndPoint);
  138. }
  139. System.Threading.Thread.Sleep(100);
  140. }
  141. }
  142. }
  143. public static void AgvReleasedOnStop(int mark, int route, int targetPos = 0, string ip = null)
  144. {
  145. byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
  146. buffer[COM_FUNC] = 0x38;
  147. buffer[4] = (byte)((targetPos >> 8) & 0xFF);
  148. buffer[5] = (byte)(targetPos & 0xFF);
  149. int Route = 0x3030 + route;
  150. buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
  151. buffer[COM_ROUTE] = (byte)(Route & 0xFF);
  152. buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
  153. buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
  154. buffer[COM_CHECK] = XOR_Check(buffer, 12);
  155. // if (!GlobalPara.SendBytesList.Contains(buffer))
  156. {
  157. for (int i = 0; i < 1; i++)
  158. {
  159. // GlobalPara.SendBytesList.Add(buffer);
  160. if (ip == null)
  161. {
  162. GlobalPara.communication.SendData(buffer, null);
  163. }
  164. else
  165. {
  166. IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), 8899);
  167. GlobalPara.communication.SendData(buffer, iPEndPoint);
  168. }
  169. System.Threading.Thread.Sleep(100);
  170. }
  171. }
  172. }
  173. public static byte XOR_Check(byte[] buffer, int count, int index = 0)
  174. {
  175. byte b = 0;
  176. for (int i = 0; i < count; i++)
  177. {
  178. b ^= buffer[index + i];
  179. }
  180. return b;
  181. }
  182. public static string ByteToHexString(byte[] buffer)
  183. {
  184. StringBuilder builder = new StringBuilder(buffer.Length * 3);
  185. foreach (byte data in buffer)
  186. {
  187. builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
  188. }
  189. return builder.ToString().ToUpper();
  190. }
  191. public static void ReleaseAgvOnStop(int mark, int route, int targetPos = 0,int times=3)
  192. {
  193. byte[] buffer = new byte[14] { 0x10, 0, 0x30, 0x30, 0x30, 0x50, 0x30, 0x30, 0, 0, 0, 0, 0, 0x03 };
  194. buffer[COM_FUNC] = 0x37;
  195. buffer[4] = (byte)((targetPos >> 8) & 0xFF);
  196. buffer[5] = (byte)(targetPos & 0xFF);
  197. int Route = 0x3030 + route;
  198. buffer[COM_ROUTE - 1] = (byte)((Route >> 8) & 0xFF);
  199. buffer[COM_ROUTE] = (byte)(Route & 0xFF);
  200. buffer[COM_MARK] = (byte)((mark & 0xff00) >> 8);
  201. buffer[COM_MARK + 1] = (byte)(mark & 0x00ff);
  202. buffer[COM_CHECK] = XOR_Check(buffer, 12);
  203. // if (!GlobalPara.SendBytesList.Contains(buffer))
  204. {
  205. for (int i = 0; i < times; i++)
  206. {
  207. GlobalPara.communication.SendData(buffer,null);
  208. System.Threading.Thread.Sleep(20);
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// 控制AGV暂停或者放行
  214. /// </summary>
  215. /// <param name="agvID">AGV编号</param>
  216. /// <param name="iOperation">0是启动,1是放行</param>
  217. public static void ControlAGVBy1071(int agvID, int iOperation,int times=1)
  218. {
  219. byte[] buffer = new byte[10] { 0x10,0x71,0, 0, 0, 0, 0,0,0 ,0x03};
  220. buffer[2] = (byte)(agvID);
  221. if (iOperation >= 0 && iOperation < 8)
  222. {
  223. buffer[3] = Convert.ToByte((1 << iOperation) | 0x00);
  224. }
  225. buffer[8] = XOR_Check(buffer, 8);
  226. // if (!GlobalPara.SendBytesList.Contains(buffer))
  227. {
  228. for (int i = 0; i < times; i++)
  229. {
  230. GlobalPara.communication.SendData(buffer, null);
  231. System.Threading.Thread.Sleep(20);
  232. }
  233. }
  234. }
  235. public static void AlarmStartOrStop(int flag, int number, string ipAddress = null)
  236. {
  237. byte[] buffer = new byte[16] { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  238. buffer[4] = (byte)(flag);
  239. buffer[14] = (byte)(number);
  240. buffer[15] = XOR_Check(buffer, 15);
  241. for (int i = 0; i < 1; i++)
  242. {
  243. if (string.IsNullOrEmpty(ipAddress))
  244. {
  245. GlobalPara.communication.SendData(buffer, null);
  246. }
  247. else
  248. {
  249. GlobalPara.communication.SendData(buffer, new IPEndPoint(IPAddress.Parse(ipAddress), 8899));
  250. }
  251. System.Threading.Thread.Sleep(20);
  252. }
  253. // for (int i = 0; i < 10; i++)
  254. {
  255. // port.Write(buffer, 0, buffer.Length);
  256. // Thread.Sleep(50);
  257. }
  258. if (true)
  259. {
  260. #region 写日志
  261. //string str = "结束点放行AGV(ReleaseAgv), 结束点地标: " + mark.ToString() + "路线: " + route.ToString() + "\n";
  262. string str = "";
  263. for (int i = 0; i < buffer.Length; i++)
  264. str += Convert.ToString(buffer[i], 16).ToString() + " ";
  265. Console.WriteLine(str);
  266. #endregion
  267. }
  268. }
  269. public static void AlarmStartOrStop(int flag, int number)
  270. {
  271. byte[] buffer = new byte[16] { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  272. buffer[4] = (byte)(flag);
  273. buffer[14] = (byte)(number);
  274. buffer[15] = XOR_Check(buffer, 15);
  275. for (int i = 0; i < 1; i++)
  276. {
  277. GlobalPara.communication.SendData(buffer, null);
  278. System.Threading.Thread.Sleep(20);
  279. }
  280. // for (int i = 0; i < 10; i++)
  281. {
  282. // port.Write(buffer, 0, buffer.Length);
  283. // Thread.Sleep(50);
  284. }
  285. if (true)
  286. {
  287. #region 写日志
  288. //string str = "结束点放行AGV(ReleaseAgv), 结束点地标: " + mark.ToString() + "路线: " + route.ToString() + "\n";
  289. string str = "";
  290. for (int i = 0; i < buffer.Length; i++)
  291. str += Convert.ToString(buffer[i], 16).ToString() + " ";
  292. Console.WriteLine(str);
  293. #endregion
  294. }
  295. }
  296. /// <summary>
  297. /// 控制全部AGV休眠 唤醒 启动 暂停
  298. /// </summary>
  299. /// <param name="function">0x20休眠,0x30唤醒,0x40启动,0x50暂停</param>
  300. /// <param name="area"></param>
  301. /// <param name="agvnum"></param>
  302. /// <param name="isSingle">是否单台操作</param>
  303. /// <param name="agvmark"></param>
  304. /// <param name="agvroute"></param>
  305. public static void ControlAllAgv(int function, int area=0, int agvnum = 0,bool isSingle=false, int agvmark = 0, int agvroute = 0)
  306. {
  307. byte[] buffer = new byte[14] { 0xAA, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0, 0, 0xAB };
  308. buffer[2] = (byte)function;
  309. if (isSingle)
  310. {
  311. buffer[3] = (byte)(area);//|0x80 //对单台
  312. }
  313. else
  314. {
  315. buffer[3] = (byte)(area | 0x80);//对所有
  316. }
  317. if (agvnum != 0)
  318. {
  319. buffer[4] = (byte)((agvnum & 0xff00) >> 8);
  320. buffer[5] = (byte)(agvnum & 0x00ff);
  321. }
  322. if (agvmark != 0)
  323. {
  324. buffer[6] = (byte)((agvmark & 0xff00) >> 8);
  325. buffer[7] = (byte)(agvmark & 0x00ff);
  326. }
  327. if (agvroute != 0)
  328. {
  329. buffer[8] = (byte)((agvroute & 0xff00) >> 8);
  330. buffer[9] = (byte)(agvroute & 0x00ff);
  331. }
  332. byte checkSum = 0x00;
  333. for (int i = 0; i < 12; i++)//异或校验
  334. {
  335. checkSum^= buffer[i];
  336. }
  337. buffer[12] = checkSum;
  338. // if (!GlobalPara.SendBytesList.Contains(buffer))
  339. {
  340. for (int i = 0; i < 5; i++)
  341. {
  342. GlobalPara.communication.SendData(buffer, null);
  343. System.Threading.Thread.Sleep(20);
  344. }
  345. }
  346. }
  347. }
  348. }