Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UHFCardHelper.cs 11 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using ReaderB;
  2. using System.Text;
  3. namespace UHFHelper
  4. {
  5. public class UHFCardHelper
  6. {
  7. /// <summary>
  8. /// 单例模式
  9. /// </summary>
  10. private static UHFCardHelper? Instance { get; set; }
  11. private UHFCardHelper()
  12. {
  13. }
  14. public static UHFCardHelper GetInstance()
  15. {
  16. if (Instance == null)
  17. {
  18. Instance = new UHFCardHelper();
  19. }
  20. return Instance;
  21. }
  22. //当前操作的ComAdr
  23. private byte fComAdr = 0xff;
  24. //返回串口的索引号
  25. private int frmcomportindex;
  26. /// <summary>
  27. /// 打开的COM口号
  28. /// </summary>
  29. public int OpenComNo { get; set; }
  30. //所有执行指令的返回值
  31. private int fCmdRet { get; set; } = 30;
  32. /// <summary>
  33. /// 串口打开标示
  34. /// </summary>
  35. public bool ComOpen { get; set; } = false;
  36. /// <summary>
  37. /// 密码
  38. /// </summary>
  39. private byte[] fPassWord = new byte[4];
  40. /// <summary>
  41. /// 错误码
  42. /// </summary>
  43. private int ferrorcode;
  44. //定义Timer类
  45. private System.Timers.Timer timer;
  46. //定义委托
  47. public delegate void SetControlValue(string value);
  48. private void InitTimer()
  49. {
  50. int interval = 1000;
  51. timer = new System.Timers.Timer(interval);
  52. timer.AutoReset = true;
  53. timer.Enabled = true;
  54. //绑定事件,每隔一秒执行一次此方法
  55. timer.Elapsed += new System.Timers.ElapsedEventHandler(MyMethod);
  56. }
  57. private void MyMethod(object sender, System.Timers.ElapsedEventArgs e)
  58. {
  59. //TODO 持续扫码
  60. }
  61. /// <summary>
  62. /// 打开串口
  63. /// </summary>
  64. /// <returns></returns>
  65. public Resultoutput OpenPort()
  66. {
  67. int port = 0;
  68. int openresult;
  69. fComAdr = Convert.ToByte("FF", 16); // $FF;
  70. try
  71. {
  72. openresult = StaticClassReaderB.AutoOpenComPort(ref port, ref fComAdr, 5, ref frmcomportindex);
  73. OpenComNo = frmcomportindex;
  74. if (openresult == 0)
  75. {
  76. ComOpen = true;
  77. //自动执行读取写卡器信息
  78. byte[] TrType = new byte[2];
  79. byte[] VersionInfo = new byte[2];
  80. byte ReaderType = 0;
  81. byte ScanTime = 0;
  82. byte dmaxfre = 0;
  83. byte dminfre = 0;
  84. byte powerdBm = 0;
  85. fCmdRet = StaticClassReaderB.GetReaderInformation(ref fComAdr, VersionInfo, ref ReaderType, TrType, ref dmaxfre, ref dminfre, ref powerdBm, ref ScanTime, frmcomportindex);
  86. if ((fCmdRet == 0x35) | (fCmdRet == 0x30))
  87. {
  88. StaticClassReaderB.CloseSpecComPort(frmcomportindex);
  89. ComOpen = false;
  90. return new Resultoutput { Res = false, ResMes = "串口通讯错误" };
  91. }
  92. if ((OpenComNo == -1) && (openresult == 0x30))
  93. {
  94. return new Resultoutput { Res = false, ResMes = "串口通讯错误" };
  95. }
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. return new Resultoutput { Res = false, ResMes = ex.ToString() };
  101. }
  102. ComOpen = true;
  103. //var result = WorkMode();
  104. //if (!result.Res)
  105. //{
  106. // return result;
  107. //}
  108. return new Resultoutput { Res = true, ResMes = "串口打开成功" };
  109. }
  110. /// <summary>
  111. /// 关闭串口
  112. /// </summary>
  113. /// <returns></returns>
  114. public Resultoutput ClosePort()
  115. {
  116. fCmdRet = StaticClassReaderB.CloseSpecComPort(OpenComNo);
  117. if (fCmdRet == 0)
  118. {
  119. ComOpen = false;
  120. }
  121. else
  122. {
  123. return new Resultoutput { Res = false, ResMes = "串口关闭失败" };
  124. }
  125. return new Resultoutput { Res = true, ResMes = "串口关闭成功" };
  126. }
  127. /// <summary>
  128. /// 设置工作模式
  129. /// </summary>
  130. public Resultoutput WorkMode()
  131. {
  132. byte[] Parameter = new byte[6];
  133. //应答模式
  134. Parameter[0] = Convert.ToByte(0);
  135. //工作模式 EPCC1 ISO18000-6B
  136. int Reader_bit0 = 0;
  137. //输出模式
  138. int Reader_bit1 = 0;
  139. //蜂鸣开关
  140. int Reader_bit2 = 1;
  141. //起始地址
  142. int Reader_bit3 = 0;
  143. //是否SYRIS485输出
  144. int Reader_bit4 = 0;
  145. Parameter[1] = Convert.ToByte(Reader_bit0 * 1 + Reader_bit1 * 2 + Reader_bit2 * 4 + Reader_bit3 * 8 + Reader_bit4 * 16);
  146. //存储区 EPC区
  147. Parameter[2] = 1;
  148. //起始字地址(Hex)
  149. Parameter[3] = Convert.ToByte("02", 16);
  150. //读取字数
  151. Parameter[4] = Convert.ToByte(1);
  152. //单张标签过滤时间
  153. Parameter[5] = Convert.ToByte(0);
  154. //设置工作模式
  155. fCmdRet = StaticClassReaderB.SetWorkMode(ref fComAdr, Parameter, frmcomportindex);
  156. if (fCmdRet == 0)
  157. {
  158. return new Resultoutput { Res = true, ResMes = "工作模式设置成功" };
  159. }
  160. return new Resultoutput { Res = false, ResMes = "读卡失败" };
  161. }
  162. /// <summary>
  163. /// 读卡
  164. /// </summary>
  165. /// <returns></returns>
  166. public string ReadCard()
  167. {
  168. if (!ComOpen) return string.Empty;
  169. int CardNum = 0;
  170. int Totallen = 0;
  171. int EPClen, m;
  172. byte[] EPC = new byte[5000];
  173. string temps;
  174. string sEPC;
  175. //fIsInventoryScan = true;
  176. byte AdrTID = 0;
  177. byte LenTID = 0;
  178. byte TIDFlag = 0;
  179. AdrTID = 0;
  180. LenTID = 0;
  181. TIDFlag = 0;
  182. fCmdRet = StaticClassReaderB.Inventory_G2(ref fComAdr, AdrTID, LenTID, TIDFlag, EPC, ref Totallen, ref CardNum, frmcomportindex);
  183. if ((fCmdRet == 1) | (fCmdRet == 2) | (fCmdRet == 3) | (fCmdRet == 4) | (fCmdRet == 0xFB))//代表已查找结束,
  184. {
  185. byte[] daw = new byte[Totallen];
  186. Array.Copy(EPC, daw, Totallen);
  187. temps = ByteArrayToHexString(daw);
  188. m = 0;
  189. if (CardNum == 0)
  190. {
  191. return "";
  192. }
  193. for (int CardIndex = 0; CardIndex < CardNum; CardIndex++)
  194. {
  195. EPClen = daw[m];
  196. sEPC = temps.Substring(m * 2 + 2, EPClen * 2);
  197. m = m + EPClen + 1;
  198. if (sEPC.Length != EPClen * 2)
  199. {
  200. return "";
  201. }
  202. string cardcode = sEPC.Substring(1, sEPC.Length - 1);
  203. return cardcode;
  204. }
  205. }
  206. return "";
  207. }
  208. /// <summary>
  209. /// 写卡
  210. /// </summary>
  211. /// <returns></returns>
  212. public Resultoutput WriteCard(string CardCodeStr)
  213. {
  214. if (!ComOpen) return new Resultoutput() { ResMes = "设备未连接", Res = false };
  215. byte[] WriteEPC = new byte[100];
  216. byte WriteEPClen;
  217. byte ENum;
  218. string AccessCode = "00000000";
  219. try
  220. {
  221. string CardCode = CardCodeStr;
  222. if ((CardCode.Length % 4) != 0)
  223. {
  224. CardCode = "0" + CardCodeStr;
  225. }
  226. WriteEPClen = Convert.ToByte(CardCode.Length / 2);
  227. ENum = Convert.ToByte(CardCode.Length / 4);
  228. byte[] EPC = new byte[ENum];
  229. EPC = HexStringToByteArray(CardCode);
  230. fPassWord = HexStringToByteArray(AccessCode);
  231. fCmdRet = StaticClassReaderB.WriteEPC_G2(ref fComAdr, fPassWord, EPC, WriteEPClen, ref ferrorcode, frmcomportindex);
  232. if (fCmdRet == 0)
  233. {
  234. return new Resultoutput { Res = true, ResMes = "写卡成功!" };
  235. }
  236. else
  237. {
  238. return new Resultoutput { Res = false, ResMes = "写卡失败!错误码:" + ferrorcode };
  239. }
  240. }
  241. catch (Exception ex)
  242. {
  243. return new Resultoutput { Res = false, ResMes = "写卡失败" + ex.Message };
  244. }
  245. }
  246. /// <summary>
  247. /// 解析数据
  248. /// </summary>
  249. /// <param name="s"></param>
  250. /// <returns></returns>
  251. private byte[] HexStringToByteArray(string s)
  252. {
  253. s = s.Replace(" ", "");
  254. byte[] buffer = new byte[s.Length / 2];
  255. for (int i = 0; i < s.Length; i += 2)
  256. buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
  257. return buffer;
  258. }
  259. /// <summary>
  260. /// 解析数据
  261. /// </summary>
  262. /// <param name="data"></param>
  263. /// <returns></returns>
  264. private string ByteArrayToHexString(byte[] data)
  265. {
  266. StringBuilder sb = new(data.Length * 3);
  267. foreach (byte b in data)
  268. sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
  269. return sb.ToString().ToUpper();
  270. }
  271. /// <summary>
  272. /// 字符串转16进制
  273. /// </summary>
  274. /// <param name="strEncode"></param>
  275. /// <returns></returns>
  276. public string ToEncode16(string strEncode)
  277. {
  278. byte[] b = Encoding.UTF8.GetBytes(strEncode);//按照指定编码将string编程字节数组
  279. string result = string.Empty;
  280. for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
  281. {
  282. result += Convert.ToString(b[i], 16);
  283. }
  284. return result;
  285. }
  286. /// <summary>
  287. /// 16进制转字符串
  288. /// </summary>
  289. /// <param name="strEncode"></param>
  290. /// <returns></returns>
  291. public string ToEncodeString(string strDecode)
  292. {
  293. string strTemp = "";
  294. byte[] b = new byte[strDecode.Length / 2];
  295. for (int i = 0; i < strDecode.Length / 2; i++)
  296. {
  297. strTemp = strDecode.Substring(i * 2, 2);
  298. b[i] = Convert.ToByte(strTemp, 16);
  299. }
  300. //按照指定编码将字节数组变为字符串
  301. return Encoding.UTF8.GetString(b);
  302. }
  303. }
  304. }