|
- using BPA.Communication;
- using BPA.Helper;
- using BPASmartClient.DosingSystem.Model;
- using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Threading;
-
- namespace BPASmartClient.DosingSystem
- {
- public class SimensSend
- {
- private volatile static SimensSend _Instance;
- public static SimensSend GetInstance => _Instance ?? (_Instance = new SimensSend());
- private SimensSend() { }
-
- static string ip = "192.168.2.200";
- public ObservableCollection<SendCtrlModel> sendCtrlModels { get; set; } = new ObservableCollection<SendCtrlModel>();
- public SendCtrlModel sendCtrlModel { get; set; } = new SendCtrlModel();
- public Siemens SendSimens { get; set; } = new Siemens();
- private bool IsConnect { get; set; }
- public bool tempValue = false;
- public void Connect()
- {
- try
- {
-
- IsConnect = SendSimens.Connect(new BPA.Communication.Base.ConfigurationOptions() { IpAddress = "192.168.2.200", cpuType = BPA.Communication.CpuType.S71200, Port = 102 });
-
- }
- catch (Exception)
- {
-
- //throw;
- }
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- if (IsConnect)
- {
- SendSimens.Write("DB1.DBX704.0", tempValue);//设备心跳
- tempValue = !tempValue;
- }
- Thread.Sleep(1000);
- }), "设备心跳", true);
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- //App.Current.Dispatcher.Invoke(() => {
- if (IsConnect)
- {
- sendCtrlModel.SendInfo = SendSimens.Read<ushort>("DB1.DBX700.0").Content.Tobool();
- sendCtrlModel.Stop = SendSimens.Read<ushort>("I0.0").Content.Tobool();
- App.Current.Dispatcher.Invoke(() =>
- {
- if (sendCtrlModel.CylinderModels.Count > 0)
- {
- sendCtrlModel.CylinderModels.Clear();
-
- }
- sendCtrlModel.CylinderModels.Add(new CylinderModel() { Name = "气缸3", OnStatus = SendSimens.Read<ushort>("I0.1").Content.Tobool(), OffStatus = SendSimens.Read<ushort>("I0.2").Content.Tobool() });
- sendCtrlModel.CylinderModels.Add(new CylinderModel() { Name = "气缸2", OnStatus = SendSimens.Read<ushort>("I0.4").Content.Tobool(), OffStatus = SendSimens.Read<ushort>("I0.3").Content.Tobool() });
- sendCtrlModel.CylinderModels.Add(new CylinderModel() { Name = "气缸1", OnStatus = SendSimens.Read<ushort>("I0.6").Content.Tobool(), OffStatus = SendSimens.Read<ushort>("I0.5").Content.Tobool() });
-
-
- });
-
- sendCtrlModel.OutputOptical_1 = SendSimens.Read<ushort>("I0.7").Content.Tobool();
- sendCtrlModel.OutputOptical_2 = SendSimens.Read<ushort>("I1.0").Content.Tobool();
- sendCtrlModel.OutputOptical_3 = SendSimens.Read<ushort>("I1.1").Content.Tobool();
- sendCtrlModel.InputOptical_1 = SendSimens.Read<ushort>("I1.2").Content.Tobool();
- sendCtrlModel.InSend = SendSimens.Read<ushort>("DB1.DBX300.0").Content.Tobool();
- sendCtrlModel.OutSend = SendSimens.Read<ushort>("DB1.DBX300.1").Content.Tobool();
- sendCtrlModel.CacheSend = SendSimens.Read<ushort>("DB1.DBX300.2").Content.Tobool();
- sendCtrlModel.Clinder_1 = SendSimens.Read<ushort>("DB1.DBX300.3").Content.Tobool();
- sendCtrlModel.Clinder_2 = SendSimens.Read<ushort>("DB1.DBX300.4").Content.Tobool();
- sendCtrlModel.Clinder_3 = SendSimens.Read<ushort>("DB1.DBX300.5").Content.Tobool();
- sendCtrlModel.InSendSp = SendSimens.Read<ushort>("DB1.DBW100").Content;
- sendCtrlModel.OutSendSp = SendSimens.Read<ushort>("DB1.DBW100").Content;
- sendCtrlModel.CacheSendSp = SendSimens.Read<ushort>("DB1.DBW100").Content;
- }
- //});
-
- Thread.Sleep(100);
- }), "读取输送线设备数据", true);
- }
- }
- 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<PlcToComputer>(3).Content;//获取PLC到上位机的数据
- //var tt = MySiemens.Read<PlcToComputer>(3);//获取PLC到上位机的数据
- var res = MySiemens.Read<ushort[]>("DB4.DBW134", 32).Content;
- if (res != null && res is ushort[] ushorts && ushorts.Length == 32)
- {
- GlobalDevice.MotorSpeed = ushorts;
- }
- uint data = MySiemens.Read<uint>("DB5.DBD6").Content;//获取输送带控制信号
- //GlobalDevice.MotorControlFeedback = MySiemens.Read<uint>("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.2.200", cpuType = BPA.Communication.CpuType.S71200, Port = 102 });
-
- }
- catch (Exception)
- {
-
- //throw;
- }
- }
-
- /// <summary>
- /// 通过顺序编号获取西门子数据地址
- /// </summary>
- /// <param name="Prefix">地址标头</param>
- /// <param name="num">编号</param>
- /// <param name="StartAdd">起始地址</param>
- /// <returns></returns>
- 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;
- }
-
- }
- }
|