YaskawaRobotConnection.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 YaskawaRobotMessage: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 YaskawaRobotConnection: TCPPortConnectionBase
  26. {
  27. private YaskawaSR100Robot _robot;
  28. public YaskawaRobotConnection(YaskawaSR100Robot robot, string ipaddress)
  29. : base(ipaddress, "\r", true)
  30. {
  31. _robot = robot;
  32. }
  33. protected override MessageBase ParseResponse(string rawMessage)
  34. {
  35. YaskawaRobotMessage msg = new YaskawaRobotMessage();
  36. try
  37. {
  38. msg.RawMessage = rawMessage.Replace("\r","");
  39. string[] msgdata = rawMessage.Replace("\r", "").Split(',');
  40. msg.UNo = Convert.ToInt16(msgdata[1]);
  41. msg.IsAck = false;
  42. msg.IsComplete = false;
  43. msg.IsFormatError = false;
  44. msg.IsEvent = false;
  45. if (msgdata[0] == "$")
  46. {
  47. msg.IsAck = true;
  48. msg.IsComplete = false;
  49. }
  50. if (msgdata[0] == "?")
  51. {
  52. msg.IsFormatError = true;
  53. msg.ErrorCode = msgdata[1];
  54. return msg;
  55. }
  56. if (msgdata[0] == ">" && msgdata[2] == "EVNT")
  57. {
  58. msg.IsEvent = true;
  59. msg.EvNo = msgdata[3];
  60. msg.EvDate = msgdata[4];
  61. msg.EvData = msgdata[5];
  62. return msg;
  63. }
  64. if (_robot.IsEnableSeqNo)
  65. {
  66. msg.SeqNo = Convert.ToInt16(msgdata[2]);
  67. msg.Status = msgdata[3];
  68. if(msg.IsAck)
  69. msg.Ackcd = msgdata[4];
  70. msg.Command = msgdata[5];
  71. msg.Data = msgdata.Skip(6).Take(msgdata.Length -6 - (_robot.IsEnableCheckSum?1:0)).ToArray();
  72. }
  73. else
  74. {
  75. msg.Status = msgdata[2];
  76. if (msg.IsAck)
  77. msg.Ackcd = msgdata[3];
  78. msg.Command = msgdata[4];
  79. msg.Data = msgdata.Skip(5).Take(msgdata.Length - 5 - (_robot.IsEnableCheckSum ? 1 : 0)).ToArray();
  80. }
  81. if (msgdata[0] == "!")
  82. {
  83. SendAck();
  84. msg.IsComplete = true;
  85. if (_robot.IsEnableSeqNo)
  86. {
  87. msg.ErrorCode = msgdata[4];
  88. }
  89. else
  90. msg.ErrorCode = msgdata[3];
  91. if (msg.ErrorCode != "0000")
  92. {
  93. _robot.OnError($"Execution error,Error code is {msg.ErrorCode}");
  94. }
  95. }
  96. _robot.ParseStatus(msg.Status);
  97. return msg;
  98. }
  99. catch(Exception ex)
  100. {
  101. LOG.Write(ex);
  102. msg.IsFormatError = true;
  103. return msg;
  104. }
  105. }
  106. protected override void OnEventArrived(MessageBase msg)
  107. {
  108. YaskawaRobotMessage evt = msg as YaskawaRobotMessage;
  109. if(evt.EvNo == "100")
  110. {
  111. string errocode = evt.EvData.Substring(1, evt.EvData.Length-1);
  112. _robot.NotifyAlarmByErrorCode(errocode.ToUpper());
  113. if(Convert.ToInt32(errocode,16) > 0xF0)
  114. _robot.OnError($"Robot occurred error:{evt.EvData} on {evt.EvDate}.");
  115. else
  116. EV.PostWarningLog("Robot", $"Robot occurred error:{evt.EvData} on {evt.EvDate}.");
  117. }
  118. if(evt.EvNo == "140")
  119. {
  120. }
  121. }
  122. public void SendAck()
  123. {
  124. if (_robot.IsEnableSeqNo)
  125. {
  126. _robot.CurrentSeqNo = _robot.SeqnoGenerator.create();
  127. _robot.SeqnoGenerator.release(_robot.CurrentSeqNo);
  128. }
  129. string commandstr = $",{_robot.UnitNumber}" + (_robot.IsEnableSeqNo? $",{_robot.CurrentSeqNo:D2}":"") + ",ACKN";
  130. if (_robot.IsEnableCheckSum)
  131. {
  132. commandstr += ",";
  133. commandstr = commandstr + Checksum(Encoding.ASCII.GetBytes(commandstr)) + "\r";
  134. }
  135. commandstr = "$" + commandstr;
  136. SendMessage(commandstr);
  137. }
  138. private string Checksum(byte[] bytes)
  139. {
  140. int sum = 0;
  141. foreach (byte code in bytes)
  142. {
  143. sum += code;
  144. }
  145. string hex = String.Format("{0:X2}", sum % 256);
  146. return hex;
  147. }
  148. }
  149. }