Modbus working 22/11/2024

This commit is contained in:
2024-11-22 11:54:01 +07:00
parent 869ab4bc21
commit 092429cec8
4 changed files with 592 additions and 179 deletions

View File

@@ -300,6 +300,7 @@ namespace FAtoPA
CloseConnection(client);
client = null;
}
}
catch (Exception e)
@@ -309,8 +310,13 @@ namespace FAtoPA
if (key != null && client != null)
{
ModbusClientRecord prev = null;
if (_event!=null) _event.Log("ModbusSlave: New Connection from " + key);
ModbusClientRecord prev = clientmap[key];
if (clientmap.ContainsKey(key))
{
prev = clientmap[key];
}
if (prev != null)
{
@@ -354,9 +360,14 @@ namespace FAtoPA
// 2 byte Register Count, index [10,11]
byte[] cmd = new byte[12];
try {
int readcount = await stream.ReadAsync(new byte[12], 0, 12);
int readcount = await stream.ReadAsync(cmd, 0, 12);
if (readcount >= 12)
{
Debug.WriteLine($"Read from {key}, size={readcount}");
String cmdString = BitConverter.ToString(cmd);
Debug.WriteLine($"cmd={cmdString}");
client.RXBytes += (uint)readcount;
UInt16 transactionID = GetTransactionID(cmd);
UInt16 protocolID = GetProtocolID(cmd);
@@ -364,7 +375,9 @@ namespace FAtoPA
Byte unitID = GetUnitID(cmd);
Byte functionCode = GetFunctionCode(cmd);
byte[] payload = GetModbusPayload(cmd);
Debug.WriteLine($"TransactionID={transactionID}, ProtocolID={protocolID}, Length={length}, UnitID={unitID}, FunctionCode={functionCode}, Payload={payload.Length}");
String payloadstring = BitConverter.ToString(payload);
Debug.WriteLine($"Payload={payloadstring}");
if (protocolID == 0 && length == payload.Length + 2 && unitID == 1)
{
if (functionCode == 3)
@@ -483,6 +496,8 @@ namespace FAtoPA
client.TXResponse++;
}
}
else if (readcount == 0) break; // connection closed
}
catch (Exception e)
{