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<ModbusRtu.h>
#include<LiquidCrystal.h>
#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.setCursor(0,1);
lcd.print("Arduino Slave");
delay(5000);
lcd.clear();
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
/*
* 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
bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0]));
////////////////////////////////////
if (modbus_array[0] == 0) //Depends upon value in modubus_array[0] written by Master Modbus
{
digitalWrite(led1,LOW); //LED OFF if 0
lcd.setCursor(0,0);
lcd.print("L1:OFF");
}
else
{
digitalWrite(led1,HIGH); //LED ON if value other than 0
lcd.setCursor(0,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
lcd.setCursor(8,0);
lcd.print("L2:OFF");
}
else
{
digitalWrite(led2,HIGH); //LED ON if value other than 0
lcd.setCursor(9,0);
lcd.print("L2:ON ");
}
}```