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

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