RS-485 communication example using our libraries.

RS-485 communication example using our libraries.

In this example we will see how to use SimpleComm libraries and how to use the RS-485 connection through Master/Slave system. Master/slave is a model of communication where one device or process has unidirectional control over one or more other devices.

Warning!!! It is not necessary to have a Master and a Slave; simply you can send data between different devices through a communication (RS-485, RS-232, Ethernet…), the roll of any device is assigned for the programmer.

The requires to carry out that example are:

  • At list two Industrial Shields PLCs
  • SimpleComm libraries installed on your Arduino IDE. How to install SimpleComm libraries
  • Connect correctly the communication ports of RS-485 between both PLCs. See on RS-485 communication.  
  • RS-485 example communication and how to use our libraries.

Next we will proceed to explain how this example works:

We have two codes, one example for how to send data and other for how to receive them. The transmitter and receiver have an identifier/address that will be declared on the setup with the function begin(address). The transmitter PLC will be in charge to use setData() function to fill up the packet with desired data in a save way. We use this packet to not receive altered information. Once the packet is filled up, we can proceed to send the packet to other device or receiver.

In the second code, the receiver code, we will see how to read and how to extract the information that the pocket contains, what really matters. We have to know the information size (int, long, char…) that we are receiving to use the function get correctly. For more information of how to use SimpleComm library see here.

Master Code:

#include <RS485.h>
#include <SimpleComm.h>

// Create SimplePacket for sending and receiving data
SimplePacket packet;

// Define master address
uint8_t masterAddress = 0;

// Define slave address to communicate with
uint8_t slaveAddress = 1;

// Value to send as packet data
int value = 0;

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

  // Start RS485
  RS485.begin(19200L);
  RS485.setTimeout(20);

  // Start SimpleComm
  SimpleComm.begin(masterAddress);
}

///////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  static unsigned long lastSent = millis();

  // Send packet periodically: once per second
  if (millis() - lastSent >= 1000) {
    // Set request packet data
    packet.setData(value);

    // Send request to slave
    if (SimpleComm.send(RS485, packet, slaveAddress)) {
      lastSent = millis();

      Serial.print("Sent value: ");
      Serial.println(value);
    }
  }

  // Get responses
  if (SimpleComm.receive(RS485, packet)) {
    // Update value from the response
    value = packet.getInt();

    Serial.print("Received value: ");
    Serial.println(value);
  }
}

 

Slave Code:

#include <RS485.h>
#include <SimpleComm.h>

// Create SimplePacket for sending and receiving data
SimplePacket request;
SimplePacket response;

// Define slave address to communicate with
uint8_t slaveAddress = 1;

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

  // Start RS485
  RS485.begin(19200L);
  RS485.setTimeout(20);

  // Start SimpleComm
  SimpleComm.begin(slaveAddress);
}

///////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // Get requests
  if (SimpleComm.receive(RS485, request)) {
    int value = request.getInt();

    Serial.print("Received value: ");
    Serial.println(value);

    // Process value
    value++;

    // Send response to the request packet source
    response.setData(value);
    if (SimpleComm.send(RS485, response, request.getSource())) {
      Serial.print("Sent value: ");
      Serial.println(value);
    }
  }
}

See Also:

SimpleComm libraryRS-485 communicationInstaling INDUSTRIAL SHIELDS equipment to the ARDUINO IDEIndustrial Shields Library in ARDUINO IDE

Comments are closed.