Handler.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.SCCore;
  5. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  6. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.CarrierIdReaders.OmronRFID
  7. {
  8. public interface IRFIDMsg
  9. {
  10. string DeviceID { get; set; }
  11. string package(params object[] args);
  12. /// </summary>
  13. /// return value, completed
  14. /// <param name="type"></param>
  15. /// <param name="cmd"></param>
  16. /// <returns></returns>
  17. bool unpackage(string msg);
  18. }
  19. public class handler<T> : IHandler where T : IRFIDMsg, new()
  20. {
  21. public int ID { get; set; }
  22. public int Unit { get; set; }
  23. public bool IsBackground { get; set; }
  24. private static int retry_time = 3;
  25. private int retry_count = retry_time;
  26. private T _imp = new T();
  27. private object[] _objs = null;
  28. public handler(string deviceID, params object[] objs)
  29. {
  30. _imp.DeviceID = deviceID;
  31. this._objs = objs;
  32. }
  33. public bool Execute<TPort>(ref TPort port) where TPort : ICommunication
  34. {
  35. retry_count = retry_time;
  36. return port.Write(string.Format("{0}\r", _imp.package(this._objs)));
  37. }
  38. /// <summary>
  39. /// return value: bhandle
  40. /// </summary>
  41. /// <typeparam name="TPort"></typeparam>
  42. /// <param name="port"></param>
  43. /// <param name="msg"></param>
  44. /// <param name="completed"></param>
  45. /// <returns></returns>
  46. public bool OnMessage<TPort>(ref TPort port, string message, out bool completed) where TPort : ICommunication
  47. {
  48. completed = false;
  49. try
  50. {
  51. string msg = message.Trim();
  52. string type = msg.Substring(0, 2);
  53. if (!type.Equals("00")) //0: command failed
  54. {
  55. /*
  56. if (retry_count-- <= 0)
  57. {
  58. string warning = string.Format("retry over {0} times", retry_time);
  59. LOG.Warning(warning);
  60. throw (new ExcuteFailedException(warning));
  61. }
  62. */
  63. string warning = string.Format("excute failed. cause is {0}.", getErrMsg(type));
  64. LOG.Warning(warning);
  65. throw (new ExcuteFailedException(warning));
  66. //port.Write(string.Format("{0}\r", _imp.package(this._objs)));
  67. //return true;
  68. }
  69. msg = msg.Substring(2, msg.Length - 2);
  70. completed = _imp.unpackage(msg);
  71. return true;
  72. }
  73. catch (ExcuteFailedException e)
  74. {
  75. throw (e);
  76. }
  77. catch (Exception ex)
  78. {
  79. LOG.Write(ex);
  80. throw (new InvalidPackageException(message));
  81. }
  82. }
  83. private string getErrMsg(string error)
  84. {
  85. string msg = "";
  86. switch (error)
  87. {
  88. case "14":
  89. msg = "Format error There is a mistake in the command format";
  90. break;
  91. case "70":
  92. msg = "Communications error Noise or another hindrance occurs during communications with an ID Tag, and communications cannot be completed normally.";
  93. break;
  94. case "71":
  95. msg = "Verification error Correct data cannot be written to an ID Tag";
  96. break;
  97. case "72":
  98. msg = "No Tag error Either there is no ID Tag in front of the CIDRW Head, or the CIDRW Head is unable to detect the ID Tag due to environmental factors";
  99. break;
  100. case "7B":
  101. msg = "Outside write area error A write operation was not completed normally because the ID Tag was in an area in which the ID Tag could be read but not written";
  102. break;
  103. case "7E":
  104. msg = "ID system error (1) The ID Tag is in a status where it cannot execute command processing";
  105. break;
  106. case "7F":
  107. msg = "ID system error (2) An inapplicable ID Tag has been used";
  108. break;
  109. case "9A":
  110. msg = "Hardware error in CPU An error occurred when writing to EEPROM.";
  111. break;
  112. }
  113. return msg;
  114. }
  115. }
  116. public class ReadHandler : IRFIDMsg //common move
  117. {
  118. public string DeviceID { get; set; }
  119. public ReadHandler()
  120. {
  121. }
  122. public string package(params object[] args)
  123. {
  124. string page = (string)args[0];
  125. return string.Format("{0}{1}", "0100", page);
  126. }
  127. public bool unpackage(string msg)
  128. {
  129. OmronRfidReader device = DEVICE.GetDevice<OmronRfidReader>(DeviceID);
  130. string asciiValue = HEX2ASCII(msg.Substring(0, Math.Min(256, msg.Length))).Trim('\0');
  131. if (asciiValue.IndexOf('\0') != -1)
  132. {
  133. asciiValue = asciiValue.Substring(0, asciiValue.IndexOf('\0'));
  134. }
  135. if (SC.ContainsItem("LoadPort.CarrierIdNeedTrimSpace") &&
  136. SC.GetValue<bool>("LoadPort.CarrierIdNeedTrimSpace"))
  137. {
  138. asciiValue = asciiValue.Trim();
  139. }
  140. device.SetCarrierIdReadResult(asciiValue);
  141. return true;
  142. }
  143. public string HEX2ASCII(string hex)
  144. {
  145. string res = String.Empty;
  146. try
  147. {
  148. for (int a = 0; a < hex.Length; a = a + 2)
  149. {
  150. string Char2Convert = hex.Substring(a, 2);
  151. int n = Convert.ToInt32(Char2Convert, 16);
  152. char c = (char)n;
  153. res += c.ToString();
  154. }
  155. }
  156. catch (Exception e)
  157. {
  158. LOG.Write(e);
  159. }
  160. return res;
  161. }
  162. }
  163. public class WriteHandler : IRFIDMsg //common move
  164. {
  165. public string DeviceID { get; set; }
  166. public string Rfid { get; set; }
  167. public WriteHandler()
  168. {
  169. }
  170. public string package(params object[] args)
  171. {
  172. string page = (string)args[0];
  173. Rfid = (string)args[1];
  174. //Rfid = ASCII2HEX((string)args[1]);
  175. return string.Format("{0}{1}{2}", "0200", page, ASCII2HEX((string)args[1]).ToUpper());
  176. }
  177. public string ASCII2HEX(string src)
  178. {
  179. while (src.Length < 128)
  180. {
  181. src = '\0' + src;
  182. }
  183. if (src.Length > 128)
  184. {
  185. src = src.Substring(0, 128);
  186. LOG.Write("RFID support max 128 characters");
  187. }
  188. string res = String.Empty;
  189. try
  190. {
  191. char[] charValues = src.ToCharArray();
  192. string hexOutput = "";
  193. foreach (char _eachChar in charValues)
  194. {
  195. // Get the integral value of the character.
  196. int value = Convert.ToInt32(_eachChar);
  197. // Convert the decimal value to a hexadecimal value in string form.
  198. hexOutput += String.Format("{0:X2}", value);
  199. // to make output as your eg
  200. // hexOutput +=" "+ String.Format("{0:X}", value);
  201. }
  202. return hexOutput;
  203. }
  204. catch (Exception e)
  205. {
  206. LOG.Write(e);
  207. }
  208. return res;
  209. }
  210. public bool unpackage(string msg)
  211. {
  212. OmronRfidReader device = DEVICE.GetDevice<OmronRfidReader>(DeviceID);
  213. //device.SetCarrierIdWriteResult(Rfid);
  214. return true;
  215. }
  216. }
  217. }