终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

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