RS485 Communication

RS485 Communication

Arduino based PLC through RS485 port can connect with other devices which also use RS485 communication port

PLC available to use RS485:

20 I/Os PLC:

  • Ardbox Analog
  • Ardbox Relay

Ethernet PLC:

  • M-duino 21
  • M-duino 42
  • M-duino 58
  • M-duino 19R
  • M-duino 38R
  • M-duino 57R

Software:

Arduino IDE

Libraries:

Industrial Shields library

Then you can use the code:

There are two direction using RS485 port. The first one is working as a receiver and the second one is working as a sender. In this example there are both options. Receiver and sender.

First of all it is necessary to define the PLC where the program will be uploaded:

// Include Industrial Shields libraries
// avoid the comments on the product library required
// Include only one PLC library.
//#include <MDUINO.h>
//#include <MDUINO-Relay.h>
//#include <ARDBOX-Analog.h>
//#include <ARDBOX-Relay.h>

It is necessary avoid the // on the right product.

Then it is necessary to include the RS485.h library using the sentence:

#include <RS485.h>

To check if the RS485 port is working it is easy to use the serial monitor from the Arduino IDE using the right sentence inside the SETUP menu:

serial.begin(9600);

Then you can configurate the RS485 port using:

RS485.begin(38400);

Include both sentences inside the void setup() you can connect with other devices and use the serial monitor to read or writte the message sent between the devices.

NOTE: Check the velocity of transmition between the PLC and the Laptop and from the PLC to the device connected by RS485

To Receive a message by RS485 it is necessary to write the right sentence inside the void loop();

void loop() {
 // Print received byte when available
 if (RS485.available()) {
   byte rx = RS485.read();
   // Hexadecimal representation
   Serial.print("HEX: ");
   Serial.print(rx, HEX);
   // Decimal representation
   Serial.print(", DEC: ");
   Serial.println(rx, DEC);
  }
 }

DOWNLOAD The RECEIVED Message from the Arduino IDE

The received message is converted at Hexadecimal and printed by the the Serial monitor into the Arduino IDE. IT is necessary to have the USB cable connected between the PLC and the Laptop and select the right serial port and velocity of transmition.

To send a message by the RS485 it is necessary to write a diferente sentence inside the void loop();

Using the code below you can writte a message using the Serial Monitor from the Arduino IDE connecting the USB cable between the PLC and the Laptop and check the right Serial port on the Arduino IDE.

void loop() {
// Wait bytes in the serial port
if (Serial.available()) {
byte tx = Serial.read();

// Echo the byte to the serial port again
Serial.write(tx);

// And send it to the RS-485 port
RS485.write(tx);
}
}

DOWNLOAD The SEND Message from the Arduino IDE

Usually in a project the received sentence and the sended sentence can be mixed because it is necessary receive and send a message. When it happens it is necessary to confirm that the message received is finalized before send a new message becasue if the message has not finalized can apear some mistake on the communication.

Hardware configuration:

Using the Ethernet PLC it is not necessary to configurate any Switch.

Using the 20 I/Os PLC it is necessary to configurate the DIP Switch:

  • Using an Ardbox Analog: (Using RS485 Ardbox Analog loses Q0.1,Q0.2,Q0.7 outputs and I0.3 input)

/* IMPORTANT: SWITCHES configuration
FRONT SWITCH ZONE
Q0.1: OFF
RO: ON
Q0.2: OFF
DI: ON

COMMUNICATION SWITCH ZONE
DE-D7/Q0.7: OFF
RE-D4/I0.3: OFF
RX-D1/Q0.8: OFF
TX-D0/Q0.9: OFF
SDA-D2/I0.0: ON
SCL-D3/Q0.6: ON

  • Using an Ardbox Relay: (Using RS485 Ardbox Relay loses R6, R5, R2, R1 outputs)

FRONT SWITCH ZONE
R6: OFF
RO: O
R5: OFF
DI: ON

COMMUNICATION SWITCH ZONE
DE/R2: OFF
RE/R1: OFF
I0.3/RX: OFF
I0.2/TX: OFF
SCL/I0.1: ON
SDA/I0.0: ON

Comments are closed.