Compare commits

..

12 Commits

Author SHA1 Message Date
96186e98be buffersize updates 2021-05-05 23:00:55 +02:00
929f4042e0 updated readme 2021-05-01 20:24:28 +02:00
ae2a092fab updated protocol to use better ascii chars 2021-05-01 20:19:13 +02:00
0f26c86e0c removed serial checks 2021-04-14 14:38:24 +02:00
80396408f9 fixed packed 2021-04-14 13:19:16 +02:00
65061d53b1 updated example and readme 2021-04-13 14:32:42 +02:00
d7d2ff4248 updated api 2021-04-13 14:31:19 +02:00
6b26acf538 updated packet size 2021-04-04 00:07:00 +02:00
057cce2096 fixes! 2021-04-04 00:01:06 +02:00
69f48d65cd updated version 2021-03-30 23:29:23 +02:00
cfa104386b Merge branch 'master' of https://git.aterve.com/Frozenverse/SimpleSerialProtocol 2021-03-30 23:28:32 +02:00
5cf94a3079 arduino fix 2021-03-30 23:28:28 +02:00
6 changed files with 61 additions and 93 deletions

View File

@@ -38,25 +38,21 @@ SimpleSerialProtocol has a very simple API so it should be very easy to implemen
The protocol works as following, each packet has 3 seperate stages. The protocol works as following, each packet has 3 seperate stages.
### ID ### ID
This could be the device or service you want to reach. ID of packet (device)
### Command ### Type
This is the command you want to send to the device or service. Type of packet (action)
### Data ### Data
This is the data you want to send to the device or service. Data of packet (data)
Of course you could change the use of any of these values to fit your needs. Of course you could change the use of any of these values to fit your needs.
A complete packet would look like this: A complete packet would look like this:
``` ```
<ID_HERE:COMMAND_HERE:DATA_HERE> ID - TYPE - DATA
``` ```
As you can see the packet starts and ends with ``< - >`` characters and the stages are seperated with `` : `` characters.
`This means you can't use any of these characters as data in the packets!!`
Include SSP and define a new device. Include SSP and define a new device.
```c++ ```c++
#include <SimpleSerialProtocol.h> #include <SimpleSerialProtocol.h>
@@ -66,12 +62,12 @@ SimpleSerialProtocol device;
``` ```
Initialize the serial device and connection. Initialize the serial device and connection.
First parameter is the serial channel. For example; the mega 2560 has 4 serial channels (RX0/TX0 through RX3/TX3) First parameter is a serial object. For example; the mega 2560 has 4 serial channels (RX0/TX0 through RX3/TX3) so Serial, Serial1, Serial2 ... etc.
Second parameter is the baud rate. Second parameter is the baud rate.
```c++ ```c++
void setup() { void setup() {
// Initialize serial device on index 0 with a baud rate of 115200 // Initialize serial device on index 0 with a baud rate of 115200
device.init(0, 115200); device.init(Serial, 115200);
} }
``` ```
@@ -80,7 +76,7 @@ Now create a method (with a packet parameter) which will be called when a packet
void onPacket(Packet packet) { void onPacket(Packet packet) {
Serial.println("Received Packet!"); Serial.println("Received Packet!");
Serial.println(packet.id); Serial.println(packet.id);
Serial.println(packet.command); Serial.println(packet.type);
Serial.println(packet.data); Serial.println(packet.data);
Serial.println("----------------"); Serial.println("----------------");
} }
@@ -92,7 +88,7 @@ void loop() {
``` ```
To send packets you can use the ``sendPacket`` method. To send packets you can use the ``sendPacket`` method.
```c++ ```c++
deviceOne.sendPacket("IDPacket", "Commandhere", "testtest"); deviceOne.sendPacket("IDPacket", "Typehere", "testtest");
``` ```
## Contributing ## Contributing
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change. Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

View File

@@ -8,25 +8,22 @@ SimpleSerialProtocol deviceOne;
void onPacket(Packet packet) { void onPacket(Packet packet) {
Serial.println("Received Packet!"); Serial.println("Received Packet!");
Serial.println(packet.id); Serial.println(packet.id);
Serial.println(packet.command); Serial.println(packet.type);
Serial.println(packet.data); Serial.println(packet.data);
Serial.println("----------------"); Serial.println("----------------");
// To send a packet // To send a packet
deviceOne.sendPacket("IDPacket", "Commandhere", "testtest"); deviceOne.sendPacket("IDPacket", "TypeHere", "testtest");
} }
void setup() { void setup() {
// Initialize (deviceOne) serial device on index 0 with a baud rate of 115200. // Initialize (deviceOne) serial device on index 0 with a baud rate of 115200.
deviceOne.init(0, 115200); deviceOne.init(Serial, 115200);
} }
void loop() { void loop() {
// Call receive packets function (looping) to handle incoming serial packets. // Call receive packets function (looping) to handle incoming serial packets.
// If a complete packet has been received it will call the callback function (onPacket). // If a complete packet has been received it will call the callback function (onPacket).
deviceOne.receivePackets(onPacket); deviceOne.receivePackets(onPacket);
// Small delay to not over overwork CPU.
delay(1);
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "SimpleSerialProtocol", "name": "SimpleSerialProtocol",
"version": "1.1.0", "version": "1.9.0",
"description": "Easy-to-use serial protocol system for arduino type devices.", "description": "Easy-to-use serial protocol system for arduino type devices.",
"keywords": "Serial, Connectivity, USB, Simple", "keywords": "Serial, Connectivity, USB, Simple",
"repository": "repository":

View File

@@ -21,6 +21,6 @@
struct Packet struct Packet
{ {
char * id; char * id;
char * command; char * type;
char * data; char * data;
}; };

View File

@@ -18,18 +18,14 @@
* *
*/ */
// Base Arduino Library
#include <Arduino.h> #include <Arduino.h>
class SerialConnection class SerialConnection
{ {
private: private:
byte serialBufferSize = 300; int serialTransmitBufferSize = 960;
char serialBuffer[300]; int serialBufferSize = 1024;
char serialData[300] = {0}; char serialBuffer[1024];
const char packetStartMarker = '<';
const char packetEndMarker = '>';
// Receiving a new packet // Receiving a new packet
boolean newSerialData = false; boolean newSerialData = false;
@@ -44,34 +40,21 @@ class SerialConnection
Stream *serialRefrence; Stream *serialRefrence;
public: public:
void init(int serialIndex, unsigned long serialBaud); const char packetStartMarker = 180;
void send(char * data); const char packetMidMarker = 196;
const char packetEndMarker = 195;
void init(HardwareSerial &serialHandle, unsigned long serialBaud);
void send(char* data);
char* handle(); char* handle();
char* getPacketMarker(int type); int getTransmitBufferSize();
int getBufferSize();
}; };
void SerialConnection::init(int serialIndex, unsigned long serialBaud) { void SerialConnection::init(HardwareSerial &serialHandle, unsigned long serialBaud) {
if (serialIndex == 0) { // serialBuffer[serialBufferSize] = {0};
Serial.begin(serialBaud); serialRefrence = &serialHandle;
serialRefrence = &Serial; serialHandle.begin(serialBaud);
}
else if (serialIndex == 1) {
Serial1.begin(serialBaud);
serialRefrence = &Serial1;
}
else if (serialIndex == 2) {
Serial2.begin(serialBaud);
serialRefrence = &Serial2;
}
else if (serialIndex == 3) {
Serial3.begin(serialBaud);
serialRefrence = &Serial3;
}
}; };
char* SerialConnection::handle() { char* SerialConnection::handle() {
@@ -108,18 +91,10 @@ char* SerialConnection::handle() {
return NULL; return NULL;
}; };
void SerialConnection::send(char * data) { void SerialConnection::send(char* data) {
serialRefrence->println(data); serialRefrence->println(data);
}; };
char* SerialConnection::getPacketMarker(int type) { int SerialConnection::getTransmitBufferSize() {
if (type == 0) { return (int)serialTransmitBufferSize;
return "<";
} else {
return ">";
}
};
int SerialConnection::getBufferSize() {
return (int)serialBufferSize;
}; };

View File

@@ -18,7 +18,6 @@
* *
*/ */
// Base Arduino Library
#include <Arduino.h> #include <Arduino.h>
#include "Packet.h" #include "Packet.h"
@@ -29,23 +28,13 @@ class SimpleSerialProtocol
private: private:
SerialConnection serialConnection; SerialConnection serialConnection;
public: public:
bool init(int deviceIndex, unsigned long baud); bool init(HardwareSerial &serialHandle, unsigned long baud);
bool receivePackets(void (*callback)(Packet)); bool receivePackets(void (*callback)(Packet));
bool sendPacket(char* ID, char* command, char* data); bool sendPacket(char* ID, char* command, char* data);
}; };
bool SimpleSerialProtocol::init(int deviceIndex, unsigned long baud) { bool SimpleSerialProtocol::init(HardwareSerial &serialHandle, unsigned long baud) {
// Check if device index is in range serialConnection.init(serialHandle, baud);
if (deviceIndex < 0 || deviceIndex > 4) {
return false;
}
// Init serial connection
serialConnection.init(deviceIndex, baud);
// Serial Connection Delay
delay(5);
return true; return true;
}; };
@@ -55,30 +44,32 @@ bool SimpleSerialProtocol::receivePackets(void (*callback)(Packet)) {
// Check if received a complete buffer // Check if received a complete buffer
if (receivedBuffer != NULL) { if (receivedBuffer != NULL) {
int bufferSize = serialConnection.getBufferSize();
// Buffer pointer // Buffer pointer
char* strtokIndex; char* strtokIndex;
char id[bufferSize / 2];
char command[bufferSize / 2]; // Marker
char data[bufferSize / 2]; char marker[1] = "";
marker[0] = serialConnection.packetMidMarker;
// Get ID // Get ID
strtokIndex = strtok(receivedBuffer, ":"); strtokIndex = strtok(receivedBuffer, marker);
char id[strlen(strtokIndex)];
strcpy(id, strtokIndex); strcpy(id, strtokIndex);
// Get Command // Get Type
strtokIndex = strtok(NULL, ":"); strtokIndex = strtok(NULL, marker);
strcpy(command, strtokIndex); char type[strlen(strtokIndex)];
strcpy(type, strtokIndex);
// Get Data // Get Data
strtokIndex = strtok(NULL, ":"); strtokIndex = strtok(NULL, marker);
char data[strlen(strtokIndex)];
strcpy(data, strtokIndex); strcpy(data, strtokIndex);
// Pack received data into packet // Pack received data into packet
Packet packet; Packet packet;
packet.id = id; packet.id = id;
packet.command = command; packet.type = type;
packet.data = data; packet.data = data;
// Call pointer callback function // Call pointer callback function
@@ -92,14 +83,23 @@ bool SimpleSerialProtocol::receivePackets(void (*callback)(Packet)) {
return false; return false;
}; };
bool SimpleSerialProtocol::sendPacket(char* ID, char* command, char* data) { bool SimpleSerialProtocol::sendPacket(char* id, char* type, char* data) {
// Create new packet buffer // Create new packet buffer
char serialPacketBuffer[serialConnection.getBufferSize()]; char serialPacketBuffer[serialConnection.getTransmitBufferSize()];
// Pack data in buffer // Pack data in buffer
sprintf(serialPacketBuffer, "%s%s:%s:%s%s", serialConnection.getPacketMarker(0), ID, command, data, serialConnection.getPacketMarker(1)); sprintf(serialPacketBuffer, "%c%s%c%s%c%s%c",
serialConnection.packetStartMarker,
id,
serialConnection.packetMidMarker,
type,
serialConnection.packetMidMarker,
data,
serialConnection.packetEndMarker
);
// Send buffer to serial device // Send buffer to serial device
serialConnection.send(serialPacketBuffer); serialConnection.send(serialPacketBuffer);
return true; return true;
}; };