debevv / nanoMODBUS

A compact MODBUS RTU/TCP C library for embedded/microcontrollers
MIT License
282 stars 58 forks source link

Add registers count checking for file_read and file_write operations #35

Closed arhiv6 closed 1 year ago

arhiv6 commented 1 year ago

MODBUS Application Protocol Specification does not require it explicitly but without this checking PDU of a request or response may exceed 253 bytes.

arhiv6 commented 1 year ago

I relied on document MODBUS APPLICATION PROTOCOL SPECIFICATION V1.1b3 from "Modbus Organization, Inc". I think this is the latest official documentation. But the document have contains a mistake in chapter "6.15 21 (0x15) Write File Record". Sample request defined "Request data length" as 0xFB bytes maximum, but in "Figure 25: Write File Record state diagram" checked 0xF5 bytes (I think it is copy-paste mistake from Figure 24). In my previous commit I used the length from the diagram and I copied the mistake. Right size is 0xFB of course. Checking: PDU(253bytes)-FunctionCode(1byte)-RequestDataLength(1byte)=251 bytes=0xFB bytes.

Note: Where did the number 0xF5 come from Figure 24? For operation "Read File Record" this value is 0xF5 because request may be maximum: PDU(253bytes)-FunctionCode(1byte)-ByteCount(1byte)-N*SubRequest(7bytes). N can be up to 35 maximum, because 6bytes remain and it's not enough for one more SubRequest. Therefore 35*SubRequest(7bytes)=245bytes=0xF5 bytes.

debevv commented 1 year ago

Got it, so it was really an error. Thank you