mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
Added method to change connection params
This commit is contained in:
@@ -57,6 +57,13 @@ class Program {
|
||||
//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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// Closes the connection all cyclic polling
|
||||
/// </summary>
|
||||
|
||||
@@ -115,30 +115,40 @@ namespace MewtocolNet {
|
||||
/// <returns>A byte array or null of there was an error</returns>
|
||||
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;
|
||||
if (count % 2 != 0)
|
||||
wordLength++;
|
||||
|
||||
string endStr = (start + wordLength - 1).ToString().PadLeft(5, '0');
|
||||
|
||||
//read blocks of max 4 words per msg
|
||||
for (int i = 0; i < wordLength; i+=8) {
|
||||
|
||||
int curWordStart = start + i;
|
||||
int curWordEnd = curWordStart + 7;
|
||||
|
||||
string startStr = curWordStart.ToString().PadLeft(5, '0');
|
||||
string endStr = (curWordEnd).ToString().PadLeft(5, '0');
|
||||
|
||||
string requeststring = $"%{GetStationNumber()}#RDD{startStr}{endStr}";
|
||||
var result = await SendCommandAsync(requeststring);
|
||||
|
||||
if(result.Success && !string.IsNullOrEmpty(result.Response)) {
|
||||
if (result.Success && !string.IsNullOrEmpty(result.Response)) {
|
||||
|
||||
var res = result;
|
||||
var bytes = result.Response.ParseDTByteString(8 * 4).HexStringToByteArray();
|
||||
|
||||
var bytes = result.Response.ParseDTByteString(wordLength * 4).HexStringToByteArray();
|
||||
|
||||
if (bytes == null)
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return bytes.BigToMixedEndian().Take(count).ToArray();
|
||||
byteList.AddRange(bytes.BigToMixedEndian().Take(count).ToArray());
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return byteList.ToArray();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user