Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ReadWriteMultipleRegister support #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Modbus/IO/ModbusRtuTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static int ResponseBytesToRead(byte[] frameStart)
Modbus.ReadCoils or
Modbus.ReadInputs or
Modbus.ReadHoldingRegisters or
Modbus.ReadInputRegisters
Modbus.ReadInputRegisters or
Modbus.ReadWriteMultipleRegisters
=> frameStart[2] + 1,

Modbus.WriteSingleCoil or
Expand Down Expand Up @@ -134,4 +135,4 @@ internal override byte[] ReadRequest()

return frame;
}
}
}
4 changes: 2 additions & 2 deletions Modbus/Message/AbstractModbusMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public byte SlaveAddress
set => MessageImpl.SlaveAddress = value;
}

public byte[] MessageFrame =>
public virtual byte[] MessageFrame =>
MessageImpl.MessageFrame;

public virtual byte[] ProtocolDataUnit =>
Expand All @@ -62,4 +62,4 @@ public void Initialize(byte[] frame)
}

protected abstract void InitializeUnique(byte[] frame);
}
}
16 changes: 15 additions & 1 deletion Modbus/Message/ReadWriteMultipleRegistersRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public ReadWriteMultipleRegistersRequest(
writeData);
}

public override byte[] MessageFrame
{
get
{
byte[]? pdu = ProtocolDataUnit;
MemoryStream frame = new(1 + pdu.Length);

frame.WriteByte(SlaveAddress);
frame.Write(pdu, 0, pdu.Length);

return frame.ToArray();
}
}

public override byte[] ProtocolDataUnit
{
get
Expand Down Expand Up @@ -86,4 +100,4 @@ protected override void InitializeUnique(byte[] frame)
ReadRequest = ModbusMessageFactory.CreateModbusMessage<ReadHoldingInputRegistersRequest>(readFrame);
WriteRequest = ModbusMessageFactory.CreateModbusMessage<WriteMultipleRegistersRequest>(writeFrame);
}
}
}