Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

simplecomm library to send data through any arduino stream


Introduction

This is Industrial Shields library for all the different Industrial Shields PLC’s .

Requirements 


Ethernet or 20 I/Os PLC:      Ethernet PLC     20 I/Os PLC


SimpleComm Industrial Shields Library:  SimpleComm Github repository


Usage


With the SimpleComm library you can send data through any Arduino Stream: RS-485, RS-232, Ethernet… It is enough flexible to support different kind of communication typologies: Ad-hoc, Master-Slave, Client-Server… Using an easy to use API. The SimpleCoom library is included on Tools40 library.


The SimpleComm module sends and receives data through any Arduino Stream: RS-485, RS-232, Ethernet... It is enough flexible to support different kind of communication typologies: Ad-hoc, Master-Slave, Client-Server, and so on, using an easy to use API.

The SimplePacket class encapsulates the data into a packet.

The setData function fills up the packet with the desired data for sending it to a remote device. It is possible to fill up the packet with different types of data: bool, char, unsigned char, int, unsigned int, long, unsigned long, double, string and even custom data types.


int num = 1234;
SimplePacket packet1;
packet1.setData(num);
SimplePacket packet2;
packet2.setData("text data");
typedef struct {
    int a;
    char b;
} customType;
customType customVar;
customVar.a = 1234;
customVar.b = 'B';
SimplePacket packet3;
packet3.setData(customVar, sizeof(customVar));

There are also getter functions, which return the data contained in the packet, depending on the data type. If length is specified, it returns the data length in bytes.

int num = packet1.getInt();
const char *str = packet2.getString();
const customType *var = (const customType*) packet3.getData();

The SimpleComm class is the interface for sending and receiving packets through the desired Arduino Stream.

The begin(byte) function enables the communication system and sets the devices identifier/address. Each device has its own address which identifies it. Devices receive packets sent to them, using their address, but not to others.

byte address = 1234;
SimpleComm.begin(address);

It is possible to send packets to a device with an address, and optionally define the packet type.

byte destination = 87;
SimpleComm.send(RS485, packet, destination);
SimpleComm.send(RS485, packet, destination, 0x33);

To send packets to all devices it is possible to send a broadcast packet with the destination field set to 0 (zero).

byte destination = 0;
SimpleComm.send(RS485, packet, destination);

The receive function receives a packet from another device, using the stream. It returns true if a packet is really received.

SimplePacket rxPacket;
if (SimpleComm.receive(RS485, rxPacket)) {
    // A packet is received
}

It is also possible to receive all the packets (sniffer mode) by setting the SimpleComm address to 0 (zero). This mode is useful when implementing a gateway node between Ethernet and RS-485.

byte address = 0;
SimpleComm.begin(address);


How to install the Tools40 library in the Arduino IDE


  1. Download the Tools40 library from the GitHub. Click on: “Clone or download”. After that download the zip file on your desktop.
  2. Add the library on the Arduino IDE. Click on: Sketch–>Include library–>Add .ZIP libraryand choose “arduino-Tools40-master.zip”. After choose the arduino-Tools40-master.zip the library has to be charged. To verify the library is added correctly you can see the library on: //File/Examples/Tools40
  3. Now your library is correctly installed and you can see an example to how to use that library on our PLCs.