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:
@@ -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));
|
||||
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user