2024-05-10 17:17:57 -03:00
|
|
|
|
using Siemens.Simatic.Simulation.Runtime;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text;
|
2024-05-10 20:40:51 -03:00
|
|
|
|
using System.Text.Json.Serialization;
|
2024-05-10 17:17:57 -03:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows.Documents;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
using System.Windows.Navigation;
|
|
|
|
|
using System.Windows.Shapes;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
|
|
|
|
namespace CtrEditor.Siemens
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaction logic for PLCControl.xaml
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PLCViewModel : INotifyPropertyChanged
|
|
|
|
|
{
|
2024-05-11 15:55:44 -03:00
|
|
|
|
public readonly PLCModel PLCInterface;
|
2024-05-10 17:17:57 -03:00
|
|
|
|
private readonly DispatcherTimer _timer;
|
|
|
|
|
private string _cpuTime;
|
|
|
|
|
private string _connectionStatus = "offline";
|
|
|
|
|
private string _ip = "10.1.30.11";
|
|
|
|
|
private string _name = "PLC";
|
|
|
|
|
private string lastError;
|
2024-05-11 15:55:44 -03:00
|
|
|
|
public bool IsConnected { get; private set; }
|
2024-05-10 17:17:57 -03:00
|
|
|
|
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
2024-05-14 09:15:10 -03:00
|
|
|
|
public event EventHandler RefreshEvent;
|
|
|
|
|
|
2024-05-10 17:17:57 -03:00
|
|
|
|
public PLCViewModel()
|
|
|
|
|
{
|
2024-05-11 15:55:44 -03:00
|
|
|
|
IsConnected = false;
|
|
|
|
|
PLCInterface = new PLCModel();
|
2024-05-25 07:53:34 -03:00
|
|
|
|
_timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(30) };
|
2024-05-10 17:17:57 -03:00
|
|
|
|
_timer.Tick += (s, e) => Refresh();
|
|
|
|
|
|
|
|
|
|
ConnectCommand = new RelayCommand(Connect, () => true);
|
|
|
|
|
DisconnectCommand = new RelayCommand(Disconnect, () => true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string IP
|
|
|
|
|
{
|
|
|
|
|
get => _ip;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_ip = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get => _name;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_name = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string CpuTime
|
|
|
|
|
{
|
|
|
|
|
get => _cpuTime;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_cpuTime = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ConnectionStatus
|
|
|
|
|
{
|
|
|
|
|
get => _connectionStatus;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_connectionStatus = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 20:40:51 -03:00
|
|
|
|
[JsonIgnore]
|
2024-05-10 17:17:57 -03:00
|
|
|
|
public ICommand ConnectCommand { get; }
|
|
|
|
|
public ICommand DisconnectCommand { get; }
|
|
|
|
|
public string LastError
|
|
|
|
|
{
|
|
|
|
|
get => lastError;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
lastError = value;
|
|
|
|
|
OnPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 12:10:32 -03:00
|
|
|
|
public void Connect()
|
2024-05-10 17:17:57 -03:00
|
|
|
|
{
|
2024-05-15 06:20:09 -03:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Implementa la conexión utilizando PLCModel
|
|
|
|
|
PLCInterface.Instance = SimulationRuntimeManager.CreateInterface(Name);
|
|
|
|
|
PLCInterface.Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
|
|
|
|
|
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
|
2024-05-10 17:17:57 -03:00
|
|
|
|
|
2024-05-15 06:20:09 -03:00
|
|
|
|
if (PLCInterface.Instance != null)
|
|
|
|
|
{
|
|
|
|
|
PLCInterface.UpdateTagList();
|
|
|
|
|
_timer.Start();
|
|
|
|
|
ConnectionStatus = "connected";
|
|
|
|
|
IsConnected = true;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex)
|
2024-05-10 17:17:57 -03:00
|
|
|
|
{
|
2024-05-15 06:20:09 -03:00
|
|
|
|
LastError = ex.Message;
|
|
|
|
|
ConnectionStatus = "offline";
|
2024-05-10 17:17:57 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
|
|
|
|
|
{
|
2024-05-11 15:55:44 -03:00
|
|
|
|
PLCInterface.UpdateTagList();
|
2024-05-10 17:17:57 -03:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 12:10:32 -03:00
|
|
|
|
public void Disconnect()
|
2024-05-10 17:17:57 -03:00
|
|
|
|
{
|
2024-05-11 15:55:44 -03:00
|
|
|
|
IsConnected = false;
|
2024-05-10 17:17:57 -03:00
|
|
|
|
_timer.Stop();
|
|
|
|
|
ConnectionStatus = "offline";
|
2024-05-11 15:55:44 -03:00
|
|
|
|
PLCInterface.Instance = null;
|
2024-05-10 17:17:57 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Refresh()
|
|
|
|
|
{
|
2024-05-11 15:55:44 -03:00
|
|
|
|
if (PLCInterface.Instance != null)
|
2024-05-10 17:17:57 -03:00
|
|
|
|
{
|
2024-05-14 09:15:10 -03:00
|
|
|
|
CpuTime = PLCInterface.LeerTagInt16("\"DB HMI\".CPU_Scan_Time")?.ToString() ?? "N/A";
|
2024-05-11 15:55:44 -03:00
|
|
|
|
LastError = PLCInterface.LastError;
|
2024-05-14 09:15:10 -03:00
|
|
|
|
|
2024-05-14 12:10:32 -03:00
|
|
|
|
// Disparar el evento RefreshEvent
|
|
|
|
|
RefreshEvent?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
2024-05-10 17:17:57 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PLCModel
|
|
|
|
|
{
|
|
|
|
|
public IInstance Instance { get; set; }
|
|
|
|
|
public bool IsConfigured { get; set; }
|
|
|
|
|
public string LastError { get; set; }
|
|
|
|
|
|
|
|
|
|
public void UpdateTagList()
|
|
|
|
|
{
|
|
|
|
|
IsConfigured = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-23 14:56:14 -03:00
|
|
|
|
Instance?.UpdateTagList(ETagListDetails.IO | ETagListDetails.DB, true); // ETagListDetails.IO | ETagListDetails.DB
|
2024-05-10 17:17:57 -03:00
|
|
|
|
IsConfigured = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = ex.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool LeerSalidaBool(byte pByte, int pBit)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Instance?.OutputArea.ReadBit(pByte, (byte)pBit) ?? false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = ex.Message;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EscribirInputBool(byte pByte, int pBit, bool pValue)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Instance?.InputArea.WriteBit(pByte, (byte)pBit, pValue);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = ex.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-14 13:17:46 -03:00
|
|
|
|
public void EscribirTag(string pTag, SDataValue Value)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Instance?.Write(pTag, Value);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = pTag + ":" + ex.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public SDataValue LeerTag(string pTag)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Instance.Read(pTag);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = pTag + ":" + ex.Message;
|
|
|
|
|
return new SDataValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-10 17:17:57 -03:00
|
|
|
|
public void EscribirTagBool(string pTag, bool pValue)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Instance?.WriteBool(pTag, pValue);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = pTag + ":" + ex.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-14 07:04:22 -03:00
|
|
|
|
public void EscribirTagInt16(string pTag, int pValue)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Instance?.WriteInt16(pTag,(short) pValue);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = pTag + ":" + ex.Message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 17:17:57 -03:00
|
|
|
|
public bool LeerTagBool(string pTag)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Instance?.ReadBool(pTag) ?? false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = pTag + ":" + ex.Message;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 07:04:22 -03:00
|
|
|
|
public int? LeerTagInt16(string pTag)
|
2024-05-10 17:17:57 -03:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return Instance?.ReadInt16(pTag);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LastError = pTag + ":" + ex.Message;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public partial class PLCControl : UserControl
|
|
|
|
|
{
|
|
|
|
|
public PLCControl()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|