终端一体化运控平台
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
3.2 KiB

  1. using BPA.Communication;
  2. using BPA.Helper;
  3. using System;
  4. using System.Threading;
  5. namespace BPASmartClient.DosingSystem
  6. {
  7. public class SiemensDevice
  8. {
  9. private volatile static SiemensDevice _Instance;
  10. public static SiemensDevice GetInstance => _Instance ?? (_Instance = new SiemensDevice());
  11. private SiemensDevice() { }
  12. public Siemens MySiemens { get; set; } = new Siemens();
  13. private bool IsConnect { get; set; }
  14. public bool tempValue = false;
  15. public void Connect(string ip)
  16. {
  17. MySiemens.Connected = () =>
  18. {
  19. TaskManage.GetInstance.StartLong(new Action(() =>
  20. {
  21. if (IsConnect)
  22. {
  23. MySiemens.Write("DB4.DBX0.0", tempValue);//设备心跳
  24. tempValue = !tempValue;
  25. }
  26. Thread.Sleep(200);
  27. }), "设备心跳", true);
  28. TaskManage.GetInstance.StartLong(new Action(() =>
  29. {
  30. GlobalDevice.PlcData = MySiemens.Read<PlcToComputer>(3).Content;//获取PLC到上位机的数据
  31. //var tt = MySiemens.Read<PlcToComputer>(3);//获取PLC到上位机的数据
  32. var res = MySiemens.Read<ushort[]>("DB4.DBW134", 32).Content;
  33. if (res != null && res is ushort[] ushorts && ushorts.Length == 32)
  34. {
  35. GlobalDevice.MotorSpeed = ushorts;
  36. }
  37. uint data = MySiemens.Read<uint>("DB5.DBD6").Content;//获取输送带控制信号
  38. //GlobalDevice.MotorControlFeedback = MySiemens.Read<uint>("DB3.DBD0");//获取当前输送带运行状态
  39. GlobalDevice.MotorControl = (uint)(data.ToBytes(BPA.Helper.DataFormat.ABCD)).ToInt();
  40. Thread.Sleep(50);
  41. }), "读取输送线设备数据", true);
  42. };
  43. try
  44. {
  45. IsConnect = MySiemens.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = "192.168.0.100", cpuType = BPA.Communication.CpuType.S71200, Port = 102 });
  46. }
  47. catch (Exception)
  48. {
  49. //throw;
  50. }
  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. }