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

166 lines
7.5 KiB

  1. using BPA.Communication;
  2. using BPA.Helper;
  3. using BPASmartClient.DosingSystem.Model;
  4. using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Threading;
  9. namespace BPASmartClient.DosingSystem
  10. {
  11. public class SimensSend
  12. {
  13. private volatile static SimensSend _Instance;
  14. public static SimensSend GetInstance => _Instance ?? (_Instance = new SimensSend());
  15. private SimensSend() { }
  16. static string ip = "192.168.2.200";
  17. public ObservableCollection<SendCtrlModel> sendCtrlModels { get; set; } = new ObservableCollection<SendCtrlModel>();
  18. public SendCtrlModel sendCtrlModel { get; set; } = new SendCtrlModel();
  19. public Siemens SendSimens { get; set; } = new Siemens();
  20. private bool IsConnect { get; set; }
  21. public bool tempValue = false;
  22. public void Connect()
  23. {
  24. try
  25. {
  26. IsConnect = SendSimens.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = "192.168.2.200", cpuType = BPA.Communication.CpuType.S71200, Port = 102 });
  27. }
  28. catch (Exception)
  29. {
  30. //throw;
  31. }
  32. TaskManage.GetInstance.StartLong(new Action(() =>
  33. {
  34. if (IsConnect)
  35. {
  36. SendSimens.Write("DB1.DBX704.0", tempValue);//设备心跳
  37. tempValue = !tempValue;
  38. }
  39. Thread.Sleep(1000);
  40. }), "设备心跳", true);
  41. TaskManage.GetInstance.StartLong(new Action(() =>
  42. {
  43. //App.Current.Dispatcher.Invoke(() => {
  44. if (IsConnect)
  45. {
  46. sendCtrlModel.SendInfo = SendSimens.Read<ushort>("DB1.DBX700.0").Content.Tobool();
  47. sendCtrlModel.Stop = SendSimens.Read<ushort>("I0.0").Content.Tobool();
  48. App.Current.Dispatcher.Invoke(() =>
  49. {
  50. if (sendCtrlModel.CylinderModels.Count > 0)
  51. {
  52. sendCtrlModel.CylinderModels.Clear();
  53. }
  54. sendCtrlModel.CylinderModels.Add(new CylinderModel() { Name = "气缸3", OnStatus = SendSimens.Read<ushort>("I0.1").Content.Tobool(), OffStatus = SendSimens.Read<ushort>("I0.2").Content.Tobool() });
  55. sendCtrlModel.CylinderModels.Add(new CylinderModel() { Name = "气缸2", OnStatus = SendSimens.Read<ushort>("I0.4").Content.Tobool(), OffStatus = SendSimens.Read<ushort>("I0.3").Content.Tobool() });
  56. sendCtrlModel.CylinderModels.Add(new CylinderModel() { Name = "气缸1", OnStatus = SendSimens.Read<ushort>("I0.6").Content.Tobool(), OffStatus = SendSimens.Read<ushort>("I0.5").Content.Tobool() });
  57. });
  58. sendCtrlModel.OutputOptical_1 = SendSimens.Read<ushort>("I0.7").Content.Tobool();
  59. sendCtrlModel.OutputOptical_2 = SendSimens.Read<ushort>("I1.0").Content.Tobool();
  60. sendCtrlModel.OutputOptical_3 = SendSimens.Read<ushort>("I1.1").Content.Tobool();
  61. sendCtrlModel.InputOptical_1 = SendSimens.Read<ushort>("I1.2").Content.Tobool();
  62. sendCtrlModel.InSend = SendSimens.Read<ushort>("DB1.DBX300.0").Content.Tobool();
  63. sendCtrlModel.OutSend = SendSimens.Read<ushort>("DB1.DBX300.1").Content.Tobool();
  64. sendCtrlModel.CacheSend = SendSimens.Read<ushort>("DB1.DBX300.2").Content.Tobool();
  65. sendCtrlModel.Clinder_1 = SendSimens.Read<ushort>("DB1.DBX300.3").Content.Tobool();
  66. sendCtrlModel.Clinder_2 = SendSimens.Read<ushort>("DB1.DBX300.4").Content.Tobool();
  67. sendCtrlModel.Clinder_3 = SendSimens.Read<ushort>("DB1.DBX300.5").Content.Tobool();
  68. sendCtrlModel.InSendSp = SendSimens.Read<ushort>("DB1.DBW100").Content;
  69. sendCtrlModel.OutSendSp = SendSimens.Read<ushort>("DB1.DBW100").Content;
  70. sendCtrlModel.CacheSendSp = SendSimens.Read<ushort>("DB1.DBW100").Content;
  71. }
  72. //});
  73. Thread.Sleep(100);
  74. }), "读取输送线设备数据", true);
  75. }
  76. }
  77. public class SiemensDevice
  78. {
  79. private volatile static SiemensDevice _Instance;
  80. public static SiemensDevice GetInstance => _Instance ?? (_Instance = new SiemensDevice());
  81. private SiemensDevice() { }
  82. public Siemens MySiemens { get; set; } = new Siemens();
  83. private bool IsConnect { get; set; }
  84. public bool tempValue = false;
  85. public void Connect(string ip)
  86. {
  87. MySiemens.Connected = () =>
  88. {
  89. TaskManage.GetInstance.StartLong(new Action(() =>
  90. {
  91. if (IsConnect)
  92. {
  93. MySiemens.Write("DB4.DBX0.0", tempValue);//设备心跳
  94. tempValue = !tempValue;
  95. }
  96. Thread.Sleep(200);
  97. }), "设备心跳", true);
  98. TaskManage.GetInstance.StartLong(new Action(() =>
  99. {
  100. GlobalDevice.PlcData = MySiemens.Read<PlcToComputer>(3).Content;//获取PLC到上位机的数据
  101. //var tt = MySiemens.Read<PlcToComputer>(3);//获取PLC到上位机的数据
  102. var res = MySiemens.Read<ushort[]>("DB4.DBW134", 32).Content;
  103. if (res != null && res is ushort[] ushorts && ushorts.Length == 32)
  104. {
  105. GlobalDevice.MotorSpeed = ushorts;
  106. }
  107. uint data = MySiemens.Read<uint>("DB5.DBD6").Content;//获取输送带控制信号
  108. //GlobalDevice.MotorControlFeedback = MySiemens.Read<uint>("DB3.DBD0");//获取当前输送带运行状态
  109. GlobalDevice.MotorControl = (uint)(data.ToBytes(BPA.Helper.DataFormat.ABCD)).ToInt();
  110. Thread.Sleep(50);
  111. }), "读取输送线设备数据", true);
  112. };
  113. try
  114. {
  115. IsConnect = MySiemens.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = "192.168.2.200", cpuType = BPA.Communication.CpuType.S71200, Port = 102 });
  116. }
  117. catch (Exception)
  118. {
  119. //throw;
  120. }
  121. }
  122. /// <summary>
  123. /// 通过顺序编号获取西门子数据地址
  124. /// </summary>
  125. /// <param name="Prefix">地址标头</param>
  126. /// <param name="num">编号</param>
  127. /// <param name="StartAdd">起始地址</param>
  128. /// <returns></returns>
  129. public string GetSiemensBitSingleAdd(string Prefix, int num, int StartAdd = 0)
  130. {
  131. if (num > 0)
  132. {
  133. string Add = string.Empty;
  134. var FirstAdd = num / 8;
  135. var EndAdd = (num % 8);
  136. if (EndAdd == 0)
  137. {
  138. FirstAdd--;
  139. EndAdd = 7;
  140. }
  141. else EndAdd--;
  142. Add = $"{Prefix}{FirstAdd + StartAdd}.{EndAdd}";
  143. return Add;
  144. }
  145. return default;
  146. }
  147. }
  148. }