Search code examples
protocolsmodbus

Modbus 20 (0x14) Read File Record - How long is the file


I am implementing Modbus function 20 (0x14) Read File Record.

Modbus Spec - function 20 (0x14) Read File Record. 
-------------------------------------------------------------------

Request: 
| Name                          | Size      | Example BYTES 
*-------------------------------*-----------*----------------------
| Function code                 | 1 Byte    | 0x14
| Byte Count                    | 1 Byte    | 0x07 to 0xF5 bytes
| Sub-Req. x, Reference Type    | 1 Byte    | 06
| Sub-Req. x, File Number       | 2 Bytes   | 0x0001 to 0xFFFF
| Sub-Req. x, Record Number     | 2 Bytes   | 0x0000 to 0x270F
| Sub-Req. x, Record Length     | 2 Bytes   | N

Response: 
| Name                          | Size      | Example BYTES 
*-------------------------------*-----------*----------------------
| Function code                 | 1 Byte    | 0x14
| Resp. data Length             | 1 Byte    | 0x07 to 0xF5
| Sub-Req. x, File Resp. length | 1 Byte    | 0x07 to 0xF5
| Sub-Req. x, Reference Type    | 1 Byte    | 6
| Sub-Req. x, Record Data       | N*2 Bytes |

Error
| Name                          | Size      | Example 
*-------------------------------*-----------*----------------------
| Error code                    | 1 Byte    | 0x94
| Exception code                | 1 Byte    | 01 ILLEGAL FUNCTION 
                                              02 ILLEGAL DATA ADDRESS 
                                              03 ILLEGAL DATA VALUE 
                                              04 SERVER DEVICE FAILURE 
                                              08 MEMORY PARITY ERROR

Source: http://www.modbus.org/specs.php

The file that I am requesting is 100 long. I made a file request that has a offset (Record Number) of 89 for 20 bytes long (Record Length). This should cause an error as I am requesting more bytes then are in the file.

  • What error will I get?
  • How do I determine how long the file is?

I am hoping I do not have to make a request for a file, and if there is an error, try again with a smaller length until I get a successful response.


Solution

  • This is old, but I'll answer, because I'm just implementing a Modbus slave, so...

    You will most likely get 02. Standards conforming Modbus will only return 03 with this command if bytecount is off bounds, that is less than 7 or more than 245. The standard is quite vague with this command, and it only really makes sense with Modicon PLC's, anyway. Other suppliers either don't implement these or "do whatever the eff they want".

    I for example mapped our string registers into these.