497 lines
16 KiB
C#
497 lines
16 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
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;
|
|
|
|
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()
|
|
{
|
|
|
|
}
|
|
|
|
public void ucLoaded()
|
|
{
|
|
IsConnected = false;
|
|
PlcData.LastError = "";
|
|
PlcData.ConnectionStatus = "offline";
|
|
}
|
|
|
|
[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.OnSoftwareConfigurationChanged += Instance_OnSoftwareConfigurationChanged;
|
|
//_plcModel.Instance.CommunicationInterface = ECommunicationInterface.Softbus;
|
|
|
|
if (Instance != null)
|
|
{
|
|
UpdateTagList();
|
|
PlcData.ConnectionStatus = Instance.OperatingState.ToString();
|
|
IsConnected = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
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();
|
|
}
|
|
|
|
public void UpdateTagList()
|
|
{
|
|
IsConfigured = false;
|
|
try
|
|
{
|
|
Instance?.UpdateTagList(ETagListDetails.IO | ETagListDetails.DB | ETagListDetails.M, true); // ETagListDetails.IO | ETagListDetails.DB
|
|
IsConfigured = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = ex.Message;
|
|
}
|
|
}
|
|
|
|
public bool? LeerBool(string sTag)
|
|
{
|
|
try
|
|
{
|
|
if (!isConnected)
|
|
{
|
|
PlcData.LastError = "Not Connected";
|
|
return false;
|
|
}
|
|
if (sTag == null)
|
|
{ return false; }
|
|
var tag = ParseTagAddress(sTag);
|
|
if (tag.tagType == EDataType.Unknown) // Normal Tag?
|
|
{
|
|
return Instance?.ReadBool(sTag);
|
|
}
|
|
else if (tag.tagType == EDataType.Bool)
|
|
{
|
|
// Read not Work for ImputArea
|
|
if (tag.areaType == EArea.Output)
|
|
return Instance?.OutputArea.ReadBit(tag.word_offset, tag.bit);
|
|
if (tag.areaType == EArea.Marker)
|
|
return Instance?.MarkerArea.ReadBit(tag.word_offset, tag.bit);
|
|
}
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = sTag + ":" + ex.Message;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool? EscribirBool(string sTag, bool Value)
|
|
{
|
|
try
|
|
{
|
|
if (!isConnected)
|
|
{
|
|
PlcData.LastError = "Not Connected";
|
|
return false;
|
|
}
|
|
if (sTag == null)
|
|
{ return false; }
|
|
var tag = ParseTagAddress(sTag);
|
|
if (tag.tagType == EDataType.Unknown) // Normal Tag?
|
|
{
|
|
Instance?.WriteBool(sTag, Value);
|
|
}
|
|
else if (tag.tagType == EDataType.Bool)
|
|
{
|
|
if (tag.areaType == EArea.Input)
|
|
Instance?.InputArea.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)
|
|
Instance?.MarkerArea.WriteBit(tag.word_offset, tag.bit, Value);
|
|
}
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = sTag + ":" + ex.Message;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public string? LeerNumber(string sTag, bool Signed = false)
|
|
{
|
|
try
|
|
{
|
|
if (!isConnected)
|
|
{
|
|
PlcData.LastError = "Not Connected";
|
|
return "";
|
|
}
|
|
if (sTag == null)
|
|
{ return ""; }
|
|
|
|
var tag = ParseTagAddress(sTag.Trim());
|
|
if (tag.tagType == EDataType.Unknown) // Normal Tag?
|
|
{
|
|
var data = Instance?.Read(sTag);
|
|
return data.Value.ToString();
|
|
}
|
|
else if (tag.tagType == EDataType.Byte)
|
|
{
|
|
// 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();
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
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();
|
|
}
|
|
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 "";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = sTag + ":" + ex.Message;
|
|
return "";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public bool LeerSalidaBool(byte pByte, int pBit)
|
|
{
|
|
try
|
|
{
|
|
return Instance?.OutputArea.ReadBit(pByte, (byte)pBit) ?? false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.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)
|
|
{
|
|
PlcData.LastError = ex.Message;
|
|
}
|
|
}
|
|
public void EscribirTag(string pTag, SDataValue Value)
|
|
{
|
|
try
|
|
{
|
|
Instance?.Write(pTag, Value);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = pTag + ":" + ex.Message;
|
|
}
|
|
}
|
|
public SDataValue LeerTag(string pTag)
|
|
{
|
|
try
|
|
{
|
|
return Instance.Read(pTag);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = pTag + ":" + ex.Message;
|
|
return new SDataValue();
|
|
}
|
|
}
|
|
public void EscribirTagBool(string pTag, bool pValue)
|
|
{
|
|
try
|
|
{
|
|
Instance?.WriteBool(pTag, pValue);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = pTag + ":" + ex.Message;
|
|
}
|
|
}
|
|
public void EscribirTagInt16(string pTag, int pValue)
|
|
{
|
|
try
|
|
{
|
|
Instance?.WriteInt16(pTag, (short)pValue);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = pTag + ":" + ex.Message;
|
|
}
|
|
}
|
|
|
|
public bool LeerTagBool(string pTag)
|
|
{
|
|
try
|
|
{
|
|
return Instance?.ReadBool(pTag) ?? false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = pTag + ":" + ex.Message;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public int? LeerTagInt16(string pTag)
|
|
{
|
|
try
|
|
{
|
|
return Instance?.ReadInt16(pTag);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
PlcData.LastError = pTag + ":" + ex.Message;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
public static TagAddress ParseTagAddress(string tag)
|
|
{
|
|
TagAddress tagAddress = new TagAddress();
|
|
if (tag==null)
|
|
return null;
|
|
if (tag.StartsWith("%"))
|
|
{
|
|
if (tag.StartsWith("%DB"))
|
|
{
|
|
tagAddress.areaType = EArea.DataBlock;
|
|
var match = Regex.Match(tag, @"^%DB(\d+)\.DB([X|B|W|D])(\d+)\.(\d+)$");
|
|
|
|
if (match.Success)
|
|
{
|
|
tagAddress.DB = int.Parse(match.Groups[1].Value);
|
|
char dataTypeChar = match.Groups[2].Value[0];
|
|
tagAddress.word_offset = uint.Parse(match.Groups[3].Value);
|
|
tagAddress.bit = byte.Parse(match.Groups[4].Value);
|
|
|
|
switch (dataTypeChar)
|
|
{
|
|
case 'X':
|
|
tagAddress.tagType = EDataType.Bool;
|
|
break;
|
|
case 'B':
|
|
tagAddress.tagType = EDataType.Byte;
|
|
break;
|
|
case 'W':
|
|
tagAddress.tagType = EDataType.Word;
|
|
break;
|
|
case 'D':
|
|
tagAddress.tagType = EDataType.DWord;
|
|
break;
|
|
default:
|
|
tagAddress.tagType = EDataType.Unknown;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if (tag.StartsWith("%M"))
|
|
{
|
|
tagAddress.areaType = EArea.Marker;
|
|
ParseNonDBAddress(tag, tagAddress);
|
|
}
|
|
else if (tag.StartsWith("%E"))
|
|
{
|
|
tagAddress.areaType = EArea.Input;
|
|
ParseNonDBAddress(tag, tagAddress);
|
|
}
|
|
else if (tag.StartsWith("%A"))
|
|
{
|
|
tagAddress.areaType = EArea.Output;
|
|
ParseNonDBAddress(tag, tagAddress);
|
|
}
|
|
else
|
|
{
|
|
tagAddress.areaType = EArea.InvalidArea;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// If it's not a direct address, treat it as a tag name
|
|
tagAddress.areaType = EArea.InvalidArea;
|
|
tagAddress.tagType = EDataType.Unknown;
|
|
}
|
|
|
|
return tagAddress;
|
|
}
|
|
|
|
private static void ParseNonDBAddress(string tag, TagAddress tagAddress)
|
|
{
|
|
var match = Regex.Match(tag, @"^%[EMA](\d+)\.(\d+)$");
|
|
if (match.Success)
|
|
{
|
|
tagAddress.word_offset = uint.Parse(match.Groups[1].Value);
|
|
tagAddress.bit = byte.Parse(match.Groups[2].Value);
|
|
tagAddress.tagType = EDataType.Bool;
|
|
}
|
|
else
|
|
{
|
|
match = Regex.Match(tag, @"^%[EMA]([XBWDF])(\d+)$");
|
|
if (match.Success)
|
|
{
|
|
char dataTypeChar = match.Groups[1].Value[0];
|
|
tagAddress.word_offset = uint.Parse(match.Groups[2].Value);
|
|
|
|
switch (dataTypeChar)
|
|
{
|
|
case 'X':
|
|
tagAddress.tagType = EDataType.Bool;
|
|
break;
|
|
case 'B':
|
|
tagAddress.tagType = EDataType.Byte;
|
|
break;
|
|
case 'W':
|
|
tagAddress.tagType = EDataType.Word;
|
|
break;
|
|
case 'D':
|
|
tagAddress.tagType = EDataType.DWord;
|
|
break;
|
|
default:
|
|
tagAddress.tagType = EDataType.Unknown;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class TagAddress
|
|
{
|
|
public EArea areaType;
|
|
public EDataType tagType;
|
|
public int DB;
|
|
public uint word_offset;
|
|
public byte bit;
|
|
}
|
|
|
|
}
|