using BPA.Communication; using BPA.Helper; using System; using System.Threading; namespace BPASmartClient.DosingSystem { public class SiemensDevice { private volatile static SiemensDevice _Instance; public static SiemensDevice GetInstance => _Instance ?? (_Instance = new SiemensDevice()); private SiemensDevice() { } public Siemens MySiemens { get; set; } = new Siemens(); private bool IsConnect { get; set; } public bool tempValue = false; public void Connect(string ip) { MySiemens.Connected = () => { TaskManage.GetInstance.StartLong(new Action(() => { if (IsConnect) { MySiemens.Write("DB4.DBX0.0", tempValue);//设备心跳 tempValue = !tempValue; } Thread.Sleep(200); }), "设备心跳", true); TaskManage.GetInstance.StartLong(new Action(() => { GlobalDevice.PlcData = MySiemens.Read(3).Content;//获取PLC到上位机的数据 //var tt = MySiemens.Read(3);//获取PLC到上位机的数据 var res = MySiemens.Read("DB4.DBW134", 32).Content; if (res != null && res is ushort[] ushorts && ushorts.Length == 32) { GlobalDevice.MotorSpeed = ushorts; } uint data = MySiemens.Read("DB5.DBD6").Content;//获取输送带控制信号 //GlobalDevice.MotorControlFeedback = MySiemens.Read("DB3.DBD0");//获取当前输送带运行状态 GlobalDevice.MotorControl = (uint)(data.ToBytes(BPA.Helper.DataFormat.ABCD)).ToInt(); Thread.Sleep(50); }), "读取输送线设备数据", true); }; try { IsConnect = MySiemens.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = "192.168.0.100", cpuType = BPA.Communication.CpuType.S71200, Port = 102 }); } catch (Exception) { //throw; } } /// /// 通过顺序编号获取西门子数据地址 /// /// 地址标头 /// 编号 /// 起始地址 /// public string GetSiemensBitSingleAdd(string Prefix, int num, int StartAdd = 0) { if (num > 0) { string Add = string.Empty; var FirstAdd = num / 8; var EndAdd = (num % 8); if (EndAdd == 0) { FirstAdd--; EndAdd = 7; } else EndAdd--; Add = $"{Prefix}{FirstAdd + StartAdd}.{EndAdd}"; return Add; } return default; } } }