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

94 lines
3.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using BPASmartClient.S7Net;
  8. using BPA.Helper;
  9. using S7.Net;
  10. using BPASmartClient.CustomResource.Pages.Model;
  11. namespace BPASmartClient.DosingSystem
  12. {
  13. public class SiemensDevice
  14. {
  15. private volatile static SiemensDevice _Instance;
  16. public static SiemensDevice GetInstance => _Instance ?? (_Instance = new SiemensDevice());
  17. private SiemensDevice() { }
  18. public SiemensHelper MySiemens { get; set; } = new SiemensHelper();
  19. private bool IsConnect { get; set; }
  20. public bool tempValue = false;
  21. public void Connect(string ip)
  22. {
  23. ThreadManage.GetInstance().StartLong(new Action(() =>
  24. {
  25. if (IsConnect)
  26. {
  27. MySiemens.Write("DB4.DBX0.0", tempValue);//设备心跳
  28. tempValue = !tempValue;
  29. }
  30. Thread.Sleep(200);
  31. }), "设备心跳", true);
  32. ThreadManage.GetInstance().StartLong(new Action(() =>
  33. {
  34. try
  35. {
  36. while (!MySiemens.IsConnected)
  37. {
  38. MySiemens.Connect(S7.Net.CpuType.S71200, ip);
  39. Thread.Sleep(2000);
  40. }
  41. }
  42. catch (Exception ex)
  43. {
  44. }
  45. IsConnect = MySiemens.IsConnected;
  46. if (IsConnect)
  47. {
  48. GlobalDevice.PlcData = MySiemens.ReadClass<PlcToComputer>(3);//获取PLC到上位机的数据
  49. var res = MySiemens.Read(DataType.DataBlock, 4, 134, VarType.Word, 32);
  50. if (res != null && res is ushort[] ushorts && ushorts.Length == 32)
  51. {
  52. GlobalDevice.MotorSpeed = ushorts;
  53. }
  54. uint data = MySiemens.Read<uint>("DB5.DBD6");//获取输送带控制信号
  55. //GlobalDevice.MotorControlFeedback = MySiemens.Read<uint>("DB3.DBD0");//获取当前输送带运行状态
  56. GlobalDevice.MotorControl = (uint)(data.ToBytes(BPA.Helper.DataFormat.ABCD)).ToInt();
  57. }
  58. Thread.Sleep(50);
  59. }), "读取输送线设备数据", true);
  60. }
  61. /// <summary>
  62. /// 通过顺序编号获取西门子数据地址
  63. /// </summary>
  64. /// <param name="Prefix">地址标头</param>
  65. /// <param name="num">编号</param>
  66. /// <param name="StartAdd">起始地址</param>
  67. /// <returns></returns>
  68. public string GetSiemensBitSingleAdd(string Prefix, int num, int StartAdd = 0)
  69. {
  70. if (num > 0)
  71. {
  72. string Add = string.Empty;
  73. var FirstAdd = num / 8;
  74. var EndAdd = (num % 8);
  75. if (EndAdd == 0)
  76. {
  77. FirstAdd--;
  78. EndAdd = 7;
  79. }
  80. else EndAdd--;
  81. Add = $"{Prefix}{FirstAdd + StartAdd}.{EndAdd}";
  82. return Add;
  83. }
  84. return default;
  85. }
  86. }
  87. }