Hello Experts,
I am trying to do RS485 Communication between two Arduino Mega Boards. One of the Board is configured as a Transmitter and the other one is configured as a Receiver.
The connections are :
- DI (data in) to pin 11
- RO (receive out) to pin 10
- DE (data enable) and RE (receive enable) jumpered together and to pin 3
- Vcc and Gnd connected
- A and B : the RS485 pair
The transmitted data packet is :
int packet[]={0x1, 0x2, 0x3, 0x4, 0x5, 0x6};
The received data packet is :
255 255 255 255 255 0
Can anyone help me with the reason, I am not getting the same packet as transmitted.
The transmitter sketch is :
#include <SoftwareSerial.h>
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
int byteReceived;
int byteSend;
int packet[]={0x1, 0x2, 0x3, 0x4, 0x5, 0x6};
void setup()
{
Serial.begin(9600);
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
RS485Serial.begin(9600); // set the data rate
}
void loop()
{
int i = 0;
for (i = 0; i<= 5; i++) {
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.write(byteReceived); // Send byte to Remote Arduino
}
delay(10);
digitalWrite(SSerialTxControl, RS485Receive); // Enable RS485 Transmit
digitalWrite(Pin13LED, LOW); // Show activity
digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
delay(2000);
}
The Receiver Sketch is :
#include <SoftwareSerial.h>
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
int byteReceived;
int byteSend;
void setup()
{
Serial.begin(9600);
Serial.println("SerialRemote");
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive);
RS485Serial.begin(9600);
}
void loop()
{
//Copy input data to output
while (RS485Serial.available())
{
byteSend = RS485Serial.read(); // Read the byte
Serial.print(byteSend);
Serial.print(" ");
}
Serial.println("");
delay(2000);
}