EfemMessage.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Text.RegularExpressions;
  8. using Aitex.Core.RT.Event;
  9. using Aitex.Sorter.Common;
  10. using MECF.Framework.Common.Equipment;
  11. using VirgoCommon;
  12. namespace VirgoRT.Device.YASKAWA
  13. {
  14. [Serializable]
  15. public sealed class EfemMessage : IEfemMessage, ICloneable
  16. {
  17. public enum MsgHead
  18. {
  19. MOV, GET, ACK, INF, SET, CAN, ABS, EVT, NAK
  20. }
  21. public const char EOF = ';';
  22. public EfemOperation Operation { get; set; }
  23. public MsgHead Head { get; set; }
  24. public ModuleName Port { get; set; }
  25. public IList<string> Data { get; set; }
  26. public MsgDirection Direct { get; set; } = MsgDirection.To;
  27. public IList<string> Parameters { get; set; }
  28. public string RawString { get; set; }
  29. public string Factor { get; set; } // NAK, CAN
  30. public override string ToString()
  31. {
  32. string sPara = ToParamString();
  33. string sOP = EfemConstant.OperationString[Operation];
  34. string context = $"{Head}:{sOP}{sPara}";
  35. context += EOF;
  36. return context;
  37. }
  38. public string ToParamString()
  39. {
  40. string res = string.Empty;
  41. if (Parameters == null || Parameters.Count <= 0)
  42. return res;
  43. foreach (var para in Parameters)
  44. {
  45. res += '/' + para;
  46. }
  47. return res;
  48. }
  49. public object Clone()
  50. {
  51. using (Stream objStream = new MemoryStream())
  52. {
  53. IFormatter formatter = new BinaryFormatter();
  54. formatter.Serialize(objStream, this);
  55. objStream.Seek(0, SeekOrigin.Begin);
  56. return formatter.Deserialize(objStream) as EfemMessage ?? throw new InvalidOperationException();
  57. }
  58. }
  59. }
  60. [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
  61. public class HardwareCmdAttribute : Attribute
  62. {
  63. public string Member { get; set; }
  64. public HardwareCmdAttribute(string str) { this.Member = str; }
  65. }
  66. [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
  67. public class InterpretationAttribute : Attribute
  68. {
  69. public string Comment { get; set; }
  70. public InterpretationAttribute(string str) { this.Comment = str; }
  71. }
  72. public static class Extension
  73. {
  74. private const string ERROR_MSG = "hardware message invalid";
  75. /// <summary>
  76. /// NAK:FACTOR|Message*;
  77. /// ACK:Message*;
  78. /// CAN:Message*|Factor/Place;
  79. /// ABS:Message*|Error/Parameter1/Parameter2;
  80. /// EVT:Message*;
  81. /// </summary>
  82. public static EfemMessage ToMessage(this string context)
  83. {
  84. if (string.IsNullOrEmpty(context) || context.Length < 3)
  85. throw new ArgumentNullException(ERROR_MSG);
  86. EfemMessage msg = new EfemMessage { RawString = context, Direct = MsgDirection.From, Data = new List<string>() };
  87. // remove EOT
  88. string messageBody = context.Substring(0, context.IndexOf(EfemMessage.EOF) + 1);
  89. try
  90. {
  91. // split up the string
  92. string[] sBodies = messageBody.Split(Constant.delimiters);
  93. string sHead = sBodies[0];
  94. string sBasic = sBodies[1];
  95. string sPort = sBodies[2];
  96. // Head
  97. if (Enum.TryParse(sHead, true, out EfemMessage.MsgHead msgHead))
  98. {
  99. msg.Head = msgHead;
  100. }
  101. switch (msg.Head)
  102. {
  103. case EfemMessage.MsgHead.ACK:
  104. case EfemMessage.MsgHead.MOV:
  105. case EfemMessage.MsgHead.INF:
  106. case EfemMessage.MsgHead.EVT:
  107. // Basic
  108. msg.Operation = EfemConstant.ToOperation(sBasic);
  109. // Port
  110. if (Regex.IsMatch(sPort, @"P[123]$"))
  111. {
  112. msg.Port = sPort.ToModule();
  113. }
  114. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  115. {
  116. msg.Port = sPort.ToModule();
  117. }
  118. else
  119. {
  120. msg.Port = ModuleName.EFEM;
  121. }
  122. // Data
  123. switch (msg.Operation)
  124. {
  125. case EfemOperation.GetWaferInfo:
  126. msg.Data.Add(sBodies[3]);
  127. break;
  128. case EfemOperation.SigStatus:
  129. msg.Data.Add(sBodies[2]);
  130. msg.Data.Add(sBodies[3]);
  131. msg.Data.Add(sBodies[4]);
  132. break;
  133. case EfemOperation.Ready:
  134. msg.Data.Add(sBodies[2]);
  135. break;
  136. default:
  137. break;
  138. }
  139. break;
  140. case EfemMessage.MsgHead.NAK:
  141. msg.Factor = sBodies[1];
  142. msg.Data.Add(sBodies[2]);
  143. break;
  144. case EfemMessage.MsgHead.CAN:
  145. // CAN:ERROR/CLEAR;
  146. int a1 = messageBody.IndexOf(':');
  147. int a3 = messageBody.IndexOf(';');
  148. int a2 = messageBody.IndexOf('|');
  149. if (a2 > 0)
  150. {
  151. msg.Data.Add(messageBody.Substring(a1 + 1, a2 - a1 - 1));
  152. msg.Factor = messageBody.Substring(a2 + 1, a3 - a2 - 1);
  153. }
  154. else
  155. {
  156. int a8 = messageBody.IndexOf('/');
  157. msg.Factor = sBodies[2];
  158. }
  159. break;
  160. case EfemMessage.MsgHead.ABS:
  161. // ABS:INIT/ALL|ERROR/SYSTEM_FF10/UNDEFINITION;
  162. int a4 = messageBody.IndexOf('|');
  163. int a5 = messageBody.IndexOf(';');
  164. string errStr = messageBody.Substring(a4 + 1, a5 - a4 - 1);
  165. string[] a6 = errStr.Split('/');
  166. if (a6.Length < 3)
  167. {
  168. throw new ApplicationException($"ABS 格式错误 {errStr}");
  169. }
  170. msg.Factor = a6[0];
  171. msg.Data.Add(a6[1]);
  172. msg.Data.Add(a6[2]);
  173. break;
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. EV.PostAlarmLog(ModuleName.EFEM.ToString(), $"收到[{context}],解析出错[{ex.Message}]");
  179. }
  180. return msg;
  181. }
  182. public static string ToHWString(this Position pos)
  183. {
  184. string res = string.Empty;
  185. if (ModuleHelper.IsAligner(pos.Module) || ModuleHelper.IsCooling(pos.Module))
  186. {
  187. res = Constant.ModuleString[pos.Module];
  188. }
  189. else
  190. {
  191. string sMod = Constant.ModuleString[pos.Module];
  192. int num = pos.Slot + 1;
  193. string sSlot = num.ToString("D2");
  194. res = $"{sMod}{sSlot}";
  195. }
  196. return res;
  197. }
  198. public static string ToHWString(this ModuleName mod)
  199. {
  200. return Constant.ModuleString[mod];
  201. }
  202. public static ModuleName ToModule(this string str)
  203. {
  204. if (Constant.StringModule.ContainsKey(str))
  205. return Constant.StringModule[str];
  206. throw new ApplicationException($"Cannot translate {str}");
  207. }
  208. public static string HardwareCmd(this Enum val)
  209. {
  210. FieldInfo fi = val.GetType().GetField(val.ToString());
  211. return fi.GetCustomAttributes(typeof(HardwareCmdAttribute), false) is HardwareCmdAttribute[] attrs
  212. && attrs.Length > 0 ? attrs[0].Member : val.ToString();
  213. }
  214. public static string Interpretation(this Enum val)
  215. {
  216. FieldInfo fi = val.GetType().GetField(val.ToString());
  217. return fi.GetCustomAttributes(typeof(InterpretationAttribute), false) is InterpretationAttribute[] attrs
  218. && attrs.Length > 0 ? attrs[0].Comment : val.ToString();
  219. }
  220. }
  221. /// <summary>
  222. /// constant define
  223. /// </summary>
  224. public class Constant
  225. {
  226. public static readonly char[] delimiters = { ':', '/', '>', '|', ';' };
  227. public const string ALL = "ALL";
  228. public const string MAP = "MAPDT";
  229. public const string SYS = "SYSTEM";
  230. public const string STOWER = "STOWER";
  231. public static readonly Dictionary<ModuleName, string> ModuleString = new Dictionary<ModuleName, string>
  232. {
  233. [ModuleName.EFEM] = "ALL",
  234. [ModuleName.EfemRobot]= "ROB",
  235. [ModuleName.LP1] = "P1",
  236. [ModuleName.LP2] = "P2",
  237. [ModuleName.PMA] = "LLA",
  238. [ModuleName.PMB] = "LLB",
  239. [ModuleName.Aligner1] = "ALIGN3",
  240. [ModuleName.Aligner2] = "ALIGN4",
  241. [ModuleName.Cooling1] = "ALIGN",
  242. [ModuleName.Cooling2] = "ALIGN2"
  243. };
  244. public static readonly Dictionary<string, ModuleName> StringModule = new Dictionary<string, ModuleName>
  245. {
  246. ["ALL"] = ModuleName.EFEM,
  247. ["System"] = ModuleName.EFEM,
  248. ["P1"] = ModuleName.LP1,
  249. ["P2"] = ModuleName.LP2,
  250. ["LLA"] = ModuleName.PMA,
  251. ["LLB"] = ModuleName.PMB,
  252. ["ALIGN"] = ModuleName.Cooling1,
  253. ["ALIGN2"] = ModuleName.Cooling2,
  254. ["ALIGN3"] = ModuleName.Aligner1,
  255. ["ALIGN4"] = ModuleName.Aligner2,
  256. };
  257. public static readonly Dictionary<Hand, string> ArmString = new Dictionary<Hand, string>
  258. {
  259. [Hand.Blade1] = "ARM2",
  260. [Hand.Blade2] = "ARM1",
  261. [Hand.Both] = "ARM3"
  262. };
  263. public static readonly Dictionary<ExtendPos, string> ExtendPosString = new Dictionary<ExtendPos, string>
  264. {
  265. //[ExtendPos.GetReady] = "G1",
  266. //[ExtendPos.GetTarget] = "GB",
  267. //[ExtendPos.GetBack] = "G4",
  268. //[ExtendPos.PutReady] = "P1",
  269. //[ExtendPos.PutTarget] = "PB",
  270. //[ExtendPos.PutBack] = "P4"
  271. };
  272. public static readonly Dictionary<string, string> FactorString = new Dictionary<string, string>
  273. {
  274. { "MSG", "Message error" },
  275. { "NOREAD", "Communication not established" },
  276. { "NOINITCMPL", "Initialization not completed" },
  277. { "NOORGCMPL", "TODO" },
  278. { "VAC", "TODO" },
  279. { "AIR", "TODO" },
  280. { "EMS", "TODO" },
  281. { "ERROR", "TODO" },
  282. { "BUSY", "Busy (Transfer system is in use)" },
  283. { "CMPL", "TODO" },
  284. { "NOMAPCMPL", "TODO" },
  285. { "NONWAF", "NO wafer on the position" },
  286. { "WAFER", "Wafer already existed" },
  287. { "NONPOD", "TODO" },
  288. { "POD", "TODO" },
  289. { "NOTINMOTION","TODO" },
  290. { "HOLD", "TODO" },
  291. { "NOHOLD", "TODO" },
  292. { "LLCLOSE", "LL door closed state" },
  293. { "LLNOEXTEND", "TODO" },
  294. { "REMOVE", "TODO" },
  295. { "MAINTENANCE","Key switch is set o Mainte" },
  296. { "NOLINK", "TODO" },
  297. { "ARMEXTEND", "TODO" },
  298. { "POWER", "TODO" },
  299. { "PRTWAF", "TODO" },
  300. { "NOPOS", "TODO" },
  301. { "NOFUNC", "TODO" },
  302. { "PARAM_NG", "Invalid parameters are included" }
  303. };
  304. public static readonly Dictionary<string, string> ABS_PARA1 = new Dictionary<string, string>
  305. {
  306. { "SYSTEM_FF01", "No specific route data or malfunction" },
  307. { "SYSTEM_FF10", "Operation time out" },
  308. { "SYSTEM_FF11", "Communication time out between PC for robot control and controller" },
  309. { "SYSTEM_FF12", "Encoder battery error" },
  310. { "SYSTEM_FF13", "Loss of encoder information" },
  311. { "SYSTEM_FF20", "No data file of route" },
  312. { "SYSTEM_FF21", "Serial number in route data is wrong" },
  313. { "SYSTEM_FF22", "No line for serial number in route data" },
  314. { "SYSTEM_FF23", "Discrepancy in serial number between route data and teaching data" },
  315. { "SYSTEM_FF30", "Failure of pre-operation movement at the wrist in the robot" },
  316. { "SYSTEM_FF83", "Inability of the robot to return origin" },
  317. { "SYSTEM_FFB0", "Operation malfunction at fan in robot" },
  318. { "SYSTEM_FFF0", "Alarm at Z-axis driver in the robot" },
  319. { "SYSTEM_FFFF", "Robot control program malfunction" }
  320. };
  321. }
  322. }