updated protocol to use better ascii chars
This commit is contained in:
parent
0f26c86e0c
commit
ae2a092fab
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "SimpleSerialProtocol",
|
"name": "SimpleSerialProtocol",
|
||||||
"version": "1.7.0",
|
"version": "1.8.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":
|
||||||
|
@ -21,6 +21,6 @@
|
|||||||
struct Packet
|
struct Packet
|
||||||
{
|
{
|
||||||
char * id;
|
char * id;
|
||||||
char * command;
|
char * type;
|
||||||
char * data;
|
char * data;
|
||||||
};
|
};
|
||||||
|
@ -18,15 +18,13 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
class SerialConnection
|
class SerialConnection
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
int serialBufferSize = 512;
|
int serialBufferSize = 265;
|
||||||
char serialBuffer[512];
|
char serialBuffer[265];
|
||||||
char serialData[512] = {0};
|
|
||||||
|
|
||||||
const char packetStartMarker = '<';
|
|
||||||
const char packetEndMarker = '>';
|
|
||||||
|
|
||||||
// Receiving a new packet
|
// Receiving a new packet
|
||||||
boolean newSerialData = false;
|
boolean newSerialData = false;
|
||||||
@ -41,34 +39,21 @@ class SerialConnection
|
|||||||
Stream *serialRefrence;
|
Stream *serialRefrence;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void init(int serialIndex, unsigned long serialBaud);
|
const char packetStartMarker = 180;
|
||||||
|
const char packetMidMarker = 196;
|
||||||
|
const char packetEndMarker = 195;
|
||||||
|
|
||||||
|
void init(HardwareSerial &serialHandle, unsigned long serialBaud);
|
||||||
void send(char* data);
|
void send(char* data);
|
||||||
char* handle();
|
char* handle();
|
||||||
char* getPacketMarker(int type);
|
|
||||||
int getBufferSize();
|
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() {
|
||||||
@ -109,14 +94,6 @@ void SerialConnection::send(char * data) {
|
|||||||
serialRefrence->println(data);
|
serialRefrence->println(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
char* SerialConnection::getPacketMarker(int type) {
|
|
||||||
if (type == 0) {
|
|
||||||
return "<";
|
|
||||||
} else {
|
|
||||||
return ">";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int SerialConnection::getBufferSize() {
|
int SerialConnection::getBufferSize() {
|
||||||
return (int)serialBufferSize;
|
return (int)serialBufferSize;
|
||||||
};
|
};
|
@ -18,6 +18,8 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include "Packet.h"
|
#include "Packet.h"
|
||||||
#include "SerialConnection.h"
|
#include "SerialConnection.h"
|
||||||
|
|
||||||
@ -26,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;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -52,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
|
||||||
@ -89,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.getBufferSize()];
|
||||||
|
|
||||||
// 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;
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user