how to write data like string from industruino to pc through modbus network(rs485=usb)

i want to send data to PLC from arduino.So I am considring industruino as master and and PLC as slave ..BUT currently m not having PLC so  I have to test it on modscan32 whether data is going or not..on holding register ...on modscan32 holding register series is like 0x4000001,0X4000002. how to write data on these series of register

Sunil Mali
Sunil Mali
| 4 1 1
Asked on 2/13/20, 10:41 AM

Hi, modbus registers are 16-bit so each register can hold two 8-bit characters.

If you want the Master to send registers to the Slave, you can use function 16 PRESET_MULTIPLE_REGISTERS; that will fill the holding registers at the addresses you mention. You can start from the Modbus RTU Master example at and check that library's documentation.

modbus_construct(&packets[PACKET1], SlaveID, PRESET_MULTIPLE_REGISTERS, 0, 4, 0);

This constructor allows you to write 4 registers (e.g. 8 8-bit characters) to slave addresses 0x40001-0c40004 from the Master's register index 0-3.


| 1 1 3
Answered on 2/14/20, 1:05 AM

can we wtire on slave using serial.write() .

Sunil Mali
on 2/19/20, 6:32 AM

Modbus can pass registers only, via a register array. You can use serial.write() to communicate via the RS485 port to another device that has an RS485, such as this example with an arduino UNO

on 2/19/20, 8:59 AM

an we use serial.write() to write on PLC??how can we check Serial.write() is actually writing data or not.Because we dont have PLC, we witing code for write on rs485port on PLC.modbus slave simulator tool is useful or not??

Sunil Mali
on 2/20/20, 9:43 AM

#include <Wire.h>
#include <Indio.h>
#include <SimpleModbusMaster.h>
#include <UC1701.h>
static UC1701 lcd;

  this example uses holding registers:
  [0-3] sends 4 registers (int) to the slave: status of 4 LEDs = digital outputs of the slave
  [6-7] reads 2 registers (int) from the slave: status of 2 PUSH BUTTONS = digital inputs of the slave
  actually we read 4 registers but only 2 are connected to buttons

//////////////////// Port information ///////////////////
#define baud 9600  // use 9600 on D21G
#define timeout 2000
#define polling 20 // the scan rate, standard was 200
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 9                                                           // INDUSTRUINO RS485
#define SlaveID 1

// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 4                                          // e.g. INDIO digital I/O

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
  PACKET1,                                          // set 4 registers
  TOTAL_NO_OF_PACKETS // leave this last entry

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

void setup()

  lcd.setCursor(1, 1);
  lcd.print("hello Industruino!");
  lcd.setCursor(1, 3);
  lcd.print("Modbus RTU Master");

  // Initialize each packet: packet, slave-id, function, start  slave indexof, number of regs, start of master index
  // set 4 registers
modbus_construct(&packets[PACKET1], SlaveID, PRESET_MULTIPLE_REGISTERS, 0, 4, 0);

// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
// Serial = INDUSTRUINO RS485  -- Serial1 on 32u4/1286

void loop()
  modbus_update();                          // send Master request to Slave, as defined above
  for (int u = 0; u < 3; u++) {
    regs[u] = 4;





I have written this code to write on modscan tool.I am using usb to rs485 converter between arduino and pc.But in modscan no data getting reflected.Its showing "**MODBUS message TIMED-OUT**" and "Recieved invalid Response to MODBUS Query".


Sunil Mali
Sunil Mali
| 4 1 1
Answered on 2/19/20, 6:26 AM

hi, it looks like your modscan tool is a MASTER, not a slave. the master sends out queries, the slave only responds to queries. you can use the slave example of the Industruino to make it work with a master device.

on 2/19/20, 8:56 AM

I tried with modbus slave tool, slave tool does not reflect any data. is the above code is correct? is below writes the data to holding register? for (int u = 0; u

Sunil Mali
on 2/19/20, 12:01 PM

you do not need to include the 'Serial.begin(9600);'. the code should work; you need to check this at the slave side: slave_id = 1, baud=9600, format SERIAL_8N2. if that is all fine, then try swapping the A/B wires on the RS485 just to try if that makes a difference. also note that there is a hardware switch on the baseboard to disable the RS485, it should be enabled by default but just double check

on 2/20/20, 12:35 AM

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This community is for professionals and enthusiasts of our products and services.

Read Guidelines

Question tools

60 follower(s)


Asked: 2/13/20, 10:41 AM
Seen: 2838 times
Last updated: 2/19/20, 12:07 PM