Arduino to pc modbus communication using RS485

i use arduino nano as slave and pc as master. i send data from pc to arduino and it works but i can not find the right code to read data stored on arduino.
here is the code that i use to send data from pc to arduino:

#include <Servo.h> 
#define led1 2   // pin d2          
#define led2 5   //  pind5

LiquidCrystal lcd(8,9,10,11,12,13);   //    LCD pins (RS=D8, E=D9, D4=D10, D5=D11, D6=D12, D7=D13)              

Modbus bus;  //Initialize bus object for class Modbus.  

//for storing values for Modbus communication an array is declared with the three values initialized with zero.
uint16_t modbus_array[] = {0,0};  

void setup() {
  lcd.begin(16,2);                //Lcd set in 16x2 mode
  lcd.print("RS-485 Modbus");     //Welcome Message
  lcd.print("Arduino Slave");
   *         IDK WTF IS THIS
  bus = Modbus(1,1,4);        
  bus.begin(4800);    //9600 baudrate           

void loop() {
  // bus.poll() is used to write and receive value from the master Modbus
     if (modbus_array[0] == 0)    //Depends upon value in modubus_array[0] written by Master Modbus
    digitalWrite(led1,LOW);    //LED OFF if 0
     digitalWrite(led1,HIGH);  //LED ON if value other than 0
     lcd.print("L1:ON ");       
   if (modbus_array[1] == 0)    //Depends upon value in modbus_array[1] written by Master Modbus
    digitalWrite(led2,LOW);   //LED OFF if 0
     digitalWrite(led2,HIGH);  //LED ON if value other than 0
     lcd.print("L2:ON ");


Welcome to the forum and congratulations with the formatting of your code sample.

Can you detail the tool on the pc you are using?

Is your connection RS232C or RS485?

Does your pc tool allow you to do a read coil status / read input register / read holding register at the same address where you could write?

Does your tool show details about the traffic (messages) between PC and Arduino? Can you see if you are receiving something back from the Arduino?

thanks for your consideration
its rs485 and i am using the usb to rs485 convertor
the tool i am using is :"simple modbus master" app v8.1.2
and yes it does show the traffic

Can you see if you are receiving something back from the Arduino?

Do you see traffic coming back ?

I ask this because the last parameter of the Modbus(1,1,4) is the IO which you use to you control the direction of the RS485 on the side of the Arduino, this should be connected to the direction pin of the TTL to RS485 chip on your Arduino side.

If you open the ModbusRtu.h file you will find this:

 * @brief
 * DEPRECATED constructor for a Master/Slave.
 * USE Modbus(uint8_t, T_Stream&, uint8_t) INSTEAD.
 * @param u8id   node address 0=master, 1..247=slave
 * @param u8serno  serial port used 0..3 (ignored for software serial)
 * @param u8txenpin pin for txen RS-485 (=0 means USB/RS232C mode)
 * @ingroup setup
 * @overload Modbus::Modbus(uint8_t u8id, T_Stream& port, uint8_t u8txenpin)
Modbus::Modbus(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.