mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
@@ -14,6 +14,7 @@ internal class Program {
|
||||
|
||||
//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
|
||||
|
||||
@@ -34,6 +34,11 @@ namespace MewtocolNet {
|
||||
/// </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>
|
||||
@@ -42,8 +47,9 @@ namespace MewtocolNet {
|
||||
/// <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
|
||||
|
||||
@@ -128,12 +128,13 @@ namespace MewtocolNet {
|
||||
/// <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
|
||||
};
|
||||
@@ -144,12 +145,13 @@ namespace MewtocolNet {
|
||||
/// 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
|
||||
|
||||
@@ -106,7 +106,7 @@ namespace MewtocolNet {
|
||||
internal int tryReconnectDelayMs = 1000;
|
||||
|
||||
internal bool usePoller = false;
|
||||
internal bool alwaysGetMetadata = true;
|
||||
internal bool alwaysGetMetadata = false;
|
||||
|
||||
internal Func<int, Task> onBeforeReconnectTryTask;
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MewtocolNet {
|
||||
|
||||
/// <inheritdoc/>
|
||||
public sealed class MewtocolInterfaceSerial : MewtocolInterface, IPlcSerial {
|
||||
|
||||
private bool autoSerial;
|
||||
@@ -32,6 +33,9 @@ namespace MewtocolNet {
|
||||
/// <inheritdoc/>
|
||||
public StopBits SerialStopBits { get; private set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool RtsEnabled { get; private set; }
|
||||
|
||||
//Serial
|
||||
internal SerialPort serialClient;
|
||||
|
||||
@@ -76,7 +80,7 @@ namespace MewtocolNet {
|
||||
}
|
||||
|
||||
/// <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");
|
||||
@@ -87,6 +91,7 @@ namespace MewtocolNet {
|
||||
SerialParity = _parity;
|
||||
SerialStopBits = _stopBits;
|
||||
stationNumber = _station;
|
||||
RtsEnabled = rtsEnable;
|
||||
|
||||
if (stationNumber != 0xEE && stationNumber > 99)
|
||||
throw new NotSupportedException("Station number can't be greater than 99");
|
||||
@@ -231,7 +236,8 @@ namespace MewtocolNet {
|
||||
Parity = par,
|
||||
StopBits = sbits,
|
||||
ReadTimeout = 100,
|
||||
Handshake = Handshake.None
|
||||
Handshake = Handshake.None,
|
||||
RtsEnable = RtsEnabled,
|
||||
};
|
||||
|
||||
PortName = port;
|
||||
|
||||
@@ -134,7 +134,7 @@ namespace MewtocolNet {
|
||||
|
||||
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) {
|
||||
|
||||
@@ -177,7 +177,7 @@ namespace MewtocolNet {
|
||||
|
||||
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) {
|
||||
|
||||
|
||||
@@ -23,15 +23,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns {
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
@@ -41,15 +43,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns {
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
@@ -71,15 +75,19 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns {
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
@@ -91,15 +99,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns {
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
@@ -111,15 +121,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns {
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace MewtocolNet.SetupClasses {
|
||||
/// <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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user