using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using BPASmartClient.S7Net; using BPA.Helper; using S7.Net; namespace BPASmartClient.DosingSystemSingle { public class SiemensDevice { private volatile static SiemensDevice _Instance; public static SiemensDevice GetInstance => _Instance ?? (_Instance = new SiemensDevice()); private SiemensDevice() { } public SiemensHelper MySiemens { get; set; } = new SiemensHelper(); private bool IsConnect { get; set; } bool tempValue = false; public void Connect(string ip) { try { while (!MySiemens.IsConnected) { MySiemens.Connect(S7.Net.CpuType.S71200, ip); Thread.Sleep(2000); } } catch (Exception ex) { } IsConnect = MySiemens.IsConnected; ThreadManage.GetInstance().StartLong(new Action(() => { if (IsConnect) MySiemens.Write("DB4.DBX0.0", tempValue);//设备心跳 tempValue = !tempValue; Thread.Sleep(100); }), "设备心跳", true); ThreadManage.GetInstance().StartLong(new Action(() => { if (IsConnect) { GlobalDevice.PlcData = MySiemens.ReadClass(3);//获取PLC到上位机的数据 var res = MySiemens.Read(DataType.DataBlock, 4, 134, VarType.Word, 32); if (res != null && res is ushort[] ushorts && ushorts.Length == 32) { GlobalDevice.MotorSpeed = ushorts; } GlobalDevice.MotorControl = MySiemens.Read("DB5.DBD6");//获取输送带控制信号 //GlobalDevice.MotorControlFeedback = MySiemens.Read("DB3.DBD0");//获取当前输送带运行状态 GlobalDevice.MotorControl = (uint)(GlobalDevice.MotorControl.ToBytes(BPA.Helper.DataFormat.ABCD)).ToInt(); Thread.Sleep(50); } }), "读取输送线设备数据", true); } /// /// 通过顺序编号获取西门子数据地址 /// /// 地址标头 /// 编号 /// 起始地址 /// 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; } } }