arduino modbus master

WARNING: This code is no longer being maintained and it's NOT compatible with Arduino IDE 1.x.

We recommend you check out this improved version instead.

Below you will find an open source Arduino sketch that turns the microcontroller board into a Modbus RTU master. I have implemented only functions 3, read holding registers, and 16, preset multiple registers, of the modbus RTU master over serial line. I am using the Serial class. These functions are really easy to use, but at the same time not fully configurable. There are some known issues about the compliance with the modbus specifications of this implementation.

I have tried these functions using the USB interface at 9600 bps. It works good with the Wintech's Modsim (Modbus Slave Emulator). I have not yet fully tested it, though.

Modbus RTU over serial line compliance:

This implementation uses the Arduino Serial class. Although it is easy to use, it has very few configuration options. Therefore, this implementations DOES NOT fully comply with the Modbus over serial line specification in the following issues:

1. the character frame format is a 10-bit 8N1 (8 bits, no parity, and one stop bit). The specification requires even parity, and if no parity is used it must have 2 stop bits to fill out the 11-bit character frame. To comply with the specification the Serial class must be hacked.

2. only functions 3 and 16 have been implemented.

If you are not familiar with the modbus specifications, here is a translation of what each point means in practice:

1. you can use these functions without a problem with any other modbus compliant slave device, as long as that device allows a 10-bit 8N1 character frame format. You should be aware, though, that 8N1 is clearly stated in the specification as a non-valid Modbus character frame format.

2. under normal conditions (no communications errors) these functions work perfectly ok. In case of error in the communication, you will get most probably a checksum error.

3. With functions 3 and 16 you can read and write one or more words, respectively. These two functions should all you need for most applications. If you need to read a single bit, for example, you should do some processing on a word before and after using function 3.

As an alternative to this implementation, an Arduino Modbus Master library can be found on this site.

Also, you can find the Modbus RTU slave here.