mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 11:11:23 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c7c368b55 | ||
|
|
38f0f9f523 | ||
|
|
c2aecb387a | ||
|
|
0e1f5cd12b |
@@ -97,14 +97,14 @@ class Program {
|
||||
|
||||
Task.Factory.StartNew(async () => {
|
||||
|
||||
using(var interf = new MewtocolInterface("10.237.191.3")) {
|
||||
//automatic endpoint
|
||||
using (var interf = new MewtocolInterface("10.237.191.3")) {
|
||||
|
||||
await interf.ConnectAsync();
|
||||
|
||||
if(interf.IsConnected) {
|
||||
if (interf.IsConnected) {
|
||||
|
||||
var plcInf = await interf.GetPLCInfoAsync();
|
||||
Console.WriteLine(plcInf);
|
||||
await Task.Delay(5000);
|
||||
|
||||
}
|
||||
|
||||
@@ -112,15 +112,16 @@ class Program {
|
||||
|
||||
}
|
||||
|
||||
|
||||
//manual endpoint
|
||||
using (var interf = new MewtocolInterface("10.237.191.3")) {
|
||||
|
||||
interf.HostEndpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("10.237.191.77"), 0);
|
||||
|
||||
await interf.ConnectAsync();
|
||||
|
||||
if (interf.IsConnected) {
|
||||
if(interf.IsConnected) {
|
||||
|
||||
var plcInf = await interf.GetPLCInfoAsync();
|
||||
Console.WriteLine(plcInf);
|
||||
await Task.Delay(5000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace MewtocolNet.Registers {
|
||||
|
||||
/// <summary>
|
||||
/// Contains information about the plc and its cpu
|
||||
/// </summary>
|
||||
public partial class CpuInfo {
|
||||
|
||||
/// <summary>
|
||||
/// The cpu type of the plc
|
||||
/// </summary>
|
||||
public CpuType Cputype { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Program capacity in 1K steps
|
||||
/// </summary>
|
||||
public int ProgramCapacity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Version of the cpu
|
||||
/// </summary>
|
||||
public string CpuVersion { get; set; }
|
||||
|
||||
|
||||
public static CpuInfo BuildFromHexString (string _cpuType, string _cpuVersion, string _progCapacity) {
|
||||
internal static CpuInfo BuildFromHexString (string _cpuType, string _cpuVersion, string _progCapacity) {
|
||||
|
||||
CpuInfo retInf = new CpuInfo();
|
||||
|
||||
@@ -47,8 +61,7 @@ namespace MewtocolNet.Registers {
|
||||
return retInf;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -43,7 +43,7 @@ namespace MewtocolNet {
|
||||
/// </summary>
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
private int connectTimeout = 1000;
|
||||
private int connectTimeout = 3000;
|
||||
/// <summary>
|
||||
/// The initial connection timeout in milliseconds
|
||||
/// </summary>
|
||||
@@ -52,6 +52,11 @@ namespace MewtocolNet {
|
||||
set { connectTimeout = value; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The host ip endpoint, leave it null to use an automatic interface
|
||||
/// </summary>
|
||||
public IPEndPoint HostEndpoint { get; set; }
|
||||
|
||||
private bool isConnected;
|
||||
/// <summary>
|
||||
/// The current connection state of the interface
|
||||
@@ -271,24 +276,41 @@ namespace MewtocolNet {
|
||||
|
||||
try {
|
||||
|
||||
if(HostEndpoint != null) {
|
||||
|
||||
client = new TcpClient(HostEndpoint) {
|
||||
ReceiveBufferSize = RecBufferSize,
|
||||
NoDelay = false,
|
||||
};
|
||||
var ep = (IPEndPoint)client.Client.LocalEndPoint;
|
||||
Logger.Log($"Connecting [MAN] endpoint: {ep.Address}:{ep.Port}", LogLevel.Verbose, this);
|
||||
|
||||
} else {
|
||||
|
||||
client = new TcpClient() {
|
||||
ReceiveBufferSize = RecBufferSize,
|
||||
NoDelay = false,
|
||||
ExclusiveAddressUse = true,
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
var result = client.BeginConnect(targetIP, port, null, null);
|
||||
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(ConnectTimeout));
|
||||
|
||||
if(!success) {
|
||||
if(!success || !client.Connected) {
|
||||
OnMajorSocketExceptionWhileConnecting();
|
||||
return;
|
||||
}
|
||||
|
||||
if(HostEndpoint == null) {
|
||||
var ep = (IPEndPoint)client.Client.LocalEndPoint;
|
||||
Logger.Log($"Connecting [AUTO] endpoint: {ep.Address.MapToIPv4()}:{ep.Port}", LogLevel.Verbose, this);
|
||||
}
|
||||
|
||||
stream = client.GetStream();
|
||||
stream.ReadTimeout = 1000;
|
||||
|
||||
Console.WriteLine($"Connected {client.Connected}");
|
||||
await Task.CompletedTask;
|
||||
|
||||
} catch (SocketException) {
|
||||
@@ -707,10 +729,11 @@ namespace MewtocolNet {
|
||||
|
||||
if (client == null || !client.Connected ) {
|
||||
await ConnectTCP();
|
||||
if (!client.Connected)
|
||||
return null;
|
||||
}
|
||||
|
||||
if (client == null || !client.Connected)
|
||||
return null;
|
||||
|
||||
var message = _blockString.ToHexASCIIBytes();
|
||||
|
||||
//send request
|
||||
@@ -746,7 +769,7 @@ namespace MewtocolNet {
|
||||
}
|
||||
|
||||
} catch (IOException) {
|
||||
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
|
||||
OnMajorSocketExceptionWhileConnected();
|
||||
return null;
|
||||
} catch (SocketException) {
|
||||
OnMajorSocketExceptionWhileConnected();
|
||||
|
||||
@@ -190,6 +190,7 @@ namespace MewtocolNet {
|
||||
/// Writes to the given bool register on the PLC
|
||||
/// </summary>
|
||||
/// <param name="_toWrite">The register to write to</param>
|
||||
/// <param name="value">The value to write</param>
|
||||
/// <returns>The success state of the write operation</returns>
|
||||
public async Task<bool> WriteBoolRegister (BRegister _toWrite, bool value) {
|
||||
|
||||
@@ -294,7 +295,7 @@ namespace MewtocolNet {
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of number (short, ushort, int, uint, float)</typeparam>
|
||||
/// <param name="_toWrite">The register to write</param>
|
||||
/// <param name="_stationNumber">Station number to access</param>
|
||||
/// <param name="_value">The value to write</param>
|
||||
/// <returns>The success state of the write operation</returns>
|
||||
public async Task<bool> WriteNumRegister<T> (NRegister<T> _toWrite, T _value) {
|
||||
|
||||
|
||||
@@ -6,13 +6,38 @@ namespace MewtocolNet.Registers {
|
||||
/// All modes
|
||||
/// </summary>
|
||||
public class PLCMode {
|
||||
|
||||
/// <summary>
|
||||
/// PLC is running
|
||||
/// </summary>
|
||||
public bool RunMode { get; set; }
|
||||
/// <summary>
|
||||
/// PLC is in test
|
||||
/// </summary>
|
||||
public bool TestRunMode { get; set; }
|
||||
/// <summary>
|
||||
/// BreakExcecuting
|
||||
/// </summary>
|
||||
public bool BreakExcecuting { get; set; }
|
||||
/// <summary>
|
||||
/// BreakValid
|
||||
/// </summary>
|
||||
public bool BreakValid { get; set; }
|
||||
/// <summary>
|
||||
/// PLC output is enabled
|
||||
/// </summary>
|
||||
public bool OutputEnabled { get; set; }
|
||||
/// <summary>
|
||||
/// PLC runs step per step
|
||||
/// </summary>
|
||||
public bool StepRunMode { get; set; }
|
||||
/// <summary>
|
||||
/// Message executing
|
||||
/// </summary>
|
||||
public bool MessageExecuting { get; set; }
|
||||
/// <summary>
|
||||
/// PLC is in remote mode
|
||||
/// </summary>
|
||||
public bool RemoteMode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -6,22 +6,39 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MewtocolNet.RegisterAttributes {
|
||||
|
||||
/// <summary>
|
||||
/// The size of the bitwise register
|
||||
/// </summary>
|
||||
public enum BitCount {
|
||||
/// <summary>
|
||||
/// 16 bit
|
||||
/// </summary>
|
||||
B16,
|
||||
/// <summary>
|
||||
/// 32 bit
|
||||
/// </summary>
|
||||
B32
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defines the behavior of a register property
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class RegisterAttribute : Attribute {
|
||||
|
||||
public int MemoryArea;
|
||||
public int StringLength;
|
||||
public RegisterType RegisterType;
|
||||
public SpecialAddress SpecialAddress = SpecialAddress.None;
|
||||
public BitCount BitCount;
|
||||
public int AssignedBitIndex = -1;
|
||||
internal int MemoryArea;
|
||||
internal int StringLength;
|
||||
internal RegisterType RegisterType;
|
||||
internal SpecialAddress SpecialAddress = SpecialAddress.None;
|
||||
internal BitCount BitCount;
|
||||
internal int AssignedBitIndex = -1;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for string type or numeric registers
|
||||
/// </summary>
|
||||
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||
/// <param name="stringLength">The max string length in the plc</param>
|
||||
public RegisterAttribute (int memoryArea, int stringLength = 1) {
|
||||
|
||||
MemoryArea = memoryArea;
|
||||
@@ -29,6 +46,11 @@ namespace MewtocolNet.RegisterAttributes {
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for boolean registers
|
||||
/// </summary>
|
||||
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||
/// <param name="type">The type of boolean register</param>
|
||||
public RegisterAttribute (int memoryArea, RegisterType type) {
|
||||
|
||||
if (type.ToString().StartsWith("DT"))
|
||||
@@ -40,6 +62,11 @@ namespace MewtocolNet.RegisterAttributes {
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for boolean registers
|
||||
/// </summary>
|
||||
/// <param name="spAdress">The special area in the plcs memory</param>
|
||||
/// <param name="type">The type of boolean register</param>
|
||||
public RegisterAttribute (RegisterType type, SpecialAddress spAdress) {
|
||||
|
||||
if (type.ToString().StartsWith("DT"))
|
||||
@@ -50,7 +77,11 @@ namespace MewtocolNet.RegisterAttributes {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Attribute to read numeric registers as bitwise
|
||||
/// </summary>
|
||||
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||
/// <param name="bitcount">The number of bits to parse</param>
|
||||
public RegisterAttribute (int memoryArea, BitCount bitcount) {
|
||||
|
||||
MemoryArea = memoryArea;
|
||||
@@ -59,6 +90,12 @@ namespace MewtocolNet.RegisterAttributes {
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attribute to read numeric registers as bitwise
|
||||
/// </summary>
|
||||
/// <param name="memoryArea">The area in the plcs memory</param>
|
||||
/// <param name="bitcount">The number of bits to parse</param>
|
||||
/// <param name="assignBit">The index of the bit that gets linked to the bool</param>
|
||||
public RegisterAttribute (int memoryArea, uint assignBit, BitCount bitcount) {
|
||||
|
||||
if(assignBit > 15 && bitcount == BitCount.B16) {
|
||||
|
||||
@@ -34,8 +34,17 @@ namespace MewtocolNet.RegisterAttributes {
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets called when the register collection base was linked to its parent mewtocol interface
|
||||
/// </summary>
|
||||
/// <param name="plc">The parent interface</param>
|
||||
public virtual void OnInterfaceLinked (MewtocolInterface plc) { }
|
||||
|
||||
/// <summary>
|
||||
/// Gets called when the register collection base was linked to its parent mewtocol interface
|
||||
/// and the plc connection is established
|
||||
/// </summary>
|
||||
/// <param name="plc">The parent interface</param>
|
||||
public virtual void OnInterfaceLinkedAndOnline (MewtocolInterface plc) { }
|
||||
|
||||
}
|
||||
|
||||
@@ -12,8 +12,7 @@ namespace MewtocolNet.Registers {
|
||||
internal RegisterType RegType { get; private set; }
|
||||
internal SpecialAddress SpecialAddress { get; private set; }
|
||||
|
||||
public bool NeedValue;
|
||||
public bool LastValue;
|
||||
internal bool LastValue;
|
||||
|
||||
/// <summary>
|
||||
/// The value of the register
|
||||
@@ -54,6 +53,9 @@ namespace MewtocolNet.Registers {
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the register area name
|
||||
/// </summary>
|
||||
public override string BuildMewtocolIdent () {
|
||||
|
||||
//build area code from register type
|
||||
@@ -73,9 +75,7 @@ namespace MewtocolNet.Registers {
|
||||
TriggerChangedEvnt(this);
|
||||
TriggerNotifyChange();
|
||||
}
|
||||
public override string ToString() {
|
||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
namespace MewtocolNet.Registers {
|
||||
|
||||
/// <summary>
|
||||
/// Result for a boolean register
|
||||
/// </summary>
|
||||
public class BRegisterResult {
|
||||
|
||||
/// <summary>
|
||||
/// The command result
|
||||
/// </summary>
|
||||
public CommandResult Result { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The used register
|
||||
/// </summary>
|
||||
public BRegister Register { get; set; }
|
||||
|
||||
public override string ToString() {
|
||||
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
||||
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,7 @@ namespace MewtocolNet.Registers {
|
||||
/// <typeparam name="T">The type of the numeric value</typeparam>
|
||||
public class NRegister<T> : Register {
|
||||
|
||||
public T NeedValue;
|
||||
public T LastValue;
|
||||
internal T LastValue;
|
||||
|
||||
/// <summary>
|
||||
/// The value of the register
|
||||
@@ -78,10 +77,6 @@ namespace MewtocolNet.Registers {
|
||||
TriggerNotifyChange();
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,13 +6,16 @@ namespace MewtocolNet.Registers {
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the numeric value</typeparam>
|
||||
public class NRegisterResult<T> {
|
||||
public CommandResult Result { get; set; }
|
||||
public NRegister<T> Register { get; set; }
|
||||
|
||||
public override string ToString() {
|
||||
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
||||
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
||||
}
|
||||
/// <summary>
|
||||
/// Command result
|
||||
/// </summary>
|
||||
public CommandResult Result { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The used register
|
||||
/// </summary>
|
||||
public NRegister<T> Register { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Trys to get the value of there is one
|
||||
|
||||
@@ -75,6 +75,9 @@ namespace MewtocolNet.Registers {
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the register area name
|
||||
/// </summary>
|
||||
public virtual string BuildMewtocolIdent() {
|
||||
|
||||
StringBuilder asciistring = new StringBuilder("D");
|
||||
@@ -178,6 +181,9 @@ namespace MewtocolNet.Registers {
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the register dataarea string DT for 16bit and DDT for 32 bit types
|
||||
/// </summary>
|
||||
public string GetRegisterString () {
|
||||
|
||||
if (this is NRegister<short> shortReg) {
|
||||
|
||||
@@ -9,9 +9,12 @@ namespace MewtocolNet.Registers {
|
||||
|
||||
private string lastVal = "";
|
||||
|
||||
/// <summary>
|
||||
/// The current value of the register
|
||||
/// </summary>
|
||||
public string Value => lastVal;
|
||||
|
||||
public short ReservedSize { get; set; }
|
||||
internal short ReservedSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defines a register containing a string
|
||||
@@ -32,10 +35,9 @@ namespace MewtocolNet.Registers {
|
||||
memoryLength = (int)Math.Round(wordsize + 1);
|
||||
}
|
||||
|
||||
public override string ToString() {
|
||||
return $"Adress: {MemoryAdress} Val: {Value}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the register identifier for the mewotocol protocol
|
||||
/// </summary>
|
||||
public override string BuildMewtocolIdent() {
|
||||
|
||||
StringBuilder asciistring = new StringBuilder("D");
|
||||
@@ -65,7 +67,6 @@ namespace MewtocolNet.Registers {
|
||||
TriggerNotifyChange();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
namespace MewtocolNet.Registers {
|
||||
|
||||
/// <summary>
|
||||
/// The results of a string register operation
|
||||
/// </summary>
|
||||
public class SRegisterResult {
|
||||
|
||||
/// <summary>
|
||||
/// The command result
|
||||
/// </summary>
|
||||
public CommandResult Result { get; set; }
|
||||
/// <summary>
|
||||
/// The register definition used
|
||||
/// </summary>
|
||||
public SRegister Register { get; set; }
|
||||
|
||||
public override string ToString() {
|
||||
string errmsg = Result.Success ? "" : $", Error [{Result.ErrorDescription}]";
|
||||
return $"Result [{Result.Success}], Register [{Register.ToString()}]{errmsg}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageId>MewtocolNet</PackageId>
|
||||
<Version>0.5.0</Version>
|
||||
<Version>0.5.1</Version>
|
||||
<Authors>Felix Weiss</Authors>
|
||||
<Company>Womed</Company>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
|
||||
Reference in New Issue
Block a user