终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

91 linhas
3.1 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.DosingSystem
  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. public void Connect(string ip)
  20. {
  21. //GlobalDevice.PlcData = MySiemens.ReadClass<PlcToComputer>(3);//获取PLC到上位机的数据
  22. while (!MySiemens.IsConnected)
  23. {
  24. MySiemens.Connect(S7.Net.CpuType.S71200, ip);
  25. Thread.Sleep(2000);
  26. }
  27. IsConnect = MySiemens.IsConnected;
  28. bool tempValue = false;
  29. ThreadManage.GetInstance().StartLong(new Action(() =>
  30. {
  31. if (IsConnect) MySiemens.Write("DB4.DBX0.0", tempValue);//设备心跳
  32. tempValue = !tempValue;
  33. Thread.Sleep(1);
  34. }), "设备心跳", true);
  35. ThreadManage.GetInstance().StartLong(new Action(() =>
  36. {
  37. if (IsConnect)
  38. {
  39. //测试使用
  40. GlobalDevice.PlcData = MySiemens.ReadClass<PlcToComputer>(3);//获取PLC到上位机的数据
  41. var res = MySiemens.Read(DataType.DataBlock, 4, 134, VarType.Word, 32);
  42. if (res != null && res is ushort[] ushorts && ushorts.Length == 32)
  43. {
  44. GlobalDevice.MotorSpeed = ushorts;
  45. }
  46. GlobalDevice.MotorControl = MySiemens.Read<uint>("DB5.DBD6");//获取输送带控制信号
  47. //GlobalDevice.MotorControlFeedback = MySiemens.Read<uint>("DB3.DBD0");//获取当前输送带运行状态
  48. Thread.Sleep(50);
  49. }
  50. }), "读取输送线设备数据", true);
  51. }
  52. /// <summary>
  53. /// 通过顺序编号获取西门子数据地址
  54. /// </summary>
  55. /// <param name="Prefix">地址标头</param>
  56. /// <param name="num">编号</param>
  57. /// <param name="StartAdd">起始地址</param>
  58. /// <returns></returns>
  59. public string GetSiemensBitSingleAdd(string Prefix, int num, int StartAdd = 0)
  60. {
  61. if (num > 0)
  62. {
  63. string Add = string.Empty;
  64. var FirstAdd = num / 8;
  65. var EndAdd = (num % 8);
  66. if (EndAdd == 0)
  67. {
  68. FirstAdd--;
  69. EndAdd = 7;
  70. }
  71. else EndAdd--;
  72. Add = $"{Prefix}{FirstAdd + StartAdd}.{EndAdd}";
  73. return Add;
  74. }
  75. return default;
  76. }
  77. }
  78. }