Hello friends I'm using Serial Software in Master and Serial Hardware in Slave to make a communication
Master asks all Slaves to send data.
Everything works perfectly without data loss when delay(2000);
When the delay is less than 2000 the data does not arrive correctly
I wonder if there is any way to fix this down so that communication becomes faster without data loss
I'm using:
1 - Arduino MEGA 2560 ( Master )
10 - Arduino UNO ( Slave )
12 - TTL MAX RS-485
Master Code:
/*
RS485
18/10/19
*/
#include<SoftwareSerial.h>
#define MAX_PIN 2 //MAX485 control pin
SoftwareSerial RS485(11,12);
String id_sensor_t;
char tipo_t;
char status_vaga_t;
void setup(){
RS485.begin(9600);
Serial.begin(9600);
pinMode(MAX_PIN, OUTPUT);
}
void loop(){
SendRequest();
CheckMsg();
}
void CheckMsg(){
if(RS485.available() > 0){
String received = RS485.readString();
int size_msg = received.length();
Serial.println(received);
}
}
void SendRequest(){
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("001");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("002");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("003");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("004");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("005");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("006");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("007");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("008");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("009");
digitalWrite(MAX_PIN,LOW);
delay(2000);
digitalWrite(MAX_PIN, HIGH); //Enable max485 MAX_PIN
RS485.write("010");
digitalWrite(MAX_PIN,LOW);
//delay(2000);
}
Slave Code:
/*
RS485
18/10/19
*/
#define MAX_PIN 2 //MAX485 control pin
#define trig 10
#define echo 11
String id = "001";
char type = '0';
char status_device = '0';
String msg;
void setup(){
Serial.begin(9600);
pinMode(MAX_PIN, OUTPUT);
//ULTRASONICO
pinMode(trig, OUTPUT);
pinMode(trig, LOW);
pinMode(echo, INPUT);
}
void loop(){
ReceiveMsg();
}
void ReceiveMsg(){
if(Serial.available() > 0){
String received = Serial.readString();
int size_msg = received.length();
// Se receber o id então envia o status
if (received == id){
SendMsg();
}
}
}
void SendMsg(){
int size_msg = id.length();
char char_array[size_msg];
id.toCharArray(char_array, size_msg);
char msg[10] = {'[',id[0],id[1],id[2],'.',type,'.',status_device,']'};
digitalWrite(MAX_PIN, HIGH); //Enable max485 transmission
Serial.write(msg);
delay(200);
digitalWrite(MAX_PIN,LOW); //Disable max485 transmission mode
delay(500);
}
Can someone help me? Thank you all !!


