YaskawaNxcAlignerConnection.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using MECF.Framework.Common.Communications;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Aligners.YaskawaAligner
  10. {
  11. public class YaskawaNxcAlignerMessage : AsciiMessage
  12. {
  13. public int UNo { get; set; }
  14. public int SeqNo { get; set; }
  15. public string Status { get; set; }
  16. public string Ackcd { get; set; }
  17. public string Command { get; set; }
  18. public string Data { get; set; }
  19. public string ErrorCode { get; set; }
  20. public string EvNo { get; set; }
  21. public string EvDate { get; set; }
  22. public string EvData { get; set; }
  23. }
  24. public class YaskawaNxcAlignerConnection : TCPPortConnectionBase
  25. {
  26. private YaskawaNxcAligner _aligner;
  27. public YaskawaNxcAlignerConnection(YaskawaNxcAligner pa, string ipaddress)
  28. : base(ipaddress, "\r", true)
  29. {
  30. _aligner = pa;
  31. }
  32. protected override MessageBase ParseResponse(string rawMessage)
  33. {
  34. YaskawaNxcAlignerMessage msg = new YaskawaNxcAlignerMessage();
  35. try
  36. {
  37. msg.RawMessage = rawMessage.Replace("\r", "");
  38. string msgdata = rawMessage.Replace("\r", "");
  39. string startTag = msgdata.Substring(0, 1);
  40. msg.UNo = Convert.ToInt16(msgdata.Substring(1, 1));
  41. msg.IsAck = false;
  42. msg.IsComplete = false;
  43. msg.IsFormatError = false;
  44. msg.IsEvent = false;
  45. if (startTag == "$")
  46. {
  47. msg.IsAck = true;
  48. msg.IsComplete = false;
  49. }
  50. if (startTag == "?")
  51. {
  52. msg.IsFormatError = true;
  53. }
  54. if (startTag == ">" && msgdata.Substring(4, 4) == "EVNT")
  55. {
  56. msg.IsEvent = true;
  57. msg.EvNo = msgdata.Substring(8, 3);
  58. msg.EvData = msgdata.Substring(11, msgdata.Length - 13);
  59. msg.EvDate = DateTime.Now.ToString();
  60. return msg;
  61. }
  62. if (startTag == "!")
  63. {
  64. SendAck();
  65. msg.IsComplete = true;
  66. }
  67. if (_aligner.IsEnableSeqNo)
  68. {
  69. msg.SeqNo = Convert.ToInt16(msgdata.Substring(2, 2));
  70. msg.Status = msgdata.Substring(4, 2);
  71. msg.Data = msgdata.Substring(4, msgdata.Length - 6);
  72. if (msg.IsAck)
  73. {
  74. msg.Ackcd = msgdata.Substring(6, 4);
  75. if (msg.Ackcd != "0000")
  76. {
  77. _aligner.OnError($"Execution error,Ack code is {msg.ErrorCode}");
  78. }
  79. }
  80. if (msg.IsComplete)
  81. {
  82. msg.ErrorCode = msgdata.Substring(6, 4);
  83. if (msg.ErrorCode != "0000")
  84. {
  85. _aligner.OnError($"Execution error,Error code is {msg.ErrorCode}");
  86. }
  87. }
  88. //msg.Command = msgdata[5];
  89. //msg.Data = msgdata.Skip(6).Take(msgdata.Length -6 - (_robot.IsEnableCheckSum?1:0)).ToArray();
  90. }
  91. else
  92. {
  93. //msg.SeqNo = Convert.ToInt16(msgdata.Substring(2, 2));
  94. msg.Status = msgdata.Substring(2, 2);
  95. msg.Data = msgdata.Substring(2, msgdata.Length - 4);
  96. if (msg.IsAck)
  97. {
  98. msg.Ackcd = msgdata.Substring(4, 4);
  99. if (msg.Ackcd != "0000")
  100. {
  101. _aligner.OnError($"Execution error,Ack code is {msg.ErrorCode}");
  102. }
  103. }
  104. if (msg.IsComplete)
  105. {
  106. msg.ErrorCode = msgdata.Substring(4, 4);
  107. if (msg.ErrorCode != "0000")
  108. {
  109. _aligner.OnError($"Execution error,Error code is {msg.ErrorCode}");
  110. }
  111. }
  112. }
  113. _aligner.ParseStatus(msg.Status);
  114. return msg;
  115. }
  116. catch (Exception ex)
  117. {
  118. LOG.Write(ex);
  119. msg.IsFormatError = true;
  120. return msg;
  121. }
  122. }
  123. protected override void OnEventArrived(MessageBase msg)
  124. {
  125. YaskawaNxcAlignerMessage evt = msg as YaskawaNxcAlignerMessage;
  126. if (evt.EvNo == "100")
  127. {
  128. string errocode = evt.EvData.Substring(1, evt.EvData.Length - 1);
  129. _aligner.NotifyAlarmByErrorCode(errocode);
  130. if (Convert.ToInt32(errocode,16) > 0xF0)
  131. _aligner.OnError($"Robot occurred error:{evt.EvData} on {evt.EvDate}.");
  132. else
  133. EV.PostWarningLog("Robot", $"Robot occurred error:{evt.EvData} on {evt.EvDate}.");
  134. }
  135. if (evt.EvNo == "140")
  136. {
  137. }
  138. }
  139. public void SendAck()
  140. {
  141. if (_aligner.IsEnableSeqNo)
  142. {
  143. _aligner.CurrentSeqNo = _aligner.SeqnoGenerator.create();
  144. _aligner.SeqnoGenerator.release(_aligner.CurrentSeqNo);
  145. }
  146. string commandstr = $"{_aligner.UnitNumber}" + (_aligner.IsEnableSeqNo ? $"{_aligner.CurrentSeqNo:D2}" : "") + "ACKN";
  147. if (_aligner.IsEnableCheckSum)
  148. {
  149. commandstr += "";
  150. commandstr = commandstr + Checksum(Encoding.ASCII.GetBytes(commandstr)) + "\r";
  151. }
  152. commandstr = "$" + commandstr;
  153. SendMessage(commandstr);
  154. }
  155. private string Checksum(byte[] bytes)
  156. {
  157. int sum = 0;
  158. foreach (byte code in bytes)
  159. {
  160. sum += code;
  161. }
  162. string hex = String.Format("{0:X2}", sum % 256);
  163. return hex;
  164. }
  165. }
  166. }