Added method to change connection params

This commit is contained in:
Felix Weiß
2022-07-18 16:47:43 +02:00
parent fe2d2b9fb9
commit 3c450eea97
3 changed files with 55 additions and 20 deletions

View File

@@ -47,29 +47,36 @@ class Program {
await interf.ConnectAsync( await interf.ConnectAsync(
(plcinf) => { (plcinf) => {
//reading a value from the register collection //reading a value from the register collection
Console.WriteLine($"BitValue is: {registers.BitValue}"); Console.WriteLine($"BitValue is: {registers.BitValue}");
Console.WriteLine($"TestEnum is: {registers.TestEnum}"); Console.WriteLine($"TestEnum is: {registers.TestEnum}");
//writing a value to the registers //writing a value to the registers
Task.Factory.StartNew(async () => { 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); 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 Task.Delay(2000);
await interf.SetRegisterAsync(nameof(registers.TestInt32), 100); 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)); 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)); 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)); 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"); 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)); interf.SetRegister(nameof(registers.TestTime), TimeSpan.FromSeconds(DateTime.Now.Second));
}); });

View File

@@ -123,8 +123,9 @@ namespace MewtocolNet {
internal NetworkStream stream; internal NetworkStream stream;
internal TcpClient client; internal TcpClient client;
internal readonly SerialQueue queue = new SerialQueue(); internal readonly SerialQueue queue = new SerialQueue();
private int RecBufferSize = 64; private int RecBufferSize = 128;
internal int SendExceptionsInRow = 0; internal int SendExceptionsInRow = 0;
internal bool ImportantTaskRunning = false;
#region Initialization #region Initialization
@@ -217,6 +218,23 @@ namespace MewtocolNet {
} }
/// <summary>
/// Changes the connections parameters of the PLC, only applyable when the connection is offline
/// </summary>
/// <param name="_ip">Ip adress</param>
/// <param name="_port">Port number</param>
/// <param name="_station">Station number</param>
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;
}
/// <summary> /// <summary>
/// Closes the connection all cyclic polling /// Closes the connection all cyclic polling
/// </summary> /// </summary>

View File

@@ -115,30 +115,40 @@ namespace MewtocolNet {
/// <returns>A byte array or null of there was an error</returns> /// <returns>A byte array or null of there was an error</returns>
public async Task<byte[]> ReadByteRange (int start, int count) { public async Task<byte[]> ReadByteRange (int start, int count) {
string startStr = start.ToString().PadLeft(5, '0'); var byteList = new List<byte>();
var wordLength = count / 2; var wordLength = count / 2;
if (count % 2 != 0) if (count % 2 != 0)
wordLength++; wordLength++;
string endStr = (start + wordLength - 1).ToString().PadLeft(5, '0');
string requeststring = $"%{GetStationNumber()}#RDD{startStr}{endStr}"; //read blocks of max 4 words per msg
var result = await SendCommandAsync(requeststring); 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) if (result.Success && !string.IsNullOrEmpty(result.Response)) {
return null;
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();
} }