Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
- fixes #11
- fixes #12
- fixes #13
- fixes #14
  • Loading branch information
Sandoun committed Nov 13, 2023
1 parent 6dd2a16 commit 19db0a9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 31 deletions.
1 change: 1 addition & 0 deletions Examples/Examples.BasicEthernet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static async Task AsyncMain () {

//the library provides a logging tool, comment this out if needed
Logger.LogLevel = LogLevel.Critical;
Logger.OnNewLogMessage((t, l, m) => { Console.WriteLine(m); });

//create a new interface to the plc using ethernet / tcp ip
//the using keyword is optional, if you want to use your PLC instance
Expand Down
8 changes: 7 additions & 1 deletion MewtocolNet/IPlcSerial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public interface IPlcSerial : IPlc {
/// </summary>
StopBits SerialStopBits { get; }

/// <summary>
/// Is RTS (Request to send) enabled?
/// </summary>
bool RtsEnabled { get; }

/// <summary>
/// Sets up the connection settings for the device
/// </summary>
Expand All @@ -42,8 +47,9 @@ public interface IPlcSerial : IPlc {
/// <param name="_dataBits">The serial connection data bits</param>
/// <param name="_parity">The serial connection parity</param>
/// <param name="_stopBits">The serial connection stop bits</param>
/// <param name="_rtsEnable">Is RTS (Request to send) enabled?</param>
/// <param name="_station">The station number of the PLC</param>
void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, int _station = 1);
void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, bool _rtsEnable = true, int _station = 1);

/// <summary>
/// Tries to establish a connection with the device asynchronously
Expand Down
10 changes: 6 additions & 4 deletions MewtocolNet/Mewtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ public static PostInitEth<IPlcEthernet> Ethernet(IPAddress ip, int port = 9094,
/// <param name="dataBits">DataBits of the plc toolport</param>
/// <param name="parity">Parity rate of the plc toolport</param>
/// <param name="stopBits">Stop bits of the plc toolport</param>
/// <param name="rtsEnabled">Is RTS (Request to send) enabled?</param>
/// <param name="station">Plc station number 0xEE for direct communication</param>
/// <returns></returns>
public static PostInit<IPlcSerial> Serial(string portName, BaudRate baudRate = BaudRate._19200, DataBits dataBits = DataBits.Eight, Parity parity = Parity.Odd, StopBits stopBits = StopBits.One, int station = 0xEE) {
public static PostInit<IPlcSerial> Serial(string portName, BaudRate baudRate = BaudRate._19200, DataBits dataBits = DataBits.Eight, Parity parity = Parity.Odd, StopBits stopBits = StopBits.One, bool rtsEnabled = true, int station = 0xEE) {

var instance = new MewtocolInterfaceSerial();
instance.ConfigureConnection(portName, (int)baudRate, (int)dataBits, parity, stopBits, station);
instance.ConfigureConnection(portName, (int)baudRate, (int)dataBits, parity, stopBits, rtsEnabled, station);
return new PostInit<IPlcSerial> {
intf = instance
};
Expand All @@ -144,12 +145,13 @@ public static PostInit<IPlcSerial> Serial(string portName, BaudRate baudRate = B
/// Builds a serial mewtocol interface that finds the correct settings for the given port name automatically
/// </summary>
/// <param name="portName"></param>
/// <param name="rtsEnabled">Is RTS (Request to send) enabled?</param>
/// <param name="station">Plc station number 0xEE for direct communication</param>
/// <returns></returns>
public static PostInit<IPlcSerial> SerialAuto(string portName, int station = 0xEE) {
public static PostInit<IPlcSerial> SerialAuto(string portName, bool rtsEnabled = true, int station = 0xEE) {

var instance = new MewtocolInterfaceSerial();
instance.ConfigureConnection(portName, station);
instance.ConfigureConnection(portName, station, rtsEnable: rtsEnabled);
instance.ConfigureConnectionAuto();
return new PostInit<IPlcSerial> {
intf = instance
Expand Down
2 changes: 1 addition & 1 deletion MewtocolNet/MewtocolInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public abstract partial class MewtocolInterface : IPlc {
internal int tryReconnectDelayMs = 1000;

internal bool usePoller = false;
internal bool alwaysGetMetadata = true;
internal bool alwaysGetMetadata = false;

internal Func<int, Task> onBeforeReconnectTryTask;

Expand Down
10 changes: 8 additions & 2 deletions MewtocolNet/MewtocolInterfaceSerial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace MewtocolNet {

/// <inheritdoc/>
public sealed class MewtocolInterfaceSerial : MewtocolInterface, IPlcSerial {

private bool autoSerial;
Expand All @@ -32,6 +33,9 @@ public sealed class MewtocolInterfaceSerial : MewtocolInterface, IPlcSerial {
/// <inheritdoc/>
public StopBits SerialStopBits { get; private set; }

/// <inheritdoc/>
public bool RtsEnabled { get; private set; }

//Serial
internal SerialPort serialClient;

Expand Down Expand Up @@ -76,7 +80,7 @@ public override string GetConnectionInfo() {
}

/// <inheritdoc/>
public void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, int _station = 0xEE) {
public void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, bool rtsEnable = true, int _station = 0xEE) {

if (IsConnected)
throw new NotSupportedException("Can't change the connection settings while the PLC is connected");
Expand All @@ -87,6 +91,7 @@ public void ConfigureConnection(string _portName, int _baudRate = 19200, int _da
SerialParity = _parity;
SerialStopBits = _stopBits;
stationNumber = _station;
RtsEnabled = rtsEnable;

if (stationNumber != 0xEE && stationNumber > 99)
throw new NotSupportedException("Station number can't be greater than 99");
Expand Down Expand Up @@ -231,7 +236,8 @@ private async Task<PLCInfo> TryConnectAsyncSingle(string port, int baud, int dbi
Parity = par,
StopBits = sbits,
ReadTimeout = 100,
Handshake = Handshake.None
Handshake = Handshake.None,
RtsEnable = RtsEnabled,
};

PortName = port;
Expand Down
4 changes: 2 additions & 2 deletions MewtocolNet/PLCInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ internal PLCInfo (MewtocolInterface onInterface) {

internal bool TryExtendFromEXRT(string msg) {

var regexEXRT = new Regex(@"\%EE\$EX00RT00(?<icnt>..)(?<mc>..)..(?<cap>..)(?<op>..)..(?<flg>..)(?<sdiag>....)(?<ver>..)(?<hwif>..)(?<nprog>.)(?<csumpz>...)(?<psize>...).*", RegexOptions.IgnoreCase);
var regexEXRT = new Regex(@"\%..\$EX00RT00(?<icnt>..)(?<mc>..)..(?<cap>..)(?<op>..)..(?<flg>..)(?<sdiag>....)(?<ver>..)(?<hwif>..)(?<nprog>.)(?<csumpz>...)(?<psize>...).*", RegexOptions.IgnoreCase);
var match = regexEXRT.Match(msg);
if (match.Success) {

Expand Down Expand Up @@ -177,7 +177,7 @@ internal bool TryExtendFromEXRT(string msg) {

internal static bool TryFromRT(string msg, MewtocolInterface onInterface, out PLCInfo inf) {

var regexRT = new Regex(@"\%EE\$RT(?<cputype>..)(?<cpuver>..)(?<cap>..)(?<op>..)..(?<flg>..)(?<sdiag>....).*", RegexOptions.IgnoreCase);
var regexRT = new Regex(@"\%..\$RT(?<cputype>..)(?<cpuver>..)(?<cap>..)(?<op>..)..(?<flg>..)(?<sdiag>....).*", RegexOptions.IgnoreCase);
var match = regexRT.Match(msg);
if (match.Success) {

Expand Down
52 changes: 32 additions & 20 deletions MewtocolNet/RegisterBuilding/BuilderPatterns/RBuildAnon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ public class MultStructStp<T> : MultArrayStp<T> where T : struct {

public async Task WriteAsync(T value) {

var reg = (IRegister<T>)builder.Assemble(this);
await reg.WriteAsync(value);
var reg = builder.Assemble(this);
reg.isAnonymous = true;
await ((IRegister<T>)reg).WriteAsync(value);

}

public async Task<T> ReadAsync() {

var reg = (IRegister<T>)builder.Assemble(this);
return await reg.ReadAsync();
var reg = builder.Assemble(this);
reg.isAnonymous = true;
return await ((IRegister<T>)reg).ReadAsync();

}

Expand All @@ -41,15 +43,17 @@ public class MultStringStp<T> : MultArrayStp<T> where T : class {

public async Task WriteAsync(string value) {

var reg = (IStringRegister)builder.Assemble(this);
await reg.WriteAsync(value);
var reg = builder.Assemble(this);
reg.isAnonymous = true;
await ((IStringRegister)reg).WriteAsync(value);

}

public async Task<string> ReadAsync() {

var reg = (IStringRegister)builder.Assemble(this);
return await reg.ReadAsync();
var reg = builder.Assemble(this);
reg.isAnonymous = true;
return await ((IStringRegister)reg).ReadAsync();

}

Expand All @@ -71,15 +75,19 @@ public class MultTypedArr1D<T> : TypedArr1D<T> {

public async Task WriteAsync(T[] value) {

var reg = (IArrayRegister<T>)builder.Assemble(this);
await reg.WriteAsync(value);
var reg = builder.Assemble(this);
reg.isAnonymous = true;
var tReg = (IArrayRegister<T>)reg;
await tReg.WriteAsync(value);

}

public async Task<T[]> ReadAsync() {

var reg = (IArrayRegister<T>)builder.Assemble(this);
return await reg.ReadAsync();
var reg = builder.Assemble(this);
reg.isAnonymous = true;
var tReg = (IArrayRegister<T>)reg;
return await tReg.ReadAsync();

}

Expand All @@ -91,15 +99,17 @@ public class MultTypedArr2D<T> : TypedArr2D<T> {

public async Task WriteAsync(T[,] value) {

var reg = (IArrayRegister2D<T>)builder.Assemble(this);
await reg.WriteAsync(value);
var reg = builder.Assemble(this);
reg.isAnonymous = true;
await ((IArrayRegister2D<T>)reg).WriteAsync(value);

}

public async Task<T[,]> ReadAsync() {

var reg = (IArrayRegister2D<T>)builder.Assemble(this);
return await reg.ReadAsync();
var reg = builder.Assemble(this);
reg.isAnonymous = true;
return await ((IArrayRegister2D<T>)reg).ReadAsync();

}

Expand All @@ -111,15 +121,17 @@ public class MultTypedArr3D<T> : TypedArr3D<T> {

public async Task WriteAsync(T[,,] value) {

var reg = (IArrayRegister3D<T>)builder.Assemble(this);
await reg.WriteAsync(value);
var reg = builder.Assemble(this);
reg.isAnonymous = true;
await ((IArrayRegister3D<T>)reg).WriteAsync(value);

}

public async Task<T[,,]> ReadAsync() {

var reg = (IArrayRegister3D<T>)builder.Assemble(this);
return await reg.ReadAsync();
var reg = builder.Assemble(this);
reg.isAnonymous = true;
return await ((IArrayRegister3D<T>)reg).ReadAsync();

}

Expand Down
2 changes: 1 addition & 1 deletion MewtocolNet/SetupClasses/InterfaceSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class InterfaceSettings {
/// <summary>
/// Sets wether or not the interface should always retrieve metadata on connection start
/// </summary>
public bool AlwaysGetMetadata { get; set; } = true;
public bool AlwaysGetMetadata { get; set; } = false;

}

Expand Down

0 comments on commit 19db0a9

Please sign in to comment.