Mejorado UserControl
This commit is contained in:
parent
2addea3c17
commit
b3f5ebc53a
Binary file not shown.
After Width: | Height: | Size: 8.4 KiB |
|
@ -7,8 +7,13 @@
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Icons\connect.png" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
|
||||||
|
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -18,4 +23,8 @@
|
||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Resource Include="Icons\connect.png" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,44 +1,43 @@
|
||||||
<UserControl x:Class="LibS7Adv.PLCControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
<UserControl x:Class="LibS7Adv.PLCControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:LibS7Adv">
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:LibS7Adv"
|
||||||
|
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" >
|
||||||
|
|
||||||
<UserControl.DataContext>
|
<UserControl.DataContext>
|
||||||
<local:PLCViewModel />
|
<local:PLCViewModel />
|
||||||
</UserControl.DataContext>
|
</UserControl.DataContext>
|
||||||
|
|
||||||
|
<UserControl.Resources>
|
||||||
|
|
||||||
|
<!-- Style for Connect/Disconnect Button -->
|
||||||
|
<Style x:Key="ConnectDisconnectButtonStyle" TargetType="Button">
|
||||||
|
<Setter Property="Background" Value="Transparent" />
|
||||||
|
<Style.Triggers>
|
||||||
|
<DataTrigger Binding="{Binding IsConnected}" Value="True">
|
||||||
|
<Setter Property="Background" Value="LightGreen" />
|
||||||
|
</DataTrigger>
|
||||||
|
</Style.Triggers>
|
||||||
|
</Style>
|
||||||
|
|
||||||
|
</UserControl.Resources>
|
||||||
|
|
||||||
<Grid>
|
<Grid>
|
||||||
<Grid.RowDefinitions>
|
<StackPanel>
|
||||||
<RowDefinition Height="*" />
|
<xctk:PropertyGrid MinWidth="300" x:Name="PanelEdicion" AutoGenerateProperties="True"
|
||||||
<RowDefinition Height="*" />
|
SelectedObject="{Binding PlcData}" ShowDescriptionByTooltip="True" ShowSearchBox="False"
|
||||||
<RowDefinition Height="*" />
|
ShowSortOptions="False" ForceCursor="True" Margin="5,5,5,5" />
|
||||||
<RowDefinition Height="*" />
|
|
||||||
<RowDefinition Height="*" />
|
|
||||||
</Grid.RowDefinitions>
|
|
||||||
<Grid.ColumnDefinitions>
|
|
||||||
<ColumnDefinition Width="*" />
|
|
||||||
<ColumnDefinition Width="2*" />
|
|
||||||
</Grid.ColumnDefinitions>
|
|
||||||
|
|
||||||
<Label Content="IP:" Grid.Row="0" Grid.Column="0" Margin="1" VerticalAlignment="Center" />
|
|
||||||
<TextBox Text="{Binding IP}" Grid.Row="0" Grid.Column="1" Margin="1"
|
|
||||||
VerticalAlignment="Center" />
|
|
||||||
|
|
||||||
<Label Content="Name:" Grid.Row="1" Grid.Column="0" Margin="1" VerticalAlignment="Center" />
|
|
||||||
<TextBox Text="{Binding Name}" Grid.Row="1" Grid.Column="1" Margin="1"
|
|
||||||
VerticalAlignment="Center" />
|
|
||||||
|
|
||||||
<Button Content="Connect" Command="{Binding ConnectCommand}" Grid.Row="2" Grid.Column="0" Margin="1"
|
|
||||||
VerticalAlignment="Center" />
|
|
||||||
<Button Content="Disconnect" Command="{Binding DisconnectCommand}" Grid.Row="2" Grid.Column="1" Margin="1"
|
|
||||||
VerticalAlignment="Center" />
|
|
||||||
|
|
||||||
<Label Content="Status:" Grid.Row="3" Grid.Column="0" Margin="1" VerticalAlignment="Center" />
|
|
||||||
<Label Content="{Binding ConnectionStatus}" Grid.Row="3" Grid.Column="1" Margin="1" VerticalAlignment="Center" />
|
|
||||||
<Label Grid.Row="4" Grid.Column="0" Margin="1" VerticalAlignment="Center">
|
|
||||||
Last Error:
|
|
||||||
</Label>
|
|
||||||
<Label Grid.Row="4" Grid.Column="1" Margin="1" VerticalAlignment="Center" HorizontalAlignment="Stretch">
|
|
||||||
<TextBlock Text="{Binding LastError}" TextWrapping="Wrap" Padding="3" />
|
|
||||||
</Label>
|
|
||||||
|
|
||||||
|
<ToolBarTray>
|
||||||
|
<ToolBar>
|
||||||
|
<Button Command="{Binding ConnectButtonCommand}" ToolTip="Conectar PLC"
|
||||||
|
Style="{StaticResource ConnectDisconnectButtonStyle}">
|
||||||
|
<StackPanel>
|
||||||
|
<Image Source="Icons/connect.png" Width="24" Height="24" />
|
||||||
|
<TextBlock Text="Conectar" />
|
||||||
|
</StackPanel>
|
||||||
|
</Button>
|
||||||
|
</ToolBar>
|
||||||
|
</ToolBarTray>
|
||||||
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
</UserControl>
|
</UserControl>
|
||||||
|
|
156
PLCViewModel.cs
156
PLCViewModel.cs
|
@ -3,18 +3,50 @@ using CommunityToolkit.Mvvm.Input;
|
||||||
using Siemens.Simatic.Simulation.Runtime;
|
using Siemens.Simatic.Simulation.Runtime;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Text.RegularExpressions;
|
using System.ComponentModel;
|
||||||
using System.Threading.Tasks;
|
using Newtonsoft.Json.Linq;
|
||||||
using Siemens.Simatic.Simulation.Runtime;
|
|
||||||
|
|
||||||
namespace LibS7Adv
|
namespace LibS7Adv
|
||||||
{
|
{
|
||||||
|
[DisplayName("PLC Advanced Setup")]
|
||||||
|
public partial class PlcData : ObservableObject
|
||||||
|
{
|
||||||
|
[ObservableProperty]
|
||||||
|
[property: Description("IP of the PLC")]
|
||||||
|
[property: Category("Connection:")]
|
||||||
|
string iP = "10.1.30.11";
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
[property: Description("Name of the instance at the PLC Advanced")]
|
||||||
|
[property: Category("Connection:")]
|
||||||
|
string name = "PLC Name";
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
[property: Description("CPU Info")]
|
||||||
|
[property: Category("Status:")]
|
||||||
|
string cpuTime;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
[property: Description("CPU Status")]
|
||||||
|
[property: Category("Status:")]
|
||||||
|
string connectionStatus;
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
[property: Description("API Error")]
|
||||||
|
[property: Category("Status:")]
|
||||||
|
string lastError;
|
||||||
|
}
|
||||||
|
|
||||||
public partial class PLCViewModel : ObservableObject
|
public partial class PLCViewModel : ObservableObject
|
||||||
{
|
{
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
private IInstance Instance { get; set; }
|
private IInstance Instance { get; set; }
|
||||||
|
|
||||||
private bool IsConfigured { get; set; }
|
private bool IsConfigured { get; set; }
|
||||||
|
|
||||||
|
[ObservableProperty]
|
||||||
|
PlcData plcData = new PlcData();
|
||||||
|
|
||||||
public PLCViewModel()
|
public PLCViewModel()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -23,69 +55,60 @@ namespace LibS7Adv
|
||||||
public void ucLoaded()
|
public void ucLoaded()
|
||||||
{
|
{
|
||||||
IsConnected = false;
|
IsConnected = false;
|
||||||
lastError = "";
|
PlcData.LastError = "";
|
||||||
connectionStatus = "offline";
|
PlcData.ConnectionStatus = "offline";
|
||||||
}
|
}
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
string iP = "10.1.30.11";
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
string name = "PLC Name";
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
string cpuTime;
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
string connectionStatus;
|
|
||||||
|
|
||||||
[ObservableProperty]
|
|
||||||
string lastError;
|
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
[property: JsonIgnore]
|
[property: JsonIgnore]
|
||||||
bool isConnected;
|
bool isConnected;
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
[property: JsonIgnore]
|
[property: JsonIgnore]
|
||||||
|
public void ConnectButton()
|
||||||
|
{
|
||||||
|
if (isConnected)
|
||||||
|
Disconnect();
|
||||||
|
else
|
||||||
|
Connect();
|
||||||
|
}
|
||||||
|
|
||||||
public void Connect()
|
public void Connect()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Implementa la conexión utilizando PLCModel
|
// Implementa la conexión utilizando PLCModel
|
||||||
Instance = SimulationRuntimeManager.CreateInterface(Name);
|
Instance = SimulationRuntimeManager.CreateInterface(PlcData.Name);
|
||||||
Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
|
Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
|
||||||
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
|
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
|
||||||
|
|
||||||
if (Instance != null)
|
if (Instance != null)
|
||||||
{
|
{
|
||||||
UpdateTagList();
|
UpdateTagList();
|
||||||
ConnectionStatus = Instance.OperatingState.ToString();
|
PlcData.ConnectionStatus = Instance.OperatingState.ToString();
|
||||||
IsConnected = true;
|
IsConnected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = ex.Message;
|
PlcData.LastError = ex.Message;
|
||||||
ConnectionStatus = "offline";
|
PlcData.ConnectionStatus = "offline";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Disconnect()
|
||||||
|
{
|
||||||
|
IsConnected = false;
|
||||||
|
PlcData.ConnectionStatus = "offline";
|
||||||
|
Instance = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
|
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
|
||||||
{
|
{
|
||||||
UpdateTagList();
|
UpdateTagList();
|
||||||
}
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
|
||||||
[property: JsonIgnore]
|
|
||||||
public void Disconnect()
|
|
||||||
{
|
|
||||||
IsConnected = false;
|
|
||||||
ConnectionStatus = "offline";
|
|
||||||
Instance = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void UpdateTagList()
|
public void UpdateTagList()
|
||||||
{
|
{
|
||||||
IsConfigured = false;
|
IsConfigured = false;
|
||||||
|
@ -96,7 +119,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = ex.Message;
|
PlcData.LastError = ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +129,7 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
if (!isConnected)
|
if (!isConnected)
|
||||||
{
|
{
|
||||||
LastError = "Not Connected";
|
PlcData.LastError = "Not Connected";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (sTag == null)
|
if (sTag == null)
|
||||||
|
@ -128,7 +151,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = sTag + ":" + ex.Message;
|
PlcData.LastError = sTag + ":" + ex.Message;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,7 +162,7 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
if (!isConnected)
|
if (!isConnected)
|
||||||
{
|
{
|
||||||
LastError = "Not Connected";
|
PlcData.LastError = "Not Connected";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (sTag == null)
|
if (sTag == null)
|
||||||
|
@ -162,19 +185,19 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = sTag + ":" + ex.Message;
|
PlcData.LastError = sTag + ":" + ex.Message;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public string? LeerNumber(string sTag)
|
public string? LeerNumber(string sTag, bool Signed = false)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!isConnected)
|
if (!isConnected)
|
||||||
{
|
{
|
||||||
LastError = "Not Connected";
|
PlcData.LastError = "Not Connected";
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if (sTag == null)
|
if (sTag == null)
|
||||||
|
@ -190,26 +213,51 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
// Read not Work for ImputArea
|
// Read not Work for ImputArea
|
||||||
if (tag.areaType == EArea.Output)
|
if (tag.areaType == EArea.Output)
|
||||||
return Instance?.OutputArea.ReadByte(tag.word_offset).ToString();
|
{
|
||||||
|
var val = Instance?.OutputArea.ReadByte(tag.word_offset);
|
||||||
|
if (Signed)
|
||||||
|
return val > 127 ? (val - 256).ToString() : val.ToString();
|
||||||
|
return val.ToString();
|
||||||
|
}
|
||||||
if (tag.areaType == EArea.Marker)
|
if (tag.areaType == EArea.Marker)
|
||||||
return Instance?.MarkerArea.ReadByte(tag.word_offset).ToString();
|
{
|
||||||
|
var val = Instance?.MarkerArea.ReadByte(tag.word_offset);
|
||||||
|
if (Signed)
|
||||||
|
return val > 127 ? (val-256).ToString() : val.ToString();
|
||||||
|
return val.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (tag.tagType == EDataType.Word)
|
else if (tag.tagType == EDataType.Word)
|
||||||
{
|
{
|
||||||
// Read not Work for ImputArea
|
// Read not Work for ImputArea
|
||||||
if (tag.areaType == EArea.Output)
|
if (tag.areaType == EArea.Output)
|
||||||
return Instance?.OutputArea.ReadBytes(tag.word_offset,2).ToString();
|
{
|
||||||
|
int value;
|
||||||
|
byte[] bytes = Instance?.OutputArea.ReadBytes(tag.word_offset, 2);
|
||||||
|
Array.Reverse(bytes);
|
||||||
|
if (Signed)
|
||||||
|
value = BitConverter.ToInt16(bytes, 0);
|
||||||
|
else
|
||||||
|
value = BitConverter.ToUInt16(bytes, 0);
|
||||||
|
return value.ToString();
|
||||||
|
}
|
||||||
if (tag.areaType == EArea.Marker)
|
if (tag.areaType == EArea.Marker)
|
||||||
{
|
{
|
||||||
|
int value;
|
||||||
byte[] bytes = Instance?.MarkerArea.ReadBytes(tag.word_offset, 2);
|
byte[] bytes = Instance?.MarkerArea.ReadBytes(tag.word_offset, 2);
|
||||||
return (bytes[0] + bytes[1]*256).ToString();
|
Array.Reverse(bytes);
|
||||||
|
if (Signed)
|
||||||
|
value = BitConverter.ToInt16(bytes, 0);
|
||||||
|
else
|
||||||
|
value = BitConverter.ToUInt16(bytes, 0);
|
||||||
|
return value.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = sTag + ":" + ex.Message;
|
PlcData.LastError = sTag + ":" + ex.Message;
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -224,7 +272,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = ex.Message;
|
PlcData.LastError = ex.Message;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,7 +285,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = ex.Message;
|
PlcData.LastError = ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void EscribirTag(string pTag, SDataValue Value)
|
public void EscribirTag(string pTag, SDataValue Value)
|
||||||
|
@ -248,7 +296,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public SDataValue LeerTag(string pTag)
|
public SDataValue LeerTag(string pTag)
|
||||||
|
@ -259,7 +307,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
return new SDataValue();
|
return new SDataValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -271,7 +319,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void EscribirTagInt16(string pTag, int pValue)
|
public void EscribirTagInt16(string pTag, int pValue)
|
||||||
|
@ -282,7 +330,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,7 +342,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -307,7 +355,7 @@ namespace LibS7Adv
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue