CtrEditor/Siemens/PLCControl.xaml.cs

240 lines
6.3 KiB
C#
Raw Normal View History

using Siemens.Simatic.Simulation.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json.Serialization;
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;
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; }
public event PropertyChangedEventHandler PropertyChanged;
public PLCViewModel()
{
2024-05-11 15:55:44 -03:00
IsConnected = false;
PLCInterface = new PLCModel();
_timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(1000) };
_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();
}
}
[JsonIgnore]
public ICommand ConnectCommand { get; }
public ICommand DisconnectCommand { get; }
public string LastError
{
get => lastError;
set
{
lastError = value;
OnPropertyChanged();
}
}
private void Connect()
{
// Implementa la conexión utilizando PLCModel
2024-05-11 15:55:44 -03:00
PLCInterface.Instance = SimulationRuntimeManager.CreateInterface(Name);
PLCInterface.Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
2024-05-11 15:55:44 -03:00
if (PLCInterface.Instance != null)
{
2024-05-11 15:55:44 -03:00
PLCInterface.UpdateTagList();
_timer.Start();
ConnectionStatus = "connected";
2024-05-11 15:55:44 -03:00
IsConnected = true;
}
}
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
{
2024-05-11 15:55:44 -03:00
PLCInterface.UpdateTagList();
}
private void Disconnect()
{
2024-05-11 15:55:44 -03:00
IsConnected = false;
_timer.Stop();
ConnectionStatus = "offline";
2024-05-11 15:55:44 -03:00
PLCInterface.Instance = null;
}
private void Refresh()
{
2024-05-11 15:55:44 -03:00
if (PLCInterface.Instance != null)
{
2024-05-11 15:55:44 -03:00
CpuTime = PLCInterface.LeerInt16("\"DB HMI\".CPU_Scan_Time")?.ToString() ?? "N/A";
LastError = PLCInterface.LastError;
}
}
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
{
Instance?.UpdateTagList( ETagListDetails.IO | ETagListDetails.DB);
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;
}
}
public void EscribirTagBool(string pTag, bool pValue)
{
try
{
Instance?.WriteBool(pTag, pValue);
}
catch (Exception ex)
{
LastError = pTag + ":" + ex.Message;
}
}
public bool LeerTagBool(string pTag)
{
try
{
return Instance?.ReadBool(pTag) ?? false;
}
catch (Exception ex)
{
LastError = pTag + ":" + ex.Message;
return false;
}
}
public int? LeerInt16(string pTag)
{
try
{
return Instance?.ReadInt16(pTag);
}
catch (Exception ex)
{
LastError = pTag + ":" + ex.Message;
return 0;
}
}
}
public partial class PLCControl : UserControl
{
public PLCControl()
{
InitializeComponent();
}
}
}