Problem in software serial communication

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

Looks like your dropping bits, try a lower baud rate. A should be a byte not an int!.

Mark

What is SSerialTxControl all about ?

...R

Hi, First R.

What is SSerialTxControl all about ?

...R

That is the control pin for the RS485 chip, to alternate between transmition and reception.

Second Mark.

Looks like your dropping bits, try a lower baud rate. A should be a byte not an int!.

Mark

I made the correction suggested but I can't drop the baud rate because In need to send a lot of data in a short period of time the problem is still present but I'm worried to add one bit to the received byte to correct the data because I don't know is this problem is particular of this program or is a general correction.
In this post is attach a capture of the test to look the behavior of the data.

What is the RS485 chip? Have you a link to its datasheet?

Might there be issues surrounding the switching of direction? Should there be a delay, for example, or some other process assiciated with the change?

...R

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

Look at the above, your not just of by one. It looks to me like the LS bit is most often lost but some times you drop more than one bit. Note that 98 (sent) - 32(rx) = 66 = 64 + 2 , that's 2 bits dropped.

Where did RS485 get in to this? you have two Arduinos talking over serial - no RS485 and no flow control required!.

Mark

Hi, I'm using the SN75176BP IC the datasheet cna be found in this address http://www.ti.com/lit/ds/symlink/sn65176b.pdf. Second I use RS485 because I'm planing to use this communication in a industrial enviroment were can be interference from different factors and this standar is thinked to work in this enviroment.

Are the two arduinos directly connected or is there something else in between them? Provide a circuit diagram. Three things to try,

  1. drop the baud rate and find out if the problem persists.

  2. try it without all the RS485 flow control stuff - does that work. IF it does then increase the baud rate and set how high you can get it to go

  3. Can you use hardware serial on the slave instead of software serial (this would require that you remove any debug statements from the slave code).

Mark

SN75176BP

I fail to see the point of putting this chip in as it deals with bidirectional comms where as the UARTS provide two uni directional links not one bi direction link (no common wire with the UART!).

Mark

It sounds to me like you are using an Arduino as a temporary substute for an RS485 connection that you will be using later. If that's the case forget all about the RS485 stuff and external chips until you get the regular Serial comms working between the two Arduinos.

...R