Introduction
With the SimpleComm library, you can send data through any Arduino Stream: RS-485, RS-232, Ethernet… It is enough flexible to support a different kinds of communication typologies: Ad-hoc, Master-Slave, Client-Server… Using an easy to use API. The SimpleComm library is  in ​SimpleComm library.
SimpleComm usage
SimplePacket encapsulates the data into a packet and provides these functions:
setData(data)
Fill up the packet with the desired sent data or the data received from 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, const, const char, const void (the maximum data length is 128 bytes).
getBool()
getChar()
getUChar()
getInt()
getUInt()
getLong()
getLong()
getDouble()
getString()
getData(len)
Return the data contained in the packet, depending on the data type. If length is specified, it returns the data length in bytes.
SimpleComm is the interface for sending and receiving packets through the desired Arduino Stream:
begin(address)
Enable the communication system and set the device identifier/address. The address is used to identify each device. The device receives packets that are sent to it, but not to others.
send(stream, packet, destination)
send(stream, packet, destination, type)
Send a packet to a destination device, using the stream. It is also possible to define the packet type.
receive(stream, packet)
Receive a packet sent to me from another device, using the stream. It returns true if a packet is received.
Readme
#include <SimpleComm.h>
The SimpleComm module sends and receives data through any Arduino Stream: RS-485, RS-232, Ethernet... It is enough flexible to support a different kinds 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
 the 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));
Their functions above return the data contained in the packet, depending on the data type. If length is specified, the data length is returned 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)
 the 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 your 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
 the 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);
Example
In this example, we are trying to connect through an Ethernet port to a TCP Server using the SimpleComm library and different types of messages (You can see more examples on github).
/* | |
Copyright (c) 2017 Boot&Work Corp., S.L. All rights reserved | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU Lesser General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU Lesser General Public License for more details. | |
You should have received a copy of the GNU Lesser General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#if defined(MDUINO_PLUS) | |
#include <Ethernet2.h> | |
#else | |
#include <Ethernet.h> | |
#endif | |
#include <SimpleComm.h> | |
// You can define different types of messages. | |
// Here is an example: | |
#define PRINT_TEXT_TYPE 0x01 // Used for receiving text and print it | |
#define DIGITAL_OUTPUT_TYPE 0x02 // Used for setting the digital output Q0.0 | |
#define DIGITAL_INPUT_TYPE 0x03 // Used for printing the value of the digital input I0.0 | |
// Create SimplePacket for receiving data | |
SimplePacket packet; | |
// Define Ethernet values | |
byte mac[] = {0xDE, 0xAD, 0xBE, 0x75, 0x24, 0x01}; | |
// TCP server for receiving messages | |
EthernetServer server(64321); | |
// Define my SimpleComm address | |
uint8_t myAddress = 1; | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
void setup() { | |
Serial.begin(9600L); | |
// Set IOs configuration | |
pinMode(Q0_0, OUTPUT); | |
pinMode(I0_0, INPUT); | |
// Start SimpleComm | |
 SimpleComm.begin(myAddress); | |
// Start Ethernet interface using DCHP for getting an IP address | |
 Ethernet.begin(mac); | |
 Serial.print("IP address: "); | |
 Serial.println(Ethernet.localIP()); | |
// Start TCP server | |
 server.begin(); | |
} | |
//////////////////////////////////////////////////////////////////////////////////////////////////// | |
void loop() { | |
// Wait for TCP connections | |
 EthernetClient client = server.available(); | |
if (client) { | |
// Receive packets from the connected client | |
if (SimpleComm.receive(client, packet)) { | |
 Serial.print("Received packet from: "); | |
 Serial.println(packet.getSource(), HEX); | |
// Depending on the received packet type, do one of the actions below | |
switch (packet.getType()) { | |
case PRINT_TEXT_TYPE: | |
// Print the received data as a text value | |
 Serial.print("Text: "); | |
 Serial.println(packet.getString()); | |
break; | |
case DIGITAL_OUTPUT_TYPE: | |
// Set Q0.0 value depending on the received data (as a boolean value) | |
// true: HIGH, false: LOW | |
 Serial.print("Set Q0.0 to "); | |
 Serial.println(packet.getBool() ? "HIGH" : "LOW"); | |
digitalWrite(Q0_0, packet.getBool() ? HIGH : LOW); | |
break; | |
case DIGITAL_INPUT_TYPE: | |
// Print I0.0 value: HIGH or LOW | |
 Serial.print("I0.0 value: "); | |
 Serial.println(digitalRead(I0_0) == HIGH ? "HIGH" : "LOW"); | |
break; | |
default: | |
// Treat unknown packet type too | |
 Serial.print("Received unknown packet type: "); | |
 Serial.println(packet.getType()); | |
break; | |
 } | |
 } | |
 } | |
} |
SimpleComm library to send data through a PLC arduino stream