modbus slave serial communication

hai friends…
i am just new for the modbus concepts practically. i need some clarifications

my master code is


  Basic.pde - example using ModbusMaster library

  Library:: ModbusMaster
  Author:: Doc Walker <>

  Copyright:: 2009-2016 Doc Walker

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  See the License for the specific language governing permissions and
  limitations under the License.


#include <ModbusMaster.h>

 #include "wiring_private.h" // pinPeripheral() function
    #define PIN_SERIAL1_RX 11
    #define PIN_SERIAL1_TX 10
    Uart Serial2( &sercom1, PIN_SERIAL1_RX, PIN_SERIAL1_TX, SERCOM_RX_PAD_0, UART_TX_PAD_2 ) ;

    void SERCOM1_Handler()
// instantiate ModbusMaster object
ModbusMaster node;

void setup()
  // use Serial (port 0); initialize Modbus communication baud rate

  // communicate with Modbus slave ID 2 over Serial (port 0)
  node.begin(1, Serial2);
      // Assign pins 10 & 11 SERCOM functionality
      pinPeripheral(PIN_SERIAL1_TX, PIO_SERCOM);
      pinPeripheral(PIN_SERIAL1_RX, PIO_SERCOM);

void loop()
  static uint32_t i;
  uint8_t j, result;
  uint16_t data[1];
   set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  node.setTransmitBuffer(0, lowWord(i));
  set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  node.setTransmitBuffer(1, highWord(i));
  slave: write TX buffer to (2) 16-bit registers starting at register 0
  result = node.writeMultipleRegisters(0, 2);
  slave: read (6) 16-bit registers starting at register 2 to RX buffer
  result = node.readHoldingRegisters(40000,1);
  // do something with data if read is successful
  if (result == node.ku8MBSuccess)
    for (j = 0; j < 1; j++)
      data[j] = node.getResponseBuffer(j);

i tried this code with modbus slave in python and its works well. i succesfully read the holding register 40000.

code]#!/usr/bin/env python

-- coding: utf_8 --

“”" Modbus TestKit: Implementation of Modbus protocol in python

(C)2009 - Luc Jean -
(C)2009 - Apidev -

This is distributed under GNU LGPL license, see license.txt

import sys

import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serial

#PORT = 0
PORT = ‘/dev/ttyUSB0’

def main():
logger = modbus_tk.utils.create_logger(name=“console”, record_format="%(message)s")

#Create the server
server = modbus_rtu.RtuServer(serial.Serial(PORT))

try:“running…”)“enter ‘quit’ for closing the server”)


slave_1 = server.add_slave(1)
slave_1.add_block(‘ch1’, cst.HOLDING_REGISTERS, 40000, 100)
slave_1.set_values(‘ch1’, 40000,5465)
slave_1.set_values(‘ch1’, 40002,255)
slave_1.set_values(‘ch1’, 40004,345)
slave_1.set_values(‘ch1’, 40008, 8872)
slave_1.set_values(‘ch1’, 40012, 126)
while True:
cmd = sys.stdin.readline()
args = cmd.split(’ ')

if cmd.find(‘quit’) == 0:

elif args[0] == ‘add_slave’:
slave_id = int(args[1])
sys.stdout.write(‘done: slave %d added\r\n’ % (slave_id))

elif args[0] == ‘add_block’:
slave_id = int(args[1])
name = args[2]
block_type = int(args[3])
starting_address = int(args[4])
length = int(args[5])
slave = server.get_slave(slave_id)
slave.add_block(name, block_type, starting_address, length)
sys.stdout.write(‘done: block %s added\r\n’ % (name))

elif args[0] == ‘set_values’:
slave_id = int(args[1])
name = args[2]
address = int(args[3])
values =
for val in args[4:]:
slave = server.get_slave(slave_id)
slave.set_values(name, address, values)
values = slave.get_values(name, address, len(values))
sys.stdout.write(‘done: values written: %s\r\n’ % (str(values)))

elif args[0] == ‘get_values’:
slave_id = int(args[1])
name = args[2]
address = int(args[3])
length = int(args[4])
slave = server.get_slave(slave_id)
values = slave.get_values(name, address, length)
sys.stdout.write(‘done: values read: %s\r\n’ % (str(values)))

sys.stdout.write(“unknown command %s\r\n” % (args[0]))

if name == “main”:

i tried to communicate my master by arduino modbus slave code.but i cant read the register.
i took slave code from this forum

please help me for to understand what are the things i need to focus to clear the mistake

thank you…

From the code it seems that you're using an Arduino Due or a similar board. If you don't mention a board explicitly we on this forum always assume you're using an Arduino UNO (the "standard" model). Please specify exactly what hardware you're using and how you wired it.

i took slave code from this forum

Post the actual code you tried. There are several slave sketches in that thread, I'm not sure if any of them matches your master code but I won't analyze them all because you're too lazy to post your code.