YaskawaNXCRobotConnection.cs 6.5 KB

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