Function Codes that are must have


Thread Starter

Michael Reed

I am getting fairly close to implementing MODBUS on a flow meter. Each meter will be a slave, and only have a few pieces of data. I am planning on implementing Function Codes 1 through 6 only. I thought about implementing functions 7, 8, 15, 16, and 17, but don't see the need. I probably will not implement any other functions. I have little (closer to none) actual hands on MODBUS experience. Would these be adaquate function codes for a very simple MODBUS slave, or should others be implemented? Attached are a list of all available functions. Thanks!
Definitely implemented
01 Read Coil Status
02 Read Input Status
03 Read Holding Registers
04 Read Input Registers
05 Force Single Coil
06 Preset Single Register

Pretty sure I will not implement but uncertain
07 Read Exception Status
08 Diagnostics
15 Force Multiple Coils
16 Preset Multiple Registers
17 Report Slave ID

Almost sure I will not implement
09 Program 484
10 Poll 484
11 Fetch Comm. Event Ctr
12 Fetch Comm. Event Log
13 Program Controller
14 Poll Controller
18 Program 884/M84
19 Reset Comm. Link
20 Read General Reference
21 Write General Reference
22 Mask Write 4X Register
23 Read/Write 4X Registers
24 Read FIFO Queue

Jerry Miille

You probably should consider FC15 and FC16 because some Masters can only write Coils and Registers using these commands and do not support FC5 and FC6. Otherwise, Function code 8, "loopback" option is also sometimes used to verify a remote connection so it would also be a good addition. I don't think you need anything else.

Jerry Miille

Lewis Bodden

MODBUS is referred to as a standard. There are more variations to "The Standard" than you can shake a stick at. First there is ASCII versus RTU. You should implement both to be "Compatible". There are variations on the packing of bytes with in a word. This should be configurable. There are problems with the format of Floating point numbers. The use of function codes varies widely depending on who you talk to. When using modems there are timing problems to consider. Delays and other timing considerations should be user configurable.

MODBUS is a dinosaur. Yes there are a lot of people out there still using it. There are more efficient methods of communications. You may want to consider another port with TCP/IP.

So, if you are developing a simple MODBUS ACSII device only you will eliminate half of your potential customer base. With only the first six function codes implemented you will lose yet another large chunk. Implementing functions 7, 8, 15, 16, and 17 is highly important. The more advanced users and software will use these function codes. Functions 11, 12 and 19 would be nice to help with diagnostics. Functions 22 and 23 could be used by more advanced users if you device lends itself to their use.

I have used MODBUS in many applications. If I can be of any help let me know.

[email protected]

Alex Pavloff

From the Modbus TCP spec available from (which applies here too):

3.1 Class 0

This is the minimum useful set of functions, for both a MASTER and a SLAVE.
read multiple registers (fc 3)
write multiple registers (fc 16)

3.2 Class 1

This is the additional set of functions which is commonly implemented and
interoperable. As explained before, many slaves choose to treat input,
output, discrete and register as equivalent.

read coils (fc 1)
read input discretes (fc 2)
read input registers (fc 4)
write coil (fc 5)
write single register (fc 6)
read exception status (fc 7)
This function typically has a different meaning for each slave family

And there's more too. Take a look!

Alex Pavloff
Software Engineer
Eason Technology

Steven E. Braun

Having programmed extensively over the years with a Modbus RTU Master and various flow meters, here is my opinion on how to set up your Modbus Slave.

First and most importantly, provide a "scratch pad" location that can be used to contiguously stack values so they can be read by the Master with one or two read commands. If you use floating point (or double integer) values as well as integer values, provide more than one scratch pad location for reads. Also, if you do use 32 bit values, provide some kind of capability for swapping the integer values before transmitting so Modbus RTU Masters that cannot read double precision values can get the byte ordering correctly without a lot of programming effort.

Use Function 3 for reads and use Function 16 for writes. Do not bother with Function 6. Function 16 can also do a single register write. Functions 1 and 2 are nice but not really necessary. I usually prefer to read status bits with packed registers using Function 3. Function 5 is fine if you are going to do some kind of control. Function 15 is OK if you have lots of bits to write but requires more Master programming to mask out the irrelevant bits. For this reason I prefer Function 5.

From my 15 years of experience of providing the RTU Master for the GE Fanuc PLC, I can tell you one thing about the majority of those that have programmed it (including myself): Make it as easy as possible to get any and as much data in as few reads as possible.

Steven E. Braun
SEBraun, Inc.
1126 Sudden Valley
Bellingham, WA. 98226 USA
Office 360.734.4817
[email protected]
Not adding codes 15 & 16 will make your product pretty much unmarketable.

I'd say function #3 and #16 (read & write multiple registers) are the two core/critical functions that ALL Modbus masters will expect. You may only have 1 or 2 words of data, but even to read 1 register, most masters will try to use function #16 with a count of 1.

Best regards

Lynn August Linse, Senior IA Application Engineer
15353 Barranca Parkway, Lantronix Inc, Irvine CA 92618
[email protected]
Tel: (949)279-3969 Fax: (949)453-7152
Get Plugged-In to the Lantronix quarterly solutions magazine - subscribe today at