DeviceRfid.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using EasyModbusClient.businessBody;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using YG.Device;
  8. namespace IMCS.Device
  9. {
  10. public class DeviceRfid
  11. {
  12. private static DeviceRfid deviceRfid = new DeviceRfid();
  13. public static DeviceRfid Instance { get { return deviceRfid; } }
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <param name="url"></param>
  18. /// <param name="rfidvalue">rfid需要写入的值</param>
  19. /// <param name="rfidaddress">FFID写入的地址</param>
  20. /// <param name="writeswitchaddress">写入开关</param>
  21. private void RfidWrite(DeviceList url, string rfidvalue, string rfidaddress, string writeswitchaddress)
  22. {
  23. url.DeviceSend(rfidaddress, rfidvalue.ReturnBtyesWtitString(80));
  24. System.Threading.Thread.Sleep(1000);
  25. short mc = 1;
  26. url.DeviceSend(writeswitchaddress, mc);
  27. System.Threading.Thread.Sleep(1000);
  28. mc = 0;
  29. url.DeviceSend(writeswitchaddress, mc);
  30. }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. /// <param name="device"></param>
  35. /// <param name="readswitchrfidaddress">读取的开关</param>
  36. /// <param name="startaddress"需要读取的地址</param>
  37. /// <returns></returns>
  38. private string RfidRead(DeviceList device, string readswitchrfidaddress, string startaddress)
  39. {
  40. string value = "";
  41. short mc = 1;
  42. device.DeviceSend(readswitchrfidaddress, mc);
  43. System.Threading.Thread.Sleep(1000);
  44. mc = 0;
  45. device.DeviceSend(readswitchrfidaddress, mc);
  46. System.Threading.Thread.Sleep(1000);
  47. value =Encoding.ASCII.GetString( device.DeviceRead<byte[]>(startaddress,80)).Replace("\0","");
  48. return value;
  49. }
  50. public bool DeviceSend(List<RfidDefaultList> rfid, string url)
  51. {
  52. bool result = false;
  53. DeviceList device = YG.DeviceManage.Instance.GetOne(url);
  54. if (rfid.Count > 0 && device != null)
  55. {
  56. rfid.ForEach((m) =>
  57. {
  58. short mc = 0;
  59. if (m.index.Equals(1))
  60. {
  61. RfidWrite(device, m.val, "DB200.1304.0", "DB200.114");
  62. //device.DeviceSend("DB200.372", m.val.ReturnBtyesWtitString(256));
  63. //System.Threading.Thread.Sleep(300);
  64. //mc = 1;
  65. //device.DeviceSend("DB200.114", mc);
  66. //System.Threading.Thread.Sleep(300);
  67. //mc = 0;
  68. //device.DeviceSend("DB200.114", mc);
  69. }
  70. else
  71. {
  72. RfidWrite(device, m.val, "DB200.1384.0", "DB200.630");
  73. //device.DeviceSend("DB200.888", m.val.ReturnBtyesWtitString(256));
  74. //System.Threading.Thread.Sleep(300);
  75. //mc = 1;
  76. //device.DeviceSend("DB200.630", mc);
  77. //System.Threading.Thread.Sleep(300);
  78. //mc = 0;
  79. //device.DeviceSend("DB200.630", mc);
  80. }
  81. result = true;
  82. });
  83. }
  84. return result;
  85. }
  86. public List<RfidDefaultList> DeviceRead(string url)
  87. {
  88. List<RfidDefaultList> list = new List<RfidDefaultList>();
  89. bool result = false;
  90. DeviceList device = YG.DeviceManage.Instance.GetOne(url);
  91. if (device != null)
  92. {
  93. {
  94. list.Add(new RfidDefaultList() { index = 1, val = RfidRead(device, "DB200.112", "DB200.1144.0") });
  95. list.Add(new RfidDefaultList() { index = 2, val = RfidRead(device, "DB200.628", "DB200.1224.0") });
  96. }
  97. }
  98. return list;
  99. }
  100. }
  101. }