Hello,
I have established a communication network with one master and several slaves (2-5) using RS485.
I have written a code for arithmetic operation where in a master will send a value to slave through its serial monitor (eg:5+10;1) and answer will be displayed in slave's monitor (eg: 15) which then will be sent back to the master.
My query is that when I connect a second or more slaves to the network, then master sends value to all the slaves. But I want the master to select a particular slave to send the value and to perform operation. Earlier I had used i2c connection with Wire library that had Serial.beginTransmission(slave_number).
Is there anything similar to this in RS485?
Master code:
#include "RS485_protocol.h"
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 2;
SoftwareSerial rs485 (10,11); // receive pin, transmit pin
char input[10];
char res[10];
int charsRead = 0;
int sl;
boolean received = false;
void setup()
{
//Wire.begin(0);
Serial.begin(9600);
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT);
//Wire.onReceive(receiveEvent);
}
void loop()
{
Serial.flush();
if(Serial.available() == 0)
{
}
//delay(200);
while (Serial.available() > 0)
{
//delay(100);
// 1 becomes 10 while there is data in the buffer
charsRead = Serial.readBytesUntil('\n', input, sizeof(input)-1);
//input[charsRead] = '\0';
//number = atoi(input);
//Serial.println(number);
//Serial.println(number);
//delay(5);
sl = input[charsRead - 1] - '0';
input[charsRead - 1] = '\0';
}
//Serial.print("You entered: ");
if(charsRead > 0){
//Serial.println(sl);
Serial.println(input);
digitalWrite (ENABLE_PIN, HIGH); // enable sending
rs485.write(input);
digitalWrite (ENABLE_PIN, LOW);
charsRead = 0;
input[0] = 0;
}
int i=0;
//delay(100);
while(rs485.available()){
//delay(100);
digitalWrite (ENABLE_PIN, LOW);
char c;
c = rs485.read();
res[i++] = c;
//Serial.println(c);
digitalWrite (ENABLE_PIN, HIGH);
received = true;
}
if(i>0)
Serial.println(res);
}
Slave code:
#include "RS485_protocol.h"
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 2;
SoftwareSerial rs485 (10, 11); // receive pin, transmit pin
boolean replay = false;
int z = -1;
void setup()
{
Serial.begin(9600);
rs485.begin (28800);
pinMode (ENABLE_PIN, OUTPUT); // register event
//Wire.onRequest(requestEvent);
}
//char number = 0;
void loop()
{
char res[10] = {0};
char a[5] = {0};
char b[5] = {0};
char operation;
int i = 0;
char c;
boolean op2 = false;
digitalWrite (ENABLE_PIN, LOW);
while(rs485.available()){
// delay(100);
c = rs485.read();
Serial.println(c);
//delay(10);
//char c = Wire.read(); // receive byte as a character
if(c == ';')
{
Serial.println("End Received");
replay = true;
break;
}
if(c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
{
i = 0;
op2 = true;
operation = c;
continue;
}
if(op2)
b[i++] = c;
else
a[i++] = c;
}
digitalWrite (ENABLE_PIN, HIGH);
int x = atoi(a);
int y = atoi(b);
//Serial.println(x);
//Serial.println(y);
switch(operation)
{
case '+': z = x + y;
break;
case '-': z = x - y;
break;
case '*': z = x * y;
break;
case '/': z = x/y;
break;
case '(': z = max(x,y);
break;
case ')': z = min(x,y);
break;
}
//Serial.println(z);
if(z>0){
itoa(z, res, 10);
//Serial.println(res);
}
if(replay){
Serial.println(res);
digitalWrite (ENABLE_PIN, HIGH);
rs485.write(res);
digitalWrite (ENABLE_PIN, LOW);
replay=false;
}
}