I have Code for Mod bus Library as below. I have Gone through lots of forum. How to assign device ID, slave address, length using below library function:

#include <SimpleModbusSlave.h>
#define  LED 9  

  HOLDING_REGS_SIZE // leave this one

unsigned int holdingRegs[HOLDING_REGS_SIZE]; 
void setup()
  modbus_configure(&Serial, 9600, SERIAL_8N2, 1, 2, HOLDING_REGS_SIZE, holdingRegs);    
  pinMode(LED, OUTPUT);

void loop()
  holdingRegs[ADC_VAL] = analogRead(A0); // update data to be read by the master to adjust the PWM
  analogWrite(LED, holdingRegs[PWM_VAL]>>2); // constrain adc value from the arduino master to 255

I am getting Error has.

SimpleModbusSlaveArduino.cpp: In function 'void setup()':
SimpleModbusSlaveArduino:79: error: 'SERIAL_8N2' was not declared in this scope

Error message for IDE

I have made changes in the lib as in forum. but I found no value read by my software.

Master sending below request on read mode. But not receiving anything.

12:31:02.953: Com8, Baud Rate: 9600, Data Bits: 8 Bits, Parity: None, Stop Bits: 1 Stop Bit
12:31:02.953: Echoback: On, RTS Control: On, Transmit Delay: 5 ms, Response Delay: 1000 ms
12:31:02.953: Read Holding Register(s), Repeat Loop
12:31:02.953: Device Address: 01h, Register: 0000h
12:31:02.953: |-> Read Register: 0000h
12:31:02.968: -> [01h] [0Dh] [00h] [00h] [00h] [01h] [CBh] [EDh]


  • 1
    Are you using the Uno or the Mega? Someone mentioned the Due, is that what you're using? You have both tags, but that doesn't make much sense IMO. Apr 19, 2014 at 13:22

2 Answers 2


Not sure where you got the library from, but if it's from this github repo, you're using it wrong.

The function prototype for modbus_configure is:

void modbus_configure(long baud, byte _slaveID, byte _TxEnablePin, unsigned int _holdingRegsSize, unsigned char _lowLatency);

The example file shows how to use it correctly

modbus_configure(115200, 1, 2, TOTAL_REGS_SIZE);
  • I tried already it is not working. i have pasted Error for your library
    – AMPS
    Apr 2, 2014 at 7:11
  • All those errors are, again, with using the library. Read the error carefully and look at the appropriate part of your code. Other than re-stating the error, I can't help you unless you show code.
    – sachleen
    Apr 2, 2014 at 17:16

In your first example the error is that SERIAL_8N2 is not declared. maybe you are using an old Arduino IDE? try latest 1.0.x or 1.5.x

in the second case TOTAL_REGS_SIZE is a pointer to int, while the function expect a char. Without code is hard to tell what is wrong and how, but in your first code it should be

modbus_configure(115200, 1, 2, HOLDING_REGS_SIZE);

also because you are not passing a reference to the array, you have to pass a pointer pointing the current element in the update function

  • As u said downaded v 1.5 now. But error is same error
    – AMPS
    Apr 3, 2014 at 4:33
  • seems like you are using an arduino DUE, witch is not explicitly supported by the lib.
    – Lesto
    Apr 3, 2014 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.