EfemMessage.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.StateTrack:
  126. case EfemOperation.GetWaferInfo:
  127. msg.Data.Add(sBodies[3]);
  128. break;
  129. case EfemOperation.CarrierId:
  130. msg.Data.Add(msg.RawString.Substring("INF:CSTID/P1/".Length));
  131. break;
  132. case EfemOperation.SigStatus:
  133. msg.Data.Add(sBodies[2]);
  134. msg.Data.Add(sBodies[3]);
  135. msg.Data.Add(sBodies[4]);
  136. break;
  137. case EfemOperation.Ready:
  138. msg.Data.Add(sBodies[2]);
  139. break;
  140. default:
  141. break;
  142. }
  143. break;
  144. case EfemMessage.MsgHead.NAK:
  145. msg.Operation = EfemConstant.ToOperation(sBasic);
  146. // Port
  147. if (Regex.IsMatch(sPort, @"P[123]$"))
  148. {
  149. msg.Port = sPort.ToModule();
  150. }
  151. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  152. {
  153. msg.Port = sPort.ToModule();
  154. }
  155. else
  156. {
  157. msg.Port = ModuleName.EFEM;
  158. }
  159. msg.Factor = sBodies[1];
  160. msg.Data.Add(sBodies[2]);
  161. msg.Operation = EfemConstant.ToOperation(sBasic);
  162. break;
  163. case EfemMessage.MsgHead.CAN:
  164. msg.Operation = EfemConstant.ToOperation(sBasic);
  165. // Port
  166. if (Regex.IsMatch(sPort, @"P[123]$"))
  167. {
  168. msg.Port = sPort.ToModule();
  169. }
  170. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  171. {
  172. msg.Port = sPort.ToModule();
  173. }
  174. else
  175. {
  176. msg.Port = ModuleName.EFEM;
  177. }
  178. // CAN:ERROR/CLEAR;
  179. int a1 = messageBody.IndexOf(':');
  180. int a3 = messageBody.IndexOf(';');
  181. int a2 = messageBody.IndexOf('|');
  182. if (a2 > 0)
  183. {
  184. msg.Data.Add(messageBody.Substring(a1 + 1, a2 - a1 - 1));
  185. msg.Factor = messageBody.Substring(a2 + 1, a3 - a2 - 1);
  186. }
  187. else
  188. {
  189. int a8 = messageBody.IndexOf('/');
  190. msg.Factor = sBodies[2];
  191. }
  192. break;
  193. case EfemMessage.MsgHead.ABS:
  194. msg.Operation = EfemConstant.ToOperation(sBasic);
  195. // Port
  196. if (Regex.IsMatch(sPort, @"P[123]$"))
  197. {
  198. msg.Port = sPort.ToModule();
  199. }
  200. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  201. {
  202. msg.Port = sPort.ToModule();
  203. }
  204. else
  205. {
  206. msg.Port = ModuleName.EFEM;
  207. }
  208. // ABS:INIT/ALL|ERROR/SYSTEM_FF10/UNDEFINITION;
  209. //ABS: UNLOAD / ALIGN3 / ARM2 / WS4 | ERROR / RB | 2B90
  210. int a4 = messageBody.IndexOf('|');
  211. int a5 = messageBody.IndexOf(';');
  212. string errStr = messageBody.Substring(a4 + 1, a5 - a4 - 1);
  213. string[] a6 = errStr.Split('/', '|');
  214. if (a6.Length < 3)
  215. {
  216. throw new ApplicationException($"ABS 格式错误 {errStr}");
  217. }
  218. msg.Factor = a6[0];
  219. msg.Data.Add(a6[1]);
  220. msg.Data.Add(a6[2]);
  221. break;
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. EV.PostAlarmLog(ModuleName.EFEM.ToString(), $"收到[{context}],解析出错[{ex.Message}]");
  227. }
  228. return msg;
  229. }
  230. public static string ToHWString(this Position pos)
  231. {
  232. string res = string.Empty;
  233. if (ModuleHelper.IsAligner(pos.Module) || ModuleHelper.IsCooling(pos.Module))
  234. {
  235. res = Constant.ModuleString[pos.Module];
  236. }
  237. else
  238. {
  239. string sMod = Constant.ModuleString[pos.Module];
  240. int num = pos.Slot + 1;
  241. string sSlot = num.ToString("D2");
  242. res = $"{sMod}{sSlot}";
  243. }
  244. return res;
  245. }
  246. public static string ToHWString(this ModuleName mod)
  247. {
  248. return Constant.ModuleString[mod];
  249. }
  250. public static ModuleName ToModule(this string str)
  251. {
  252. if (Constant.StringModule.ContainsKey(str))
  253. return Constant.StringModule[str];
  254. throw new ApplicationException($"Cannot translate {str}");
  255. }
  256. public static string HardwareCmd(this Enum val)
  257. {
  258. FieldInfo fi = val.GetType().GetField(val.ToString());
  259. return fi.GetCustomAttributes(typeof(HardwareCmdAttribute), false) is HardwareCmdAttribute[] attrs
  260. && attrs.Length > 0 ? attrs[0].Member : val.ToString();
  261. }
  262. public static string Interpretation(this Enum val)
  263. {
  264. FieldInfo fi = val.GetType().GetField(val.ToString());
  265. return fi.GetCustomAttributes(typeof(InterpretationAttribute), false) is InterpretationAttribute[] attrs
  266. && attrs.Length > 0 ? attrs[0].Comment : val.ToString();
  267. }
  268. }
  269. /// <summary>
  270. /// constant define
  271. /// </summary>
  272. public class Constant
  273. {
  274. public static readonly char[] delimiters = { ':', '/', '>', '|', ';' };
  275. public const string ALL = "ALL";
  276. public const string MAP = "MAPDT";
  277. public const string SYS = "SYSTEM";
  278. public const string STOWER = "STOWER";
  279. public static readonly Dictionary<ModuleName, string> ModuleString = new Dictionary<ModuleName, string>
  280. {
  281. [ModuleName.EFEM] = "ALL",
  282. [ModuleName.EfemRobot]= "ROB",
  283. [ModuleName.LP1] = "P1",
  284. [ModuleName.LP2] = "P2",
  285. [ModuleName.PMA] = "LLA",
  286. [ModuleName.PMB] = "LLB",
  287. [ModuleName.Aligner1] = "ALIGN3",
  288. [ModuleName.Aligner2] = "ALIGN4",
  289. [ModuleName.Cooling1] = "ALIGN",
  290. [ModuleName.Cooling2] = "ALIGN2"
  291. };
  292. public static readonly Dictionary<string, ModuleName> StringModule = new Dictionary<string, ModuleName>
  293. {
  294. ["ALL"] = ModuleName.EFEM,
  295. ["System"] = ModuleName.EFEM,
  296. ["P1"] = ModuleName.LP1,
  297. ["P2"] = ModuleName.LP2,
  298. ["LLA"] = ModuleName.PMA,
  299. ["LLB"] = ModuleName.PMB,
  300. ["ALIGN"] = ModuleName.Cooling1,
  301. ["ALIGN2"] = ModuleName.Cooling2,
  302. ["ALIGN3"] = ModuleName.Aligner1,
  303. ["ALIGN4"] = ModuleName.Aligner2,
  304. };
  305. public static readonly Dictionary<Hand, string> ArmString = new Dictionary<Hand, string>
  306. {
  307. [Hand.Blade1] = "ARM2",
  308. [Hand.Blade2] = "ARM1",
  309. [Hand.Both] = "ARM3"
  310. };
  311. public static readonly Dictionary<ExtendPos, string> ExtendPosString = new Dictionary<ExtendPos, string>
  312. {
  313. //[ExtendPos.GetReady] = "G1",
  314. //[ExtendPos.GetTarget] = "GB",
  315. //[ExtendPos.GetBack] = "G4",
  316. //[ExtendPos.PutReady] = "P1",
  317. //[ExtendPos.PutTarget] = "PB",
  318. //[ExtendPos.PutBack] = "P4"
  319. };
  320. public static readonly Dictionary<string, string> FactorString = new Dictionary<string, string>
  321. {
  322. { "MSG", "Message error" },
  323. { "NOREAD", "Communication not established" },
  324. { "NOINITCMPL", "Initialization not completed" },
  325. { "NOORGCMPL", "Not org finished" },
  326. { "VAC", "Vac error" },
  327. { "AIR", "Air error" },
  328. { "EMS", "EMS pushed" },
  329. { "ERROR", "Has Error" },
  330. { "BUSY", "Busy (Module in use)" },
  331. { "CMPL", "Already Completed" },
  332. { "NOMAPCMPL", "Not Mapped" },
  333. { "NONWAF", "NO wafer on the position" },
  334. { "WAFER", "Wafer already existed" },
  335. { "NONPOD", "No Pod" },
  336. { "POD", "Pod Exist" },
  337. { "NOTINMOTION","Not in Motion" },
  338. { "HOLD", "Holded" },
  339. { "NOHOLD", "Not hold" },
  340. { "LLCLOSE", "LL door closed state" },
  341. { "LLNOEXTEND", "LL not extended" },
  342. { "REMOVE", "remove" },
  343. { "MAINTENANCE","In maintain mode or EFEM offline" },
  344. { "NOLINK", "not config" },
  345. { "ARMEXTEND", "arm extended" },
  346. { "POWER", "power off" },
  347. { "PRTWAF", "TODO" },
  348. { "NOPOS", "Not at valid position" },
  349. { "NOFUNC", "not support" },
  350. { "PARAM_NG", "Invalid parameters are included" }
  351. };
  352. public static readonly Dictionary<string, string> ABS_PARA1 = new Dictionary<string, string>
  353. {
  354. { "SYSTEM_FF01", "No specific route data or malfunction" },
  355. { "SYSTEM_FF10", "Operation time out" },
  356. { "SYSTEM_FF11", "Communication time out between PC for robot control and controller" },
  357. { "SYSTEM_FF12", "Encoder battery error" },
  358. { "SYSTEM_FF13", "Loss of encoder information" },
  359. { "SYSTEM_FF20", "No data file of route" },
  360. { "SYSTEM_FF21", "Serial number in route data is wrong" },
  361. { "SYSTEM_FF22", "No line for serial number in route data" },
  362. { "SYSTEM_FF23", "Discrepancy in serial number between route data and teaching data" },
  363. { "SYSTEM_FF30", "Failure of pre-operation movement at the wrist in the robot" },
  364. { "SYSTEM_FF83", "Inability of the robot to return origin" },
  365. { "SYSTEM_FFB0", "Operation malfunction at fan in robot" },
  366. { "SYSTEM_FFF0", "Alarm at Z-axis driver in the robot" },
  367. { "SYSTEM_FFFF", "Robot control program malfunction" }
  368. };
  369. }
  370. }