Hi this is a little description of the situation:
I'm using the library software serial to communicate two Arduinos one is a UNO (with the library running) and the other is a Mega 2560 (using the Serial 3 port). The mega works like the master of this communication and the UNO like the slave returning all the bit's that receive to the master.
This is the code that I use to the test. Source YourDuino.com some parts were modified.
Master
#define SSerialTxControl 34 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
int a = 'a';
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial3.begin(4800);
Serial.println("YourDuino.com SoftwareSerial remote loop example");
Serial.println("Use Serial Monitor, type in upper window, ENTER");
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
}
void loop()
{
delay(5000);
a=a+1;
{
Serial.println("Nuevo Dato");
digitalWrite(Pin13LED, HIGH);
Serial.println("Valor leido");
Serial.println(a);
digitalWrite(SSerialTxControl, RS485Transmit);
Serial3.write(a); // Send byte to Remote Arduino
Serial3.flush();
Serial.println("Valor enviado");
Serial.println(a);
digitalWrite(Pin13LED, LOW); // Show activity
digitalWrite(SSerialTxControl, RS485Receive);
delay(100);
if(Serial3.available()){
byteReceived = Serial3.read();
Serial.println("Respuesta Recivida");
Serial.println(byteReceived);
while(Serial3.available()){
Serial3.read();
}
}
}
}
Slave
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 5 //Serial Receive pin
#define SSerialTX 4 //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;
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial.println("SerialRemote"); // Can be ignored
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(4800); // set the data rate
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
//Copy input data to output
if(RS485Serial.available())
{
Serial.println("Respuesta Recivida");
byteSend = RS485Serial.read();
Serial.println(byteSend);
delay(100);
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.write(byteSend);
RS485Serial.flush();
digitalWrite(SSerialTxControl, RS485Receive);
Serial.println("Respuesta enviada");
Serial.println(byteSend);
}// End If RS485SerialAvailable
}
When I test the code I receive this information in the serial monitor
YourDuino.com SoftwareSerial remote loop example
Use Serial Monitor, type in upper window, ENTER
Nuevo Dato
Valor leido
98
Valor enviado
98
Respuesta Recivida
32
Nuevo Dato
Valor leido
99
Valor enviado
99
Respuesta Recivida
98
Nuevo Dato
Valor leido
100
Valor enviado
100
Respuesta Recivida
99
Nuevo Dato
Valor leido
101
Valor enviado
101
Respuesta Recivida
100
Nuevo Dato
Valor leido
102
Valor enviado
102
Respuesta Recivida
101
Nuevo Dato
Valor leido
103
Valor enviado
103
Respuesta Recivida
102
Nuevo Dato
Valor leido
104
Valor enviado
104
Respuesta Recivida
103
Nuevo Dato
Valor leido
105
Valor enviado
105
Respuesta Recivida
104
Nuevo Dato
Valor leido
106
Valor enviado
106
Respuesta Recivida
105
Nuevo Dato
Valor leido
107
Valor enviado
107
Respuesta Recivida
106
Nuevo Dato
Valor leido
108
Valor enviado
108
Respuesta Recivida
107
I don't know why is happening this I could correct this adding 1 to the received data according to the test but I don't wanna till know why is this happening I tested the response in the Slave and this send and receive the correct value.
Kian