From 3c450eea97af5603097af595a65a56ea77163bb5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Wei=C3=9F?=
<72068105+Sandoun@users.noreply.github.com>
Date: Mon, 18 Jul 2022 16:47:43 +0200
Subject: [PATCH] Added method to change connection params
---
Examples/Program.cs | 23 ++++++++-----
MewtocolNet/Mewtocol/MewtocolInterface.cs | 20 +++++++++++-
.../Mewtocol/MewtocolInterfaceRequests.cs | 32 ++++++++++++-------
3 files changed, 55 insertions(+), 20 deletions(-)
diff --git a/Examples/Program.cs b/Examples/Program.cs
index c54cd40..0ecfb80 100644
--- a/Examples/Program.cs
+++ b/Examples/Program.cs
@@ -47,29 +47,36 @@ class Program {
await interf.ConnectAsync(
(plcinf) => {
- //reading a value from the register collection
+ //reading a value from the register collection
Console.WriteLine($"BitValue is: {registers.BitValue}");
Console.WriteLine($"TestEnum is: {registers.TestEnum}");
- //writing a value to the registers
+ //writing a value to the registers
Task.Factory.StartNew(async () => {
- //set plc to run mode if not already
+ //set plc to run mode if not already
await interf.SetOperationMode(OPMode.Run);
+
+ int startAdress = 10000;
+ int entryByteSize = 20 * 20;
+
+ var bytes = await interf.ReadByteRange(startAdress, entryByteSize);
+ Console.WriteLine($"Bytes: {string.Join('-', bytes)}");
+
await Task.Delay(2000);
await interf.SetRegisterAsync(nameof(registers.TestInt32), 100);
- //adds 10 each time the plc connects to the PLCs INT regíster
+ //adds 10 each time the plc connects to the PLCs INT regíster
interf.SetRegister(nameof(registers.TestInt16), (short)(registers.TestInt16 + 10));
- //adds 1 each time the plc connects to the PLCs DINT regíster
+ //adds 1 each time the plc connects to the PLCs DINT regíster
interf.SetRegister(nameof(registers.TestInt32), (registers.TestInt32 + 1));
- //adds 11.11 each time the plc connects to the PLCs REAL regíster
+ //adds 11.11 each time the plc connects to the PLCs REAL regíster
interf.SetRegister(nameof(registers.TestFloat32), (float)(registers.TestFloat32 + 11.11));
- //writes 'Hello' to the PLCs string register
+ //writes 'Hello' to the PLCs string register
interf.SetRegister(nameof(registers.TestString2), "Hello");
- //set the current second to the PLCs TIME register
+ //set the current second to the PLCs TIME register
interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));
});
diff --git a/MewtocolNet/Mewtocol/MewtocolInterface.cs b/MewtocolNet/Mewtocol/MewtocolInterface.cs
index 815140c..ed60e05 100644
--- a/MewtocolNet/Mewtocol/MewtocolInterface.cs
+++ b/MewtocolNet/Mewtocol/MewtocolInterface.cs
@@ -123,8 +123,9 @@ namespace MewtocolNet {
internal NetworkStream stream;
internal TcpClient client;
internal readonly SerialQueue queue = new SerialQueue();
- private int RecBufferSize = 64;
+ private int RecBufferSize = 128;
internal int SendExceptionsInRow = 0;
+ internal bool ImportantTaskRunning = false;
#region Initialization
@@ -217,6 +218,23 @@ namespace MewtocolNet {
}
+ ///
+ /// Changes the connections parameters of the PLC, only applyable when the connection is offline
+ ///
+ /// Ip adress
+ /// Port number
+ /// Station number
+ public void ChangeConnectionSettings (string _ip, int _port, int _station = 1) {
+
+ if (IsConnected)
+ throw new Exception("Cannot change the connection settings while the PLC is connected");
+
+ ip = _ip;
+ port = _port;
+ stationNumber = _station;
+
+ }
+
///
/// Closes the connection all cyclic polling
///
diff --git a/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs b/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs
index 02061a4..0c6636b 100644
--- a/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs
+++ b/MewtocolNet/Mewtocol/MewtocolInterfaceRequests.cs
@@ -115,30 +115,40 @@ namespace MewtocolNet {
/// A byte array or null of there was an error
public async Task ReadByteRange (int start, int count) {
- string startStr = start.ToString().PadLeft(5, '0');
+ var byteList = new List();
+
var wordLength = count / 2;
if (count % 2 != 0)
wordLength++;
- string endStr = (start + wordLength - 1).ToString().PadLeft(5, '0');
- string requeststring = $"%{GetStationNumber()}#RDD{startStr}{endStr}";
- var result = await SendCommandAsync(requeststring);
+ //read blocks of max 4 words per msg
+ for (int i = 0; i < wordLength; i+=8) {
- if(result.Success && !string.IsNullOrEmpty(result.Response)) {
+ int curWordStart = start + i;
+ int curWordEnd = curWordStart + 7;
- var res = result;
+ string startStr = curWordStart.ToString().PadLeft(5, '0');
+ string endStr = (curWordEnd).ToString().PadLeft(5, '0');
- var bytes = result.Response.ParseDTByteString(wordLength * 4).HexStringToByteArray();
+ string requeststring = $"%{GetStationNumber()}#RDD{startStr}{endStr}";
+ var result = await SendCommandAsync(requeststring);
- if (bytes == null)
- return null;
+ if (result.Success && !string.IsNullOrEmpty(result.Response)) {
- return bytes.BigToMixedEndian().Take(count).ToArray();
+ var bytes = result.Response.ParseDTByteString(8 * 4).HexStringToByteArray();
+
+ if (bytes == null) {
+ return null;
+ }
+
+ byteList.AddRange(bytes.BigToMixedEndian().Take(count).ToArray());
+
+ }
}
- return null;
+ return byteList.ToArray();
}