4 Commits

Author SHA1 Message Date
Felix Weiß
cdae9a60fb Merge branch 'master' of https://github.com/WOmed/MewtocolNet 2022-07-13 18:28:40 +02:00
Felix Weiß
b1c2cdb70e Fixed exception on duplicate memory adresses when the adress area was different
- counted up version number
2022-07-13 18:28:28 +02:00
Felix Weiß
95bfcf94de Update README.md 2022-07-04 16:57:42 +02:00
Felix Weiß
0a93df287d Fixed more exception handling and added the ability to close a running interface 2022-07-04 16:53:55 +02:00
5 changed files with 73 additions and 47 deletions

View File

@@ -10,6 +10,9 @@ namespace Examples {
[Register(1000, RegisterType.R)]
public bool TestBool1 { get; private set; }
[Register(1000)]
public int TestDuplicate { get; private set; }
//corresponds to a XD input of the PLC
[Register(RegisterType.X, SpecialAddress.D)]
public bool TestBoolInputXD { get; private set; }

View File

@@ -89,9 +89,7 @@ namespace MewtocolNet {
}
foreach (var registerPair in Registers) {
var reg = registerPair.Value;
foreach (var reg in Registers) {
if (reg is NRegister<short> shortReg) {
var lastVal = shortReg.Value;
@@ -210,7 +208,7 @@ namespace MewtocolNet {
toAdd = new BRegister(_address, _type, _name);
}
Registers.Add(_address, toAdd);
Registers.Add(toAdd);
}
@@ -240,7 +238,7 @@ namespace MewtocolNet {
}
toAdd.collectionType = _colType;
Registers.Add(_address, toAdd);
Registers.Add(toAdd);
}
@@ -261,7 +259,7 @@ namespace MewtocolNet {
public void AddRegister (SpecialAddress _spAddress, RegisterType _type, string _name = null) {
//as bool registers
Registers.Add((int)_spAddress, new BRegister(_spAddress, _type, _name));
Registers.Add(new BRegister(_spAddress, _type, _name));
}
@@ -272,7 +270,7 @@ namespace MewtocolNet {
reg.collectionType = _colType;
//as bool registers
Registers.Add((int)_spAddress, reg);
Registers.Add(reg);
}
@@ -301,32 +299,35 @@ namespace MewtocolNet {
throw new NotSupportedException($"_lenght parameter only allowed for register of type string");
}
if (Registers.Any(x => x.Key == _address)) {
throw new NotSupportedException($"Cannot add a register multiple times, " +
$"make sure that all register attributes or AddRegister assignments have different adresses.");
}
Register toAdd;
if (regType == typeof(short)) {
Registers.Add(_address, new NRegister<short>(_address, _name));
toAdd = new NRegister<short>(_address, _name);
} else if (regType == typeof(ushort)) {
Registers.Add(_address, new NRegister<ushort>(_address, _name));
toAdd = new NRegister<ushort>(_address, _name);
} else if (regType == typeof(int)) {
Registers.Add(_address, new NRegister<int>(_address, _name));
toAdd = new NRegister<int>(_address, _name);
} else if (regType == typeof(uint)) {
Registers.Add(_address, new NRegister<uint>(_address, _name));
toAdd = new NRegister<uint>(_address, _name);
} else if (regType == typeof(float)) {
Registers.Add(_address, new NRegister<float>(_address, _name));
toAdd = new NRegister<float>(_address, _name);
} else if (regType == typeof(string)) {
Registers.Add(_address, new SRegister(_address, _length, _name));
toAdd = new SRegister(_address, _length, _name);
} else if (regType == typeof(TimeSpan)) {
Registers.Add(_address, new NRegister<TimeSpan>(_address, _name));
toAdd = new NRegister<TimeSpan>(_address, _name);
} else if (regType == typeof(bool)) {
Registers.Add(_address, new BRegister(_address, RegisterType.R, _name));
toAdd = new BRegister(_address, RegisterType.R, _name);
} else {
throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" +
$"Allowed are: short, ushort, int, uint, float and string");
}
if (Registers.Any(x => x.GetRegisterPLCName() == toAdd.GetRegisterPLCName())) {
throw new NotSupportedException($"Cannot add a register multiple times, " +
$"make sure that all register attributes or AddRegister assignments have different adresses.");
}
}
internal void AddRegister<T> (Type _colType, int _address, int _length = 1, string _name = null, bool _isBitwise = false, Type _enumType = null) {
@@ -337,12 +338,7 @@ namespace MewtocolNet {
throw new NotSupportedException($"_lenght parameter only allowed for register of type string");
}
if (Registers.Any(x => x.Key == _address) && !_isBitwise) {
throw new NotSupportedException($"Cannot add a register multiple times, " +
$"make sure that all register attributes or AddRegister assignments have different adresses.");
}
if (Registers.Any(x => x.Key == _address) && _isBitwise) {
if (Registers.Any(x => x.MemoryAdress == _address) && _isBitwise) {
return;
}
@@ -366,15 +362,19 @@ namespace MewtocolNet {
reg = new BRegister(_address, RegisterType.R, _name);
}
if (reg == null) {
throw new NotSupportedException($"The type {regType} is not allowed for Registers \n" +
$"Allowed are: short, ushort, int, uint, float and string");
} else {
reg.collectionType = _colType;
Registers.Add(_address, reg);
if (Registers.Any(x => x.GetRegisterPLCName() == reg.GetRegisterPLCName()) && !_isBitwise) {
throw new NotSupportedException($"Cannot add a register multiple times, " +
$"make sure that all register attributes or AddRegister assignments have different adresses.");
}
reg.collectionType = _colType;
Registers.Add(reg);
}
}
@@ -389,7 +389,7 @@ namespace MewtocolNet {
/// <returns></returns>
public Register GetRegister (string name) {
return Registers.FirstOrDefault(x => x.Value.Name == name).Value;
return Registers.FirstOrDefault(x => x.Name == name);
}
@@ -400,8 +400,8 @@ namespace MewtocolNet {
/// <returns>A casted register or the <code>default</code> value</returns>
public T GetRegister<T> (string name) where T : Register {
try {
var reg = Registers.FirstOrDefault(x => x.Value.Name == name);
return reg.Value as T;
var reg = Registers.FirstOrDefault(x => x.Name == name);
return reg as T;
} catch (InvalidCastException) {
return default(T);
}
@@ -416,7 +416,7 @@ namespace MewtocolNet {
/// </summary>
public List<Register> GetAllRegisters () {
return Registers.Values.ToList();
return Registers;
}

View File

@@ -67,7 +67,7 @@ namespace MewtocolNet {
/// <summary>
/// The registered data registers of the PLC
/// </summary>
public Dictionary<int, Register> Registers { get; set; } = new Dictionary<int, Register>();
public List<Register> Registers { get; set; } = new List<Register>();
private string ip;
private int port;
@@ -195,6 +195,20 @@ namespace MewtocolNet {
}
/// <summary>
/// Closes all permanent polling
/// </summary>
public void Disconnect () {
if (!IsConnected)
return;
OnMajorSocketException();
PriorityTasks.Clear();
}
/// <summary>
/// Attaches a poller to the interface that continously
/// polls the registered data registers and writes the values to them
@@ -532,7 +546,6 @@ namespace MewtocolNet {
/// Calculates checksum and sends a command to the PLC then awaits results
/// </summary>
/// <param name="_msg">MEWTOCOL Formatted request string ex: %01#RT</param>
/// <param name="_close">Auto close of frame [true]%01#RT01\r [false]%01#RT</param>
/// <returns>Returns the result</returns>
public async Task<CommandResult> SendCommandAsync (string _msg) {
@@ -622,6 +635,9 @@ namespace MewtocolNet {
} catch (IOException) {
Logger.Log($"Critical IO exception on send", LogLevel.Critical, this);
return null;
} catch (SocketException) {
OnMajorSocketException();
return null;
}
//await result
@@ -636,6 +652,9 @@ namespace MewtocolNet {
} catch (IOException) {
Logger.Log($"Critical IO exception on receive", LogLevel.Critical, this);
return null;
} catch (SocketException) {
OnMajorSocketException();
return null;
}
sw.Stop();
@@ -650,16 +669,7 @@ namespace MewtocolNet {
} catch (SocketException) {
if (IsConnected) {
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
CycleTimeMs = 0;
IsConnected = false;
Disconnected?.Invoke();
KillPoller();
}
OnMajorSocketException();
return null;
}
@@ -668,6 +678,19 @@ namespace MewtocolNet {
}
private void OnMajorSocketException () {
if (IsConnected) {
Logger.Log("The PLC connection was closed", LogLevel.Error, this);
CycleTimeMs = 0;
IsConnected = false;
Disconnected?.Invoke();
KillPoller();
}
}
#endregion

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>MewtocolNet</PackageId>
<Version>0.4.1</Version>
<Version>0.4.3</Version>
<Authors>Felix Weiss</Authors>
<Company>Womed</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

View File

@@ -53,7 +53,7 @@ Where is the RS232/Serial support?
Install this package by using [Nuget](https://www.nuget.org/packages/MewtocolNet/) or reference
```XML
<PackageReference Include="MewtocolNet" Version="0.3.0" />
<PackageReference Include="MewtocolNet" Version="0.4.2" />
```
in your dependencies.
Alternatively use the dotnet CLI and run