123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- using System;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.SCCore;
- namespace Aitex.Sorter.RT.Device.RFID
- {
- public interface IRFIDMsg
- {
- string DeviceID { get; set; }
- string package(params object[] args);
- /// </summary>
- /// return value, completed
- /// <param name="type"></param>
- /// <param name="cmd"></param>
- /// <returns></returns>
- bool unpackage(string msg);
- }
- public class RfidHandler<T> : IHandler where T : IRFIDMsg, new()
- {
- public int ID { get; set; }
- public int Unit { get; set; }
- public string Name { get; set; }
- public bool IsBackground { get; set; }
- private static int retry_time = 3;
- private int retry_count = retry_time;
- private T _imp = new T();
-
- private object[] _objs = null;
- public RfidHandler(string deviceID, params object[] objs)
- {
- _imp.DeviceID = deviceID;
- this._objs = objs;
- }
- public bool Execute<TPort>(ref TPort port) where TPort : ICommunication
- {
- retry_count = retry_time;
- return port.Write(string.Format("{0}\r", _imp.package(this._objs)));
- }
- /// <summary>
- /// return value: bhandle
- /// </summary>
- /// <typeparam name="TPort"></typeparam>
- /// <param name="port"></param>
- /// <param name="msg"></param>
- /// <param name="completed"></param>
- /// <returns></returns>
- public bool OnMessage<TPort>(ref TPort port, string message, out bool completed) where TPort : ICommunication
- {
- completed = false;
- try
- {
- string msg = message.Trim();
- string type = msg.Substring(0, 2);
- if (!type.Equals("00")) //0: command failed
- {
- /*
- if (retry_count-- <= 0)
- {
- string warning = string.Format("retry over {0} times", retry_time);
- LOG.Warning(warning);
- throw (new ExcuteFailedException(warning));
- }
- */
- string warning = string.Format("execute failed. cause is {0}.", getErrMsg(type));
- LOG.Warning(warning);
- throw (new ExcuteFailedException(warning));
-
- //port.Write(string.Format("{0}\r", _imp.package(this._objs)));
- //return true;
- }
- msg = msg.Substring(2, msg.Length - 2);
- completed = _imp.unpackage(msg);
- return true;
- }
- catch (ExcuteFailedException e)
- {
- throw (e);
- }
- catch (Exception ex)
- {
- throw (new InvalidPackageException(message));
- }
- }
- private string getErrMsg(string error)
- {
- string msg = "";
- switch (error)
- {
- case "14":
- msg = "Format error There is a mistake in the command format";
- break;
- case "70":
- msg = "Communications error Noise or another hindrance occurs during communications with an ID Tag, and communications cannot be completed normally.";
- break;
- case "71":
- msg = "Verification error Correct data cannot be written to an ID Tag";
- break;
- case "72":
- 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";
- break;
- case "7B":
- 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";
- break;
- case "7E":
- msg = "ID system error (1) The ID Tag is in a status where it cannot execute command processing";
- break;
- case "7F":
- msg = "ID system error (2) An inapplicable ID Tag has been used";
- break;
- case "9A":
- msg = "Hardware error in CPU An error occurred when writing to EEPROM.";
- break;
- }
- return msg;
- }
- }
- public class ReadHandler : IRFIDMsg //common move
- {
- public string DeviceID { get; set; }
-
- public ReadHandler()
- {
-
- }
- public string package(params object[] args)
- {
- string page = (string)args[0];
- return string.Format("{0}{1}", "0100", page);
- }
-
- public bool unpackage(string msg)
- {
- RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
- //string asciiValue = HEX2ASCII(msg.Substring(0, 256)).Trim('\0');
- string asciiValue = HEX2ASCII(msg).Trim('\0');
- if (asciiValue.IndexOf('\0') != -1)
- {
- asciiValue = asciiValue.Substring(0, asciiValue.IndexOf('\0'));
- }
- if (SC.ContainsItem("LoadPort.CarrierIdNeedTrimSpace") &&
- SC.GetValue<bool>("LoadPort.CarrierIdNeedTrimSpace"))
- {
- asciiValue = asciiValue.Trim();
- }
- device.SetCarrierIdReadResult(asciiValue);
- return true;
- }
- public string HEX2ASCII(string hex)
- {
- string res = String.Empty;
- try
- {
- for (int a = 0; a < hex.Length; a = a + 2)
- {
- string Char2Convert = hex.Substring(a, 2);
- int n = Convert.ToInt32(Char2Convert, 16);
- char c = (char)n;
- res += c.ToString();
- }
- }
- catch (Exception e)
- {
- LOG.Write(e);
- }
- return res;
- }
- }
- public class WriteHandler : IRFIDMsg //common move
- {
- public string DeviceID { get; set; }
- public string Rfid { get; set; }
- public int Length { get; set; } = 16;
- public WriteHandler()
- {
- }
- public string package(params object[] args)
- {
- string page = (string)args[0];
- Rfid = (string) args[1];
- if(args.Length >2) Length = (int)args[2];
- //Rfid = ASCII2HEX((string)args[1]);
- return string.Format("{0}{1}{2}", "0200", page, ASCII2HEX((string)args[1]).ToUpper());
- }
- public string ASCII2HEX(string src)
- {
- while (src.Length < Length*8)
- {
- src = src+ '\0';
- }
- if (src.Length > Length * 8)
- {
- src = src.Substring(0, Length * 8);
- LOG.Write($"RFID support max {(Length*8).ToString()} characters");
- }
- string res = String.Empty;
- try
- {
-
- char[] charValues = src.ToCharArray();
- string hexOutput = "";
- foreach (char _eachChar in charValues)
- {
- // Get the integral value of the character.
- int value = Convert.ToInt32(_eachChar);
- // Convert the decimal value to a hexadecimal value in string form.
- hexOutput += String.Format("{0:X2}", value);
- // to make output as your eg
- // hexOutput +=" "+ String.Format("{0:X}", value);
- }
- return hexOutput;
- }
- catch (Exception e)
- {
- LOG.Write(e);
- }
- return res;
- }
- public bool unpackage(string msg)
- {
- RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
- device.SetCarrierIdWriteResult(Rfid);
- return true;
- }
- }
- public class E99WriteHandler : IRFIDMsg //common move
- {
- public string DeviceID { get; set; }
- public string Rfid { get; set; }
- public int Length { get; set; }
- public E99WriteHandler()
- {
- }
- public string package(params object[] args)
- {
- int _startpage = (int)args[0];
- Length = (int)args[1];
- string page = GetPage(_startpage, Length);
- Rfid = (string)args[2];
-
- return string.Format("{0}{1}{2}", "0200", page, ASCII2HEX(Rfid.ToUpper()));
- }
- public string ASCII2HEX(string src)
- {
- while (src.Length < Length * 8)
- {
- src = src + '\0';
- }
- if (src.Length > Length * 8)
- {
- src = src.Substring(0, Length * 8);
- LOG.Write($"RFID support max {(Length * 8).ToString()} characters");
- }
- string res = String.Empty;
- try
- {
- char[] charValues = src.ToCharArray();
- string hexOutput = "";
- foreach (char _eachChar in charValues)
- {
- // Get the integral value of the character.
- int value = Convert.ToInt32(_eachChar);
- // Convert the decimal value to a hexadecimal value in string form.
- hexOutput += String.Format("{0:X2}", value);
- // to make output as your eg
- // hexOutput +=" "+ String.Format("{0:X}", value);
- }
- return hexOutput;
- }
- catch (Exception e)
- {
- LOG.Write(e);
- }
- return res;
- }
- public bool unpackage(string msg)
- {
- RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
- device.SetCarrierIdWriteResult(Rfid);
- return true;
- }
- private string GetPage(int startpage,int length)
- {
-
- double dpage = 0;
- for (int i = 0; i < length; i++)
- {
- dpage = dpage + Math.Pow(2, startpage+1 + i);
- }
- string pageret = String.Format("{0:X}", Convert.ToInt32(dpage));
- for (int j = pageret.Length; j < 8; j++)
- {
- pageret = "0" + pageret;
- }
- return pageret;
- }
- }
- public class E99ReadHandler : IRFIDMsg //common move
- {
- public string DeviceID { get; set; }
- public int Length { get; set; }
- public E99ReadHandler()
- {
- }
- public string package(params object[] args)
- {
- int _startpage = (int)args[0];
- Length = (int)args[1];
- string page = GetPage(_startpage, Length);
-
- return string.Format("{0}{1}", "0100", page);
- }
- public bool unpackage(string msg)
- {
- RIDReader device = DEVICE.GetDevice<RIDReader>(DeviceID);
- //string asciiValue = HEX2ASCII(msg.Substring(0, 256)).Trim('\0');
- string asciiValue = HEX2ASCII(msg).Trim('\0');
- if (asciiValue.IndexOf('\0') != -1)
- {
- asciiValue = asciiValue.Substring(0, asciiValue.IndexOf('\0'));
- }
- if (SC.ContainsItem("LoadPort.CarrierIdNeedTrimSpace") &&
- SC.GetValue<bool>("LoadPort.CarrierIdNeedTrimSpace"))
- {
- asciiValue = asciiValue.Trim();
- }
- device.SetCarrierIdReadResult(asciiValue);
- return true;
- }
- private string GetPage(int startpage, int length)
- {
- double dpage = 0;
- for (int i = 0; i < length; i++)
- {
- dpage = dpage + Math.Pow(2, startpage + 1 + i);
- }
- string pageret = String.Format("{0:X}", Convert.ToInt32(dpage));
- for (int j = pageret.Length; j < 8; j++)
- {
- pageret = "0" + pageret;
- }
- return pageret;
- }
- public string HEX2ASCII(string hex)
- {
- string res = String.Empty;
- try
- {
- for (int a = 0; a < hex.Length; a = a + 2)
- {
- string Char2Convert = hex.Substring(a, 2);
- int n = Convert.ToInt32(Char2Convert, 16);
- char c = (char)n;
- res += c.ToString();
- }
- }
- catch (Exception e)
- {
- LOG.Write(e);
- }
- return res;
- }
- }
- }
|