Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have to two arduino uno board used as master n slave connected using max rs485.
i need to write a code where master arduino will have to read the data stored in eeprom through rs485. please help as the which i have written does not work. here is the code

MASTER CODE
C++
#include <SoftwareSerial.h>
#include <EEPROM.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin

#define SSerialTxControl 3   //RS485 Direction control

#define RS485Transmit    HIGH
#define RS485Receive     LOW

#define Pin13LED         13

/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;

float f;
int eeAddress;
byte value;

int val = 0;

void setup() {//READ SLAVE ID FROM EEPROM//
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  pinMode(SSerialTxControl, OUTPUT); 
  
 
  
  digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit   

  RS485Serial.write(  EEPROM.get(eeAddress,f)); // Send byte to Remote Arduino
 // delay(10);
  digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit 

}

void loop() {
 

}



SLAVE CODE

C#
#include <EEPROM.h>
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin

#define SSerialTxControl 3   //RS485 Direction control

#define RS485Transmit    HIGH
#define RS485Receive     LOW
/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
float f;
int eeAddress;
byte value;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  float f = 123.000f;  //Variable to store in EEPROM.
  int eeAddress = 0;   //Location we want the data to be put.


  //One simple call, with the address first and the object second.
  EEPROM.put(eeAddress, f);

  pinMode(SSerialTxControl, OUTPUT);    
  RS485Serial.begin(9600);
  digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit       

  if (RS485Serial.available())  //Look for data from other Arduino
   {
    
    byteReceived = RS485Serial.read();    // Read received byte
    Serial.write(byteReceived);        // Show on Serial Monitor
    delay(10);
      
   }  
  

}

void loop() {
  // put your main code here, to run repeatedly:

}
Posted
Updated 1-Feb-16 2:28am
v2

I don't have an Arduino, but here are some tips:


  • Remove your email from the question (use the green 'Improve question' link). You might get spammed otherwise.
  • In both code snippets the setup() functions that perform the communication are never called. So there is nothing send and received.
  • The receiver (your slave) must use some kind of loop or blocking function to wait for arriving data. It actually checks only once if data are available.
  • Related to the above: The receiver (slave) must be started first. Otherwise it would miss the data send by the master before.
 
Share this answer
 
Comments
Member 12299248 1-Feb-16 8:38am    
hi
thank you for your answer.Actually i am new to coding so if you could please suggest how it should be coded with example.
thank you
Jochen Arndt 1-Feb-16 8:53am    
I don't have an Arduino. So I can't give a working example.

Because you are a beginner, I suggest to start with a C tutorial using a PC rather an embedded platform like the Arduino. Once you got the C basics, move over to the Arduino. I suggest this because compiling and debugging is simpler on a PC.

As a starting point learn about the C main() function. It is the entry point for each program. For your slave a very simple example might look like this:

int main()
{
// Initialise serial interface here

// Wait for serial data
while (!RS485Serial.available())
{
// A good implementation would check for time out here.
//if (now >= start_time + time_out_value)
//return -1;
}

// Receive data

// Process received data

// Exit program
return 0;
}
Member 12299248 1-Feb-16 8:59am    
EEPROM.get(eeAddress,f) this is the command which reads the data stored in eeprom of slave.so i need to send this over rs485.please help me with the code
Jochen Arndt 1-Feb-16 9:15am    
You have to call it from within your main() function. So add this to the end of your master source file:

int main()
{
Setup();
return 0;
}

Then the data should be send when there are no other errors in your code.
Member 12299248 23-Feb-16 0:25am    
HIthank you for your help.now i am able achieve the communication but now i am facing another problem that according to my code when led is on slave has to send the data but it is continuously sending the data which is not desired,so i have put counter so that when it sends the count will increase and data should be send only once but that is not working.
can you tell where i am wrong
As has been pointed out there are some large parts missing from your code. With Arduino there is a huge amount of working code available. Why not start there. For example:

arduino-info - SoftwareSerialRS485Example[^]

and here:
Communication between Arduino using RS485 - YouTube[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900