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

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