Compare commits
No commits in common. "81643249b9d00a37507cb0f9954b7a8c2672e8e2" and "2addea3c1750b8929a45fbd7b98797e5a03248a0" have entirely different histories.
81643249b9
...
2addea3c17
Binary file not shown.
Before Width: | Height: | Size: 8.4 KiB |
BIN
Icons/reset.png
BIN
Icons/reset.png
Binary file not shown.
Before Width: | Height: | Size: 43 KiB |
|
@ -7,14 +7,8 @@
|
|||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Remove="Icons\connect.png" />
|
||||
<None Remove="Icons\reset.png" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -24,9 +18,4 @@
|
|||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Resource Include="Icons\connect.png" />
|
||||
<Resource Include="Icons\reset.png" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -1,50 +1,44 @@
|
|||
<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:sys="clr-namespace:System;assembly=mscorlib" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" >
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:LibS7Adv">
|
||||
|
||||
<UserControl.DataContext>
|
||||
<local:PLCViewModel />
|
||||
</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>
|
||||
<StackPanel>
|
||||
<xctk:PropertyGrid MinWidth="300" x:Name="PanelEdicion" AutoGenerateProperties="True"
|
||||
SelectedObject="{Binding PlcData}" ShowDescriptionByTooltip="True" ShowSearchBox="False"
|
||||
ShowSortOptions="False" ForceCursor="True" Margin="5,5,5,5" NameColumnWidth="100" />
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="*" />
|
||||
<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>
|
||||
<Button Command="{Binding ResetAlarmButtonCommand}" ToolTip="Reset Alarm"
|
||||
>
|
||||
<StackPanel>
|
||||
<Image Source="Icons/reset.png" Width="24" Height="24" />
|
||||
<TextBlock Text="Reset" />
|
||||
</StackPanel>
|
||||
</Button>
|
||||
</ToolBar>
|
||||
</ToolBarTray>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
|
||||
</UserControl>
|
||||
|
|
172
PLCViewModel.cs
172
PLCViewModel.cs
|
@ -3,55 +3,18 @@ using CommunityToolkit.Mvvm.Input;
|
|||
using Siemens.Simatic.Simulation.Runtime;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.ComponentModel;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Siemens.Simatic.Simulation.Runtime;
|
||||
|
||||
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:")]
|
||||
[property: ReadOnly(true)]
|
||||
string cpuTime;
|
||||
|
||||
[ObservableProperty]
|
||||
[property: Display(Name = "Status")]
|
||||
[property: Description("CPU Status")]
|
||||
[property: Category("Status:")]
|
||||
[property: ReadOnly(true)]
|
||||
string connectionStatus;
|
||||
|
||||
[ObservableProperty]
|
||||
[property: Description("API Error")]
|
||||
[property: Category("Status:")]
|
||||
[property: ReadOnly(true)]
|
||||
string lastError;
|
||||
}
|
||||
|
||||
public partial class PLCViewModel : ObservableObject
|
||||
{
|
||||
[JsonIgnore]
|
||||
private IInstance Instance { get; set; }
|
||||
|
||||
private bool IsConfigured { get; set; }
|
||||
|
||||
[ObservableProperty]
|
||||
PlcData plcData = new PlcData();
|
||||
|
||||
public PLCViewModel()
|
||||
{
|
||||
|
||||
|
@ -60,69 +23,69 @@ namespace LibS7Adv
|
|||
public void ucLoaded()
|
||||
{
|
||||
IsConnected = false;
|
||||
PlcData.LastError = "";
|
||||
PlcData.ConnectionStatus = "offline";
|
||||
lastError = "";
|
||||
connectionStatus = "offline";
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
string iP = "10.1.30.11";
|
||||
|
||||
[ObservableProperty]
|
||||
string name = "PLC Name";
|
||||
|
||||
[ObservableProperty]
|
||||
string cpuTime;
|
||||
|
||||
[ObservableProperty]
|
||||
string connectionStatus;
|
||||
|
||||
[ObservableProperty]
|
||||
string lastError;
|
||||
|
||||
[ObservableProperty]
|
||||
[property: JsonIgnore]
|
||||
bool isConnected;
|
||||
|
||||
[RelayCommand]
|
||||
[property: JsonIgnore]
|
||||
public void ResetAlarmButton()
|
||||
{
|
||||
PlcData.LastError = "";
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
[property: JsonIgnore]
|
||||
public void ConnectButton()
|
||||
{
|
||||
if (isConnected)
|
||||
Disconnect();
|
||||
else
|
||||
Connect();
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (IsConnected)
|
||||
return;
|
||||
try
|
||||
{
|
||||
// Implementa la conexión utilizando PLCModel
|
||||
Instance = SimulationRuntimeManager.CreateInterface(PlcData.Name);
|
||||
Instance = SimulationRuntimeManager.CreateInterface(Name);
|
||||
Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
|
||||
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
|
||||
|
||||
if (Instance != null)
|
||||
{
|
||||
UpdateTagList();
|
||||
PlcData.ConnectionStatus = Instance.OperatingState.ToString();
|
||||
ConnectionStatus = Instance.OperatingState.ToString();
|
||||
IsConnected = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = ex.Message;
|
||||
PlcData.ConnectionStatus = "offline";
|
||||
LastError = ex.Message;
|
||||
ConnectionStatus = "offline";
|
||||
}
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
{
|
||||
IsConnected = false;
|
||||
PlcData.ConnectionStatus = "offline";
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
|
||||
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
|
||||
{
|
||||
UpdateTagList();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
[property: JsonIgnore]
|
||||
public void Disconnect()
|
||||
{
|
||||
IsConnected = false;
|
||||
ConnectionStatus = "offline";
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
|
||||
public void UpdateTagList()
|
||||
{
|
||||
IsConfigured = false;
|
||||
|
@ -133,7 +96,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = ex.Message;
|
||||
LastError = ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,7 +106,7 @@ namespace LibS7Adv
|
|||
{
|
||||
if (!isConnected)
|
||||
{
|
||||
PlcData.LastError = "Not Connected";
|
||||
LastError = "Not Connected";
|
||||
return false;
|
||||
}
|
||||
if (sTag == null)
|
||||
|
@ -165,7 +128,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = sTag + ":" + ex.Message;
|
||||
LastError = sTag + ":" + ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -176,7 +139,7 @@ namespace LibS7Adv
|
|||
{
|
||||
if (!isConnected)
|
||||
{
|
||||
PlcData.LastError = "Not Connected";
|
||||
LastError = "Not Connected";
|
||||
return false;
|
||||
}
|
||||
if (sTag == null)
|
||||
|
@ -189,7 +152,7 @@ namespace LibS7Adv
|
|||
else if (tag.tagType == EDataType.Bool)
|
||||
{
|
||||
if (tag.areaType == EArea.Input)
|
||||
Instance?.InputArea.WriteBit(tag.word_offset, tag.bit, Value);
|
||||
Instance?.OutputArea.WriteBit(tag.word_offset, tag.bit, Value);
|
||||
if (tag.areaType == EArea.Output)
|
||||
Instance?.OutputArea.WriteBit(tag.word_offset, tag.bit, Value);
|
||||
if (tag.areaType == EArea.Marker)
|
||||
|
@ -199,19 +162,19 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = sTag + ":" + ex.Message;
|
||||
LastError = sTag + ":" + ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string? LeerNumber(string sTag, bool Signed = false)
|
||||
public string? LeerNumber(string sTag)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!isConnected)
|
||||
{
|
||||
PlcData.LastError = "Not Connected";
|
||||
LastError = "Not Connected";
|
||||
return "";
|
||||
}
|
||||
if (sTag == null)
|
||||
|
@ -227,51 +190,26 @@ namespace LibS7Adv
|
|||
{
|
||||
// Read not Work for ImputArea
|
||||
if (tag.areaType == EArea.Output)
|
||||
{
|
||||
var val = Instance?.OutputArea.ReadByte(tag.word_offset);
|
||||
if (Signed)
|
||||
return val > 127 ? (val - 256).ToString() : val.ToString();
|
||||
return val.ToString();
|
||||
}
|
||||
return Instance?.OutputArea.ReadByte(tag.word_offset).ToString();
|
||||
if (tag.areaType == EArea.Marker)
|
||||
{
|
||||
var val = Instance?.MarkerArea.ReadByte(tag.word_offset);
|
||||
if (Signed)
|
||||
return val > 127 ? (val-256).ToString() : val.ToString();
|
||||
return val.ToString();
|
||||
}
|
||||
return Instance?.MarkerArea.ReadByte(tag.word_offset).ToString();
|
||||
}
|
||||
else if (tag.tagType == EDataType.Word)
|
||||
{
|
||||
// Read not Work for ImputArea
|
||||
if (tag.areaType == EArea.Output)
|
||||
{
|
||||
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();
|
||||
}
|
||||
return Instance?.OutputArea.ReadBytes(tag.word_offset,2).ToString();
|
||||
if (tag.areaType == EArea.Marker)
|
||||
{
|
||||
int value;
|
||||
byte[] bytes = Instance?.MarkerArea.ReadBytes(tag.word_offset, 2);
|
||||
Array.Reverse(bytes);
|
||||
if (Signed)
|
||||
value = BitConverter.ToInt16(bytes, 0);
|
||||
else
|
||||
value = BitConverter.ToUInt16(bytes, 0);
|
||||
return value.ToString();
|
||||
return (bytes[0] + bytes[1]*256).ToString();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = sTag + ":" + ex.Message;
|
||||
LastError = sTag + ":" + ex.Message;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
@ -286,7 +224,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = ex.Message;
|
||||
LastError = ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -299,7 +237,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = ex.Message;
|
||||
LastError = ex.Message;
|
||||
}
|
||||
}
|
||||
public void EscribirTag(string pTag, SDataValue Value)
|
||||
|
@ -310,7 +248,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = pTag + ":" + ex.Message;
|
||||
LastError = pTag + ":" + ex.Message;
|
||||
}
|
||||
}
|
||||
public SDataValue LeerTag(string pTag)
|
||||
|
@ -321,7 +259,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = pTag + ":" + ex.Message;
|
||||
LastError = pTag + ":" + ex.Message;
|
||||
return new SDataValue();
|
||||
}
|
||||
}
|
||||
|
@ -333,7 +271,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = pTag + ":" + ex.Message;
|
||||
LastError = pTag + ":" + ex.Message;
|
||||
}
|
||||
}
|
||||
public void EscribirTagInt16(string pTag, int pValue)
|
||||
|
@ -344,7 +282,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = pTag + ":" + ex.Message;
|
||||
LastError = pTag + ":" + ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -356,7 +294,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = pTag + ":" + ex.Message;
|
||||
LastError = pTag + ":" + ex.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -369,7 +307,7 @@ namespace LibS7Adv
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PlcData.LastError = pTag + ":" + ex.Message;
|
||||
LastError = pTag + ":" + ex.Message;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue