RS485 and RS232 implementation

RS485 and RS232 implementation

rs232-485 logo ny 100x100

In this post we are going to implement  RS485 ans RS232 communication protocols to the Industrial Shields PLC Ardbox and/or MDuino families.

IMPORTANT!!: 

RS232 is already available for Ardbox family and for MDuino family. Included Arduino libraries for this post

  • SoftwareSerial.h

The example presents the code for a Master device and for a Slave device to communicate through RS485 and RS232 protocols.

The routines of the sketches are the following:

#Master: The Master device reads (upon pressing “enter”) any characters that are typed from the Arduino IDE Serial monitor (Serial.read()). This  data is transmitted as byte type. The Master device reads the data and  resends it through RS485  bus (RS485Serial.write()).  After that, the Master is shifted to recive mode (RS485Receive) and it will read any bytes comming into the RS485 bus RS485Serial.read().  The read bytes are printed on the Serial monitor (Serial.write()). Finally, the Master device will print through RS232 bus “Hello World” characters (Serial1.write()). The described instructions run repeatedly in the void loop.

#Slave: Initially the Slave PLC  is set to receiving mode (digitalWrite(SSerialTxControl, RS485Receive)) and will read any bytes coming through RS485 bus RS485Serial.read().  The input bytes will be printed on Serial monitor (Serial.write()) and back to RS485 bus(RS485Serial.write()) .  Finally the Slave will read any incoming bytes from the RS232 bus (Serial1.read()) and resend them through the same protocol (Serial1.write()). The code on the .ino files allows easy-configuration for MDuino or Ardbox by commenting and/or uncommenting  the #define ARDBOX  / #define MDUINO instructions at the first lines of the sketch. Similarly, in order to enable RS232 communication  the #define ADDRS232Test  declaration should be uncommented. The .ino files can be downloaded from this link. In order to allow RS485 on Ardbox Family PLCs the RO, DI , RE , DE pins need to be enabled by setting the corresponding configuration switches to communication mode. This is described in the Ardbox Family User Guides. #Master code

#include

//Uncomment for test RS232
//#define ADDRS232Test

//Uncomment depending on the PLC you are using!
#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX        11 // RO  //RS485 Receive pin
#define SSerialTX        10 // DI  //RS485 Transmit pin
#define SSerialTxControl 7  // DE  //RS485 Direction pin
#define GroundRE         4  // RE
#elif MDUINO
#define SSerialRX        11 // RO  //RS485 Receive pin
#define SSerialTX        10 // DI  //RS485 Transmit pin
#define SSerialTxControl 14 // DE  //RS485 Direction pin
#define GroundRE         15 // RE
#endif

#define RS485Transmit    HIGH
#define RS485Receive     LOW
#define PinLED         13

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

int byteRecv;
int byteSend;

#ifdef ADDRS232Test
int counter = 0;
#endif

void setup() {
  Serial.begin(9600);
  
#ifdef ARDBOX
  while (!Serial);  
#endif
  
  Serial.println("Write some characters and press ENTER to send them through RS485");
  
  pinMode(PinLED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);  
  pinMode(GroundRE, OUTPUT);
  
  digitalWrite(SSerialTxControl, RS485Receive);
  digitalWrite(GroundRE,LOW);
 
  RS485Serial.begin(4800);

#ifdef ADDRS232Test
  Serial1.begin(9600);
#ifdef ARDBOX
  while (!Serial1);  
#endif
#endif  
}

void loop() {
  digitalWrite(PinLED, HIGH);

  if (Serial.available()) { // Read from console!
    byteRecv = Serial.read();
    
    digitalWrite(SSerialTxControl, RS485Transmit); 
    RS485Serial.write(byteRecv);
    digitalWrite(PinLED, LOW);    
    digitalWrite(SSerialTxControl, RS485Receive);       
  }
  
  if (RS485Serial.available()) { // Read from RS485!
    digitalWrite(PinLED, HIGH);
    byteRecv = RS485Serial.read();
    Serial.write(byteRecv); // Write to console!
    delay(10);
    digitalWrite(PinLED, LOW);
  }  

#ifdef ADDRS232Test
  if (counter > 5000) {
     Serial1.print("Hello World");
     counter = 0;
  }
  counter++;
#endif
}<pre>

#Slave code

 #include 

//Uncomment for test RS232
//#define ADDRS232Test

//Uncomment depending on the PLC you are using!
#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 7 // DE //RS485 Direction pin
#define GroundRE 4 // RE
#elif MDUINO
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 14 // DE //RS485 Direction pin
#define GroundRE 15 // RE
#endif

#define RS485Transmit HIGH
#define RS485Receive LOW
#define PinLED 13

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

int byteRecv;
int byteSend;

#ifdef ADDRS232Test
char inchar;
#endif

void setup() {
Serial.begin(9600);

#ifdef ARDBOX
while (!Serial);
#endif

Serial.println("RS485 Slave");

pinMode(PinLED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
pinMode(GroundRE, OUTPUT);

digitalWrite(SSerialTxControl, RS485Receive);
digitalWrite(GroundRE,LOW);

RS485Serial.begin(4800);

#ifdef ADDRS232Test
Serial1.begin(9600);
#ifdef ARDBOX
while (!Serial1);
#endif
#endif
}

void loop() {
if (RS485Serial.available()) { // Read from RS485 Master!
byteSend = RS485Serial.read();

digitalWrite(PinLED, HIGH);
delay(10);
digitalWrite(PinLED, LOW);
Serial.println(byteSend); // Write to console!
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.write(byteSend);
delay(1000); // Change if too slow!
digitalWrite(SSerialTxControl, RS485Receive);
}

#ifdef ADDRS232Test
if(Serial1.available() > 0) {
inchar = Serial1.read();
Serial.print(inchar);
}
#endif
}

Comments are closed.