EfemMessage.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 Venus_Core;
  12. namespace Venus_RT.Devices.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}{EOF}\r";
  35. return context;
  36. }
  37. public string ToParamString()
  38. {
  39. string res = string.Empty;
  40. if (Parameters == null || Parameters.Count <= 0)
  41. return res;
  42. foreach (var para in Parameters)
  43. {
  44. res += '/' + para;
  45. }
  46. return res;
  47. }
  48. public object Clone()
  49. {
  50. using (Stream objStream = new MemoryStream())
  51. {
  52. IFormatter formatter = new BinaryFormatter();
  53. formatter.Serialize(objStream, this);
  54. objStream.Seek(0, SeekOrigin.Begin);
  55. return formatter.Deserialize(objStream) as EfemMessage ?? throw new InvalidOperationException();
  56. }
  57. }
  58. }
  59. [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
  60. public class HardwareCmdAttribute : Attribute
  61. {
  62. public string Member { get; set; }
  63. public HardwareCmdAttribute(string str) { this.Member = str; }
  64. }
  65. [AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field)]
  66. public class InterpretationAttribute : Attribute
  67. {
  68. public string Comment { get; set; }
  69. public InterpretationAttribute(string str) { this.Comment = str; }
  70. }
  71. public static class Extension
  72. {
  73. private const string ERROR_MSG = "hardware message invalid";
  74. /// <summary>
  75. /// NAK:FACTOR|Message*;
  76. /// ACK:Message*;
  77. /// CAN:Message*|Factor/Place;
  78. /// ABS:Message*|Error/Parameter1/Parameter2;
  79. /// EVT:Message*;
  80. /// </summary>
  81. public static EfemMessage ToMessage(this string context)
  82. {
  83. if (string.IsNullOrEmpty(context) || context.Length < 3)
  84. throw new ArgumentNullException(ERROR_MSG);
  85. EfemMessage msg = new EfemMessage { RawString = context, Direct = MsgDirection.From, Data = new List<string>() };
  86. // remove EOT
  87. string messageBody = context.Substring(0, context.IndexOf(EfemMessage.EOF) + 1);
  88. try
  89. {
  90. // split up the string
  91. string[] sBodies = messageBody.Split(Constant.delimiters);
  92. string sHead = sBodies[0];
  93. string sBasic = sBodies[1];
  94. string sPort = sBodies[2];
  95. // Head
  96. if (Enum.TryParse(sHead, true, out EfemMessage.MsgHead msgHead))
  97. {
  98. msg.Head = msgHead;
  99. }
  100. switch (msg.Head)
  101. {
  102. case EfemMessage.MsgHead.ACK:
  103. case EfemMessage.MsgHead.MOV:
  104. case EfemMessage.MsgHead.INF:
  105. case EfemMessage.MsgHead.EVT:
  106. // Basic
  107. msg.Operation = EfemConstant.ToOperation(sBasic);
  108. // Port
  109. if (Regex.IsMatch(sPort, @"P[123]$"))
  110. {
  111. msg.Port = sPort.ToModule();
  112. }
  113. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  114. {
  115. msg.Port = sPort.ToModule();
  116. }
  117. else
  118. {
  119. msg.Port = ModuleName.EFEM;
  120. }
  121. // Data
  122. switch (msg.Operation)
  123. {
  124. case EfemOperation.StateTrack:
  125. case EfemOperation.GetWaferInfo:
  126. msg.Data.Add(sBodies[3]);
  127. break;
  128. case EfemOperation.CarrierId:
  129. msg.Data.Add(msg.RawString.Substring("INF:CSTID/P1/".Length));
  130. break;
  131. case EfemOperation.SigStatus:
  132. msg.Data.Add(sBodies[2]);
  133. msg.Data.Add(sBodies[3]);
  134. msg.Data.Add(sBodies[4]);
  135. break;
  136. case EfemOperation.Ready:
  137. msg.Data.Add(sBodies[2]);
  138. break;
  139. default:
  140. break;
  141. }
  142. break;
  143. case EfemMessage.MsgHead.NAK:
  144. msg.Operation = EfemConstant.ToOperation(sBasic);
  145. // Port
  146. if (Regex.IsMatch(sPort, @"P[123]$"))
  147. {
  148. msg.Port = sPort.ToModule();
  149. }
  150. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  151. {
  152. msg.Port = sPort.ToModule();
  153. }
  154. else
  155. {
  156. msg.Port = ModuleName.EFEM;
  157. }
  158. msg.Factor = sBodies[1];
  159. msg.Data.Add(sBodies[2]);
  160. msg.Operation = EfemConstant.ToOperation(sBasic);
  161. break;
  162. case EfemMessage.MsgHead.CAN:
  163. msg.Operation = EfemConstant.ToOperation(sBasic);
  164. // Port
  165. if (Regex.IsMatch(sPort, @"P[123]$"))
  166. {
  167. msg.Port = sPort.ToModule();
  168. }
  169. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  170. {
  171. msg.Port = sPort.ToModule();
  172. }
  173. else
  174. {
  175. msg.Port = ModuleName.EFEM;
  176. }
  177. // CAN:ERROR/CLEAR;
  178. int a1 = messageBody.IndexOf(':');
  179. int a3 = messageBody.IndexOf(';');
  180. int a2 = messageBody.IndexOf('|');
  181. if (a2 > 0)
  182. {
  183. msg.Data.Add(messageBody.Substring(a1 + 1, a2 - a1 - 1));
  184. msg.Factor = messageBody.Substring(a2 + 1, a3 - a2 - 1);
  185. }
  186. else
  187. {
  188. int a8 = messageBody.IndexOf('/');
  189. msg.Factor = sBodies[2];
  190. }
  191. break;
  192. case EfemMessage.MsgHead.ABS:
  193. msg.Operation = EfemConstant.ToOperation(sBasic);
  194. // Port
  195. if (Regex.IsMatch(sPort, @"P[123]$"))
  196. {
  197. msg.Port = sPort.ToModule();
  198. }
  199. else if (Regex.IsMatch(sPort, @"ALIGN\d?", RegexOptions.IgnoreCase))
  200. {
  201. msg.Port = sPort.ToModule();
  202. }
  203. else
  204. {
  205. msg.Port = ModuleName.EFEM;
  206. }
  207. // ABS:INIT/ALL|ERROR/SYSTEM_FF10/UNDEFINITION;
  208. int a4 = messageBody.IndexOf('|');
  209. int a5 = messageBody.IndexOf(';');
  210. string errStr = messageBody.Substring(a4 + 1, a5 - a4 - 1);
  211. string[] a6 = errStr.Split('/');
  212. if (a6.Length < 3)
  213. {
  214. throw new ApplicationException($"ABS 格式错误 {errStr}");
  215. }
  216. msg.Factor = a6[0];
  217. msg.Data.Add(a6[1]);
  218. msg.Data.Add(a6[2]);
  219. break;
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. EV.PostAlarmLog(ModuleName.EFEM.ToString(), $"收到[{context}],解析出错[{ex.Message}]");
  225. }
  226. return msg;
  227. }
  228. public static string ToHWString(this Position pos)
  229. {
  230. string res = string.Empty;
  231. if (ModuleHelper.IsAligner(pos.Module) || ModuleHelper.IsCooling(pos.Module))
  232. {
  233. res = Constant.ModuleString[pos.Module];
  234. }
  235. else
  236. {
  237. string sMod = Constant.ModuleString[pos.Module];
  238. int num = pos.Slot + 1;
  239. string sSlot = num.ToString("D2");
  240. res = $"{sMod}{sSlot}";
  241. }
  242. return res;
  243. }
  244. public static string ToHWString(this ModuleName mod)
  245. {
  246. return Constant.ModuleString[mod];
  247. }
  248. public static ModuleName ToModule(this string str)
  249. {
  250. if (Constant.StringModule.ContainsKey(str))
  251. return Constant.StringModule[str];
  252. throw new ApplicationException($"Cannot translate {str}");
  253. }
  254. public static string HardwareCmd(this Enum val)
  255. {
  256. FieldInfo fi = val.GetType().GetField(val.ToString());
  257. return fi.GetCustomAttributes(typeof(HardwareCmdAttribute), false) is HardwareCmdAttribute[] attrs
  258. && attrs.Length > 0 ? attrs[0].Member : val.ToString();
  259. }
  260. public static string Interpretation(this Enum val)
  261. {
  262. FieldInfo fi = val.GetType().GetField(val.ToString());
  263. return fi.GetCustomAttributes(typeof(InterpretationAttribute), false) is InterpretationAttribute[] attrs
  264. && attrs.Length > 0 ? attrs[0].Comment : val.ToString();
  265. }
  266. }
  267. /// <summary>
  268. /// constant define
  269. /// </summary>
  270. public class Constant
  271. {
  272. public static readonly char[] delimiters = { ':', '/', '>', '|', ';' };
  273. public const string ALL = "ALL";
  274. public const string MAP = "MAPDT";
  275. public const string SYS = "SYSTEM";
  276. public const string STOWER = "STOWER";
  277. public static readonly Dictionary<ModuleName, string> ModuleString = new Dictionary<ModuleName, string>
  278. {
  279. [ModuleName.EFEM] = "ALL",
  280. [ModuleName.EfemRobot]= "ROB",
  281. [ModuleName.LLA] = "LLLA",
  282. [ModuleName.LLB] = "LLLB",
  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. [ModuleName.Buffer] = "BUFFER"
  292. };
  293. public static readonly Dictionary<string, ModuleName> StringModule = new Dictionary<string, ModuleName>
  294. {
  295. ["ALL"] = ModuleName.EFEM,
  296. ["System"] = ModuleName.EFEM,
  297. ["P1"] = ModuleName.LP1,
  298. ["P2"] = ModuleName.LP2,
  299. ["P3"] = ModuleName.LP3,
  300. ["LLA"] = ModuleName.PMA,
  301. ["LLB"] = ModuleName.PMB,
  302. ["ALIGN"] = ModuleName.Cooling1,
  303. ["ALIGN2"] = ModuleName.Cooling2,
  304. ["ALIGN3"] = ModuleName.Aligner1,
  305. ["ALIGN4"] = ModuleName.Aligner2,
  306. ["BUFFER"] = ModuleName.Buffer,
  307. ["LLLA"] = ModuleName.LLA,
  308. ["LLLB"] = ModuleName.LLB,
  309. };
  310. public static readonly Dictionary<Hand, string> ArmString = new Dictionary<Hand, string>
  311. {
  312. [Hand.Blade1] = "ARM2",
  313. [Hand.Blade2] = "ARM1",
  314. [Hand.Both] = "ARM3"
  315. };
  316. public static readonly Dictionary<ExtendPos, string> ExtendPosString = new Dictionary<ExtendPos, string>
  317. {
  318. //[ExtendPos.GetReady] = "G1",
  319. //[ExtendPos.GetTarget] = "GB",
  320. //[ExtendPos.GetBack] = "G4",
  321. //[ExtendPos.PutReady] = "P1",
  322. //[ExtendPos.PutTarget] = "PB",
  323. //[ExtendPos.PutBack] = "P4"
  324. };
  325. public static readonly Dictionary<string, string> FactorString = new Dictionary<string, string>
  326. {
  327. { "MSG", "Message error" },
  328. { "NOREAD", "Communication not established" },
  329. { "NOINITCMPL", "Initialization not completed" },
  330. { "NOORGCMPL", "Not org finished" },
  331. { "VAC", "Vac error" },
  332. { "AIR", "Air error" },
  333. { "EMS", "EMS pushed" },
  334. { "ERROR", "Has Error" },
  335. { "BUSY", "Busy (Module in use)" },
  336. { "CMPL", "Already Completed" },
  337. { "NOMAPCMPL", "Not Mapped" },
  338. { "NONWAF", "NO wafer on the position" },
  339. { "WAFER", "Wafer already existed" },
  340. { "NONPOD", "No Pod" },
  341. { "POD", "Pod Exist" },
  342. { "NOTINMOTION","Not in Motion" },
  343. { "HOLD", "Holded" },
  344. { "NOHOLD", "Not hold" },
  345. { "LLCLOSE", "LL door closed state" },
  346. { "LLNOEXTEND", "LL not extended" },
  347. { "REMOVE", "remove" },
  348. { "MAINTENANCE","In maintain mode or EFEM offline" },
  349. { "NOLINK", "not config" },
  350. { "ARMEXTEND", "arm extended" },
  351. { "POWER", "power off" },
  352. { "PRTWAF", "TODO" },
  353. { "NOPOS", "Not at valid position" },
  354. { "NOFUNC", "not support" },
  355. { "PARAM_NG", "Invalid parameters are included" }
  356. };
  357. public static readonly Dictionary<string, string> ABS_PARA1 = new Dictionary<string, string>
  358. {
  359. { "SYSTEM_FF01", "No specific route data or malfunction" },
  360. { "SYSTEM_FF10", "Operation time out" },
  361. { "SYSTEM_FF11", "Communication time out between PC for robot control and controller" },
  362. { "SYSTEM_FF12", "Encoder battery error" },
  363. { "SYSTEM_FF13", "Loss of encoder information" },
  364. { "SYSTEM_FF20", "No data file of route" },
  365. { "SYSTEM_FF21", "Serial number in route data is wrong" },
  366. { "SYSTEM_FF22", "No line for serial number in route data" },
  367. { "SYSTEM_FF23", "Discrepancy in serial number between route data and teaching data" },
  368. { "SYSTEM_FF30", "Failure of pre-operation movement at the wrist in the robot" },
  369. { "SYSTEM_FF83", "Inability of the robot to return origin" },
  370. { "SYSTEM_FFB0", "Operation malfunction at fan in robot" },
  371. { "SYSTEM_FFF0", "Alarm at Z-axis driver in the robot" },
  372. { "SYSTEM_FFFF", "Robot control program malfunction" }
  373. };
  374. }
  375. }