终端一体化运控平台
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.
 
 
 

120 lines
4.0 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.Read<ushort>("LW630".ToModbusAdd()).OnSuccess(s =>
  33. {
  34. for (byte i = 0; i < 4; i++)
  35. {
  36. BoxDetection[i] = s.Content.GetBitValue((byte)(i + 1));
  37. }
  38. });
  39. Thread.Sleep(250);
  40. //myDevice.SlaveAddress = 2;
  41. MyModbus.Read<float>("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s.Content; });
  42. Thread.Sleep(250);
  43. }), $"其它外部设备-{PortName}", true);
  44. });
  45. //MyModbus.WithModbusRtu(PortName).UseConnected(() =>
  46. //{
  47. // ModbusRtu myDevice = (ModbusRtu)MyModbus;
  48. // myDevice.master.Transport.ReadTimeout = 3000;
  49. // myDevice.master.Transport.WriteTimeout = 3000;
  50. // TaskManage.GetInstance.StartLong(new Action(() =>
  51. // {
  52. // //设置站号
  53. // myDevice.SlaveAddress = 1;
  54. // //myDevice.Read<bool[]>("LB100".ToModbusAdd(), 4).OnSuccess(s =>
  55. // //{
  56. // // for (byte i = 0; i < s.Length; i++) BoxDetection[i] = s[i];
  57. // //});
  58. // myDevice.Read<ushort>("LW630".ToModbusAdd()).OnSuccess(s =>
  59. // {
  60. // for (byte i = 0; i < 4; i++)
  61. // {
  62. // BoxDetection[i] = s.GetBitValue((byte)(i + 1));
  63. // }
  64. // });
  65. // Thread.Sleep(250);
  66. // myDevice.SlaveAddress = 2;
  67. // myDevice.Read<float>("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s; });
  68. // Thread.Sleep(250);
  69. // }), $"其它外部设备-{PortName}", true);
  70. //});
  71. }
  72. public void WriteValue<T>(string address, T value)
  73. {
  74. throw new NotImplementedException();
  75. }
  76. /// <summary>
  77. /// 除皮
  78. /// </summary>
  79. /// <returns></returns>
  80. public bool WeigherTare()
  81. {
  82. if (MyModbus is not null && MyModbus.IsConnected())
  83. {
  84. //ModbusRtu weigher = (ModbusRtu)MyModbus;
  85. //MyModbus.SlaveAddress = 2;
  86. return MyModbus.Write<bool>("LB4".ToModbusAdd(), true).IsSuccess;
  87. }
  88. else
  89. return false;
  90. }
  91. /// <summary>
  92. /// 置零
  93. /// </summary>
  94. /// <returns></returns>
  95. public bool WeigherZero()
  96. {
  97. if (MyModbus is not null && MyModbus.IsConnected())
  98. {
  99. //ModbusRtu weigher = (ModbusRtu)MyModbus;
  100. //weigher.SlaveAddress = 2;
  101. return MyModbus.Write<bool>("LB3".ToModbusAdd(), true).IsSuccess;
  102. }
  103. else
  104. return false;
  105. }
  106. }
  107. }