I am using arduino uno (atmega328), my code doesn't work properly.
my code is,
: Master
#include "RS485_protocol.h"
const byte ENABLE_PIN = 7;
void fWrite (const byte what)
{
// rs485.write (what);
Serial.write (what);
}
int fAvailable ()
{
//rs485.available ();
Serial.available ();
}
int fRead ()
{
//return rs485.read ();
return Serial.read ();
}
void setup()
{
//Serial connection.
Serial.begin(9600););
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
}
void loop()
{
byte s=1;
byte r=5;
byte dir=1;
byte msg[] = {
s, // device id
r,
dir // direction
};
// send to slave
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
delay(10);
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
if(received)
{
if(buf[0]==0)
{
Serial.println("Reporting ");
Serial.println(" ");
Serial.print("Slave ID ");
Serial.println(buf[1]);
Serial.println(" ");
Serial.print("STATUS ");
if(buf[2]==1)
{
Serial.println("Operation Completed ");
Serial.println(" ");
}
else{
Serial.println("Operation Not Completed ");
Serial.println(" ");
}
}
}
clearbuf1(buf);
}
Master code end[/b]
: Slave
#include "RS485_protocol.h"
const byte ENABLE_PIN = 4;
void fWrite (const byte what)
{
Serial.write (what);
}
int fAvailable ()
{
return Serial.available ();
}
int fRead ()
{
return Serial.read ();
}
void setup()
{
//rs485.begin (9600);
pinMode (ENABLE_PIN, OUTPUT);
Serial.begin(9600);
}
void loop()
{
byte n=0;
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf));
if (received)
{
if(buf[0]==1
{
Serial.println("Iam slave 1");
n=1;
}
byte msg [] = {
0, // device 0 (master)
s, // slave id
n, // operation command
};
delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
delay(2000);
// end if something received
}
Slave End//
the problem is communication between two arduino's not done.
the connections are shown below attachment


