终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

122 lines
4.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Controls;
  7. namespace BPASmartClient.MorkCL.Server
  8. {
  9. /// <summary>
  10. /// 其它服务类
  11. /// </summary>
  12. internal class OtherServer : OtherDeviceSet, IModbus
  13. {
  14. public OtherServer()
  15. {
  16. //MyModbus = new ModbusRtu();
  17. }
  18. public void Init(string IP = "", int Port = 502, string PortName = "")
  19. {
  20. //MyModbus = new ModbusRtu();
  21. CommHelper.CreateModbusRtu(PortName).OnSuccess(s =>
  22. {
  23. MyModbus = s.Content;
  24. TaskManage.GetInstance.StartLong(new Action(() =>
  25. {
  26. //设置站号
  27. //(ModbusCommBase).SlaveAddress = 1;
  28. //myDevice.Read<bool[]>("LB100".ToModbusAdd(), 4).OnSuccess(s =>
  29. //{
  30. // for (byte i = 0; i < s.Length; i++) BoxDetection[i] = s[i];
  31. //});
  32. MyModbus.SlaveAddress = 1;
  33. MyModbus.Read<ushort>("LW630".ToModbusAdd()).OnSuccess(s =>
  34. {
  35. for (byte i = 0; i < 4; i++)
  36. {
  37. BoxDetection[i] = s.Content.GetBitValue((byte)(i + 1));
  38. }
  39. });
  40. Thread.Sleep(250);
  41. //myDevice.SlaveAddress = 2;
  42. MyModbus.SlaveAddress = 2;
  43. MyModbus.Read<float>("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s.Content; });
  44. Thread.Sleep(250);
  45. }), $"其它外部设备-{PortName}", true);
  46. });
  47. //MyModbus.WithModbusRtu(PortName).UseConnected(() =>
  48. //{
  49. // ModbusRtu myDevice = (ModbusRtu)MyModbus;
  50. // myDevice.master.Transport.ReadTimeout = 3000;
  51. // myDevice.master.Transport.WriteTimeout = 3000;
  52. // TaskManage.GetInstance.StartLong(new Action(() =>
  53. // {
  54. // //设置站号
  55. // myDevice.SlaveAddress = 1;
  56. // //myDevice.Read<bool[]>("LB100".ToModbusAdd(), 4).OnSuccess(s =>
  57. // //{
  58. // // for (byte i = 0; i < s.Length; i++) BoxDetection[i] = s[i];
  59. // //});
  60. // myDevice.Read<ushort>("LW630".ToModbusAdd()).OnSuccess(s =>
  61. // {
  62. // for (byte i = 0; i < 4; i++)
  63. // {
  64. // BoxDetection[i] = s.GetBitValue((byte)(i + 1));
  65. // }
  66. // });
  67. // Thread.Sleep(250);
  68. // myDevice.SlaveAddress = 2;
  69. // myDevice.Read<float>("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s; });
  70. // Thread.Sleep(250);
  71. // }), $"其它外部设备-{PortName}", true);
  72. //});
  73. }
  74. public void WriteValue<T>(string address, T value)
  75. {
  76. throw new NotImplementedException();
  77. }
  78. /// <summary>
  79. /// 除皮
  80. /// </summary>
  81. /// <returns></returns>
  82. public bool WeigherTare()
  83. {
  84. if (MyModbus is not null && MyModbus.IsConnected())
  85. {
  86. //ModbusRtu weigher = (ModbusRtu)MyModbus;
  87. //MyModbus.SlaveAddress = 2;
  88. MyModbus.SlaveAddress = 2;
  89. return MyModbus.Write<bool>("LB4".ToModbusAdd(), true).IsSuccess;
  90. }
  91. else
  92. return false;
  93. }
  94. /// <summary>
  95. /// 置零
  96. /// </summary>
  97. /// <returns></returns>
  98. public bool WeigherZero()
  99. {
  100. if (MyModbus is not null && MyModbus.IsConnected())
  101. {
  102. //ModbusRtu weigher = (ModbusRtu)MyModbus;
  103. //weigher.SlaveAddress = 2;
  104. MyModbus.SlaveAddress = 2;
  105. return MyModbus.Write<bool>("LB3".ToModbusAdd(), true).IsSuccess;
  106. }
  107. else
  108. return false;
  109. }
  110. }
  111. }