diff --git a/Icons/connect.png b/Icons/connect.png new file mode 100644 index 0000000..4a0bf70 Binary files /dev/null and b/Icons/connect.png differ diff --git a/LibS7Adv.csproj b/LibS7Adv.csproj index 6ece5ae..2bbcb22 100644 --- a/LibS7Adv.csproj +++ b/LibS7Adv.csproj @@ -7,8 +7,13 @@ enable + + + + + @@ -18,4 +23,8 @@ + + + + diff --git a/PLCControl.xaml b/PLCControl.xaml index ea1ea6d..c5dad9f 100644 --- a/PLCControl.xaml +++ b/PLCControl.xaml @@ -1,44 +1,43 @@  - + 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" > + + + + + + + + - - - - - - - - - - - - - + diff --git a/PLCViewModel.cs b/PLCViewModel.cs index 4368289..101e5bf 100644 --- a/PLCViewModel.cs +++ b/PLCViewModel.cs @@ -3,18 +3,50 @@ using CommunityToolkit.Mvvm.Input; using Siemens.Simatic.Simulation.Runtime; using Newtonsoft.Json; using System.Text.RegularExpressions; -using System.Text.RegularExpressions; -using System.Threading.Tasks; -using Siemens.Simatic.Simulation.Runtime; +using System.ComponentModel; +using Newtonsoft.Json.Linq; 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 { [JsonIgnore] private IInstance Instance { get; set; } + private bool IsConfigured { get; set; } + [ObservableProperty] + PlcData plcData = new PlcData(); + public PLCViewModel() { @@ -23,69 +55,60 @@ namespace LibS7Adv public void ucLoaded() { IsConnected = false; - lastError = ""; - connectionStatus = "offline"; + PlcData.LastError = ""; + 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] [property: JsonIgnore] bool isConnected; [RelayCommand] [property: JsonIgnore] + public void ConnectButton() + { + if (isConnected) + Disconnect(); + else + Connect(); + } + public void Connect() { try { // Implementa la conexión utilizando PLCModel - Instance = SimulationRuntimeManager.CreateInterface(Name); + Instance = SimulationRuntimeManager.CreateInterface(PlcData.Name); Instance.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged; //_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus; if (Instance != null) { UpdateTagList(); - ConnectionStatus = Instance.OperatingState.ToString(); + PlcData.ConnectionStatus = Instance.OperatingState.ToString(); IsConnected = true; } } catch (Exception ex) { - LastError = ex.Message; - ConnectionStatus = "offline"; + PlcData.LastError = ex.Message; + PlcData.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; @@ -96,7 +119,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = ex.Message; + PlcData.LastError = ex.Message; } } @@ -106,7 +129,7 @@ namespace LibS7Adv { if (!isConnected) { - LastError = "Not Connected"; + PlcData.LastError = "Not Connected"; return false; } if (sTag == null) @@ -128,7 +151,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = sTag + ":" + ex.Message; + PlcData.LastError = sTag + ":" + ex.Message; return false; } } @@ -139,7 +162,7 @@ namespace LibS7Adv { if (!isConnected) { - LastError = "Not Connected"; + PlcData.LastError = "Not Connected"; return false; } if (sTag == null) @@ -162,19 +185,19 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = sTag + ":" + ex.Message; + PlcData.LastError = sTag + ":" + ex.Message; return false; } } - public string? LeerNumber(string sTag) + public string? LeerNumber(string sTag, bool Signed = false) { try { if (!isConnected) { - LastError = "Not Connected"; + PlcData.LastError = "Not Connected"; return ""; } if (sTag == null) @@ -190,26 +213,51 @@ namespace LibS7Adv { // Read not Work for ImputArea 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) - 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) { // Read not Work for ImputArea 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) { + int value; 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 ""; } catch (Exception ex) { - LastError = sTag + ":" + ex.Message; + PlcData.LastError = sTag + ":" + ex.Message; return ""; } } @@ -224,7 +272,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = ex.Message; + PlcData.LastError = ex.Message; return false; } } @@ -237,7 +285,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = ex.Message; + PlcData.LastError = ex.Message; } } public void EscribirTag(string pTag, SDataValue Value) @@ -248,7 +296,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = pTag + ":" + ex.Message; + PlcData.LastError = pTag + ":" + ex.Message; } } public SDataValue LeerTag(string pTag) @@ -259,7 +307,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = pTag + ":" + ex.Message; + PlcData.LastError = pTag + ":" + ex.Message; return new SDataValue(); } } @@ -271,7 +319,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = pTag + ":" + ex.Message; + PlcData.LastError = pTag + ":" + ex.Message; } } public void EscribirTagInt16(string pTag, int pValue) @@ -282,7 +330,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = pTag + ":" + ex.Message; + PlcData.LastError = pTag + ":" + ex.Message; } } @@ -294,7 +342,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = pTag + ":" + ex.Message; + PlcData.LastError = pTag + ":" + ex.Message; return false; } } @@ -307,7 +355,7 @@ namespace LibS7Adv } catch (Exception ex) { - LastError = pTag + ":" + ex.Message; + PlcData.LastError = pTag + ":" + ex.Message; return 0; } }