RfidHandler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using System;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.SCCore;
  5. namespace Aitex.Sorter.RT.Device.RFID
  6. {
  7. public interface IRFIDMsg
  8. {
  9. string DeviceID { get; set; }
  10. string package(params object[] args);
  11. /// </summary>
  12. /// return value, completed
  13. /// <param name="type"></param>
  14. /// <param name="cmd"></param>
  15. /// <returns></returns>
  16. bool unpackage(string msg);
  17. }
  18. public class RfidHandler<T> : IHandler where T : IRFIDMsg, new()
  19. {
  20. public int ID { get; set; }
  21. public int Unit { get; set; }
  22. public string Name { 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 RfidHandler(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("execute 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. throw (new InvalidPackageException(message));
  80. }
  81. }
  82. private string getErrMsg(string error)
  83. {
  84. string msg = "";
  85. switch (error)
  86. {
  87. case "14":
  88. msg = "Format error There is a mistake in the command format";
  89. break;
  90. case "70":
  91. msg = "Communications error Noise or another hindrance occurs during communications with an ID Tag, and communications cannot be completed normally.";
  92. break;
  93. case "71":
  94. msg = "Verification error Correct data cannot be written to an ID Tag";
  95. break;
  96. case "72":
  97. 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";
  98. break;
  99. case "7B":
  100. 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";
  101. break;
  102. case "7E":
  103. msg = "ID system error (1) The ID Tag is in a status where it cannot execute command processing";
  104. break;
  105. case "7F":
  106. msg = "ID system error (2) An inapplicable ID Tag has been used";
  107. break;
  108. case "9A":
  109. msg = "Hardware error in CPU An error occurred when writing to EEPROM.";
  110. break;
  111. }
  112. return msg;
  113. }
  114. }
  115. public class ReadHandler : IRFIDMsg //common move
  116. {
  117. public string DeviceID { get; set; }
  118. public ReadHandler()
  119. {
  120. }
  121. public string package(params object[] args)
  122. {
  123. string page = (string)args[0];
  124. return string.Format("{0}{1}", "0100", page);
  125. }
  126. public bool unpackage(string msg)
  127. {
  128. RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
  129. //string asciiValue = HEX2ASCII(msg.Substring(0, 256)).Trim('\0');
  130. string asciiValue = HEX2ASCII(msg).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 int Length { get; set; } = 16;
  168. public WriteHandler()
  169. {
  170. }
  171. public string package(params object[] args)
  172. {
  173. string page = (string)args[0];
  174. Rfid = (string) args[1];
  175. if(args.Length >2) Length = (int)args[2];
  176. //Rfid = ASCII2HEX((string)args[1]);
  177. return string.Format("{0}{1}{2}", "0200", page, ASCII2HEX((string)args[1]).ToUpper());
  178. }
  179. public string ASCII2HEX(string src)
  180. {
  181. while (src.Length < Length*8)
  182. {
  183. src = src+ '\0';
  184. }
  185. if (src.Length > Length * 8)
  186. {
  187. src = src.Substring(0, Length * 8);
  188. LOG.Write($"RFID support max {(Length*8).ToString()} characters");
  189. }
  190. string res = String.Empty;
  191. try
  192. {
  193. char[] charValues = src.ToCharArray();
  194. string hexOutput = "";
  195. foreach (char _eachChar in charValues)
  196. {
  197. // Get the integral value of the character.
  198. int value = Convert.ToInt32(_eachChar);
  199. // Convert the decimal value to a hexadecimal value in string form.
  200. hexOutput += String.Format("{0:X2}", value);
  201. // to make output as your eg
  202. // hexOutput +=" "+ String.Format("{0:X}", value);
  203. }
  204. return hexOutput;
  205. }
  206. catch (Exception e)
  207. {
  208. LOG.Write(e);
  209. }
  210. return res;
  211. }
  212. public bool unpackage(string msg)
  213. {
  214. RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
  215. device.SetCarrierIdWriteResult(Rfid);
  216. return true;
  217. }
  218. }
  219. public class E99WriteHandler : IRFIDMsg //common move
  220. {
  221. public string DeviceID { get; set; }
  222. public string Rfid { get; set; }
  223. public int Length { get; set; }
  224. public E99WriteHandler()
  225. {
  226. }
  227. public string package(params object[] args)
  228. {
  229. int _startpage = (int)args[0];
  230. Length = (int)args[1];
  231. string page = GetPage(_startpage, Length);
  232. Rfid = (string)args[2];
  233. return string.Format("{0}{1}{2}", "0200", page, ASCII2HEX(Rfid.ToUpper()));
  234. }
  235. public string ASCII2HEX(string src)
  236. {
  237. while (src.Length < Length * 8)
  238. {
  239. src = src + '\0';
  240. }
  241. if (src.Length > Length * 8)
  242. {
  243. src = src.Substring(0, Length * 8);
  244. LOG.Write($"RFID support max {(Length * 8).ToString()} characters");
  245. }
  246. string res = String.Empty;
  247. try
  248. {
  249. char[] charValues = src.ToCharArray();
  250. string hexOutput = "";
  251. foreach (char _eachChar in charValues)
  252. {
  253. // Get the integral value of the character.
  254. int value = Convert.ToInt32(_eachChar);
  255. // Convert the decimal value to a hexadecimal value in string form.
  256. hexOutput += String.Format("{0:X2}", value);
  257. // to make output as your eg
  258. // hexOutput +=" "+ String.Format("{0:X}", value);
  259. }
  260. return hexOutput;
  261. }
  262. catch (Exception e)
  263. {
  264. LOG.Write(e);
  265. }
  266. return res;
  267. }
  268. public bool unpackage(string msg)
  269. {
  270. RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
  271. device.SetCarrierIdWriteResult(Rfid);
  272. return true;
  273. }
  274. private string GetPage(int startpage,int length)
  275. {
  276. double dpage = 0;
  277. for (int i = 0; i < length; i++)
  278. {
  279. dpage = dpage + Math.Pow(2, startpage+1 + i);
  280. }
  281. string pageret = String.Format("{0:X}", Convert.ToInt32(dpage));
  282. for (int j = pageret.Length; j < 8; j++)
  283. {
  284. pageret = "0" + pageret;
  285. }
  286. return pageret;
  287. }
  288. }
  289. public class E99ReadHandler : IRFIDMsg //common move
  290. {
  291. public string DeviceID { get; set; }
  292. public int Length { get; set; }
  293. public E99ReadHandler()
  294. {
  295. }
  296. public string package(params object[] args)
  297. {
  298. int _startpage = (int)args[0];
  299. Length = (int)args[1];
  300. string page = GetPage(_startpage, Length);
  301. return string.Format("{0}{1}", "0100", page);
  302. }
  303. public bool unpackage(string msg)
  304. {
  305. RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
  306. //string asciiValue = HEX2ASCII(msg.Substring(0, 256)).Trim('\0');
  307. string asciiValue = HEX2ASCII(msg).Trim('\0');
  308. if (asciiValue.IndexOf('\0') != -1)
  309. {
  310. asciiValue = asciiValue.Substring(0, asciiValue.IndexOf('\0'));
  311. }
  312. if (SC.ContainsItem("LoadPort.CarrierIdNeedTrimSpace") &&
  313. SC.GetValue<bool>("LoadPort.CarrierIdNeedTrimSpace"))
  314. {
  315. asciiValue = asciiValue.Trim();
  316. }
  317. device.SetCarrierIdReadResult(asciiValue);
  318. return true;
  319. }
  320. private string GetPage(int startpage, int length)
  321. {
  322. double dpage = 0;
  323. for (int i = 0; i < length; i++)
  324. {
  325. dpage = dpage + Math.Pow(2, startpage + 1 + i);
  326. }
  327. string pageret = String.Format("{0:X}", Convert.ToInt32(dpage));
  328. for (int j = pageret.Length; j < 8; j++)
  329. {
  330. pageret = "0" + pageret;
  331. }
  332. return pageret;
  333. }
  334. public string HEX2ASCII(string hex)
  335. {
  336. string res = String.Empty;
  337. try
  338. {
  339. for (int a = 0; a < hex.Length; a = a + 2)
  340. {
  341. string Char2Convert = hex.Substring(a, 2);
  342. int n = Convert.ToInt32(Char2Convert, 16);
  343. char c = (char)n;
  344. res += c.ToString();
  345. }
  346. }
  347. catch (Exception e)
  348. {
  349. LOG.Write(e);
  350. }
  351. return res;
  352. }
  353. }
  354. }