Giter VIP home page Giter VIP logo

slay2's Introduction

slay2

Abstract

slay2 is a serial communication protocol driver on ISO/OSI layer 2 implemented in C++. It implements reliable, full duplex, stream based communication on top of a byte orientated interface like UART. Data is transfered in frames of up to 256 payload bytes, secured by a 8-bit sequence counter and a 32-bit CRC. Successfully transfered frames are acknowledged by the receiver. Non-acknowledged frames will be automatically retransmitted. slay2 offers up to 256 logical communication channels. The lower the channel number, the higher the transfer priority.

Nullmodem

Concept

The protocol driver is realized as virtual C++ class Slay2. For target adaption, user must derive from this class and implement the virtual methods:

class Slay2
{
   ...
   virtual unsigned int getTime1ms(void) = 0;
   virtual unsigned int getTxCount(void) = 0;
   virtual int transmit(const unsigned char * data, unsigned int len) = 0;
   virtual int receive(unsigned char * buffer, unsigned int size) = 0;

Slay2 serves as factory class to open (create) / close (destroy) communication channels of type Slay2Channel:

   Slay2Channel * open(const unsigned int channel);
   void close(Slay2Channel * const channel);

The actual communication takes place by means of Slay2Channel, using its send method and receive callback:

class Slay2Channel
{
   ...
   int send(const unsigned char * data, const unsigned int len, const bool more=false);
   void setReceiver(const Slay2Receiver receiver, void * const obj=NULL);

Application Example

//main.cpp
#include "slay2.h"
#include "slay2_linux.h"            //Slay2Linux is a target adaption of slay2 for Linux

...

static Slay2Linux slay2;            //Slay2Linux is a child of Slay2 (e.g. Slay2Linux : public Slay2 ...)
static Slay2Channel * slay2ch[3];   //3 logical slay2 communication channels

...

static void onSerialReceive(void * const obj, const unsigned char * const data, const unsigned int len)
{
   Slay2Channel * thiz = (Slay2Channel *)obj; //e.g. you can use "obj" to reference an object instance
   ...
}

static void anotherOSerialReceive(void * const obj, const unsigned char * const data, const unsigned int len)
{
   ...
}

...

int main(int argc, char * argv[])
{
   //optional - if the target adaption requires an initialization....
   slay2.init("/dev/ttyUSB0", 115200);      //this is a Slay2Linux specific method to initialize the tty device

   //open the communication channel 0, and set receive callback (used by channel 0 and 1, with parameter)
   slay2ch[0] = slay2.open(0);
   slay2ch[0]->setReceiver(&onSerialReceive, (void *)slay2ch[0]);

   //open the communication channel 1, and set receive callback (used by channel 0 and 1, with parameter)
   slay2ch[1] = slay2.open(1);
   slay2ch[1]->setReceiver(&onSerialReceive, (void *)slay2ch[1]);

   //open the communication channel 2, and set its own receive callback
   slay2ch[2] = slay2.open(2);
   slay2ch[2]->setReceiver(&anotherOnSerialReceive); //obj will be NULL, because of default parameter


   //enter application loop
   while (true)
   {
      /*
         application logic to send data using
          slay2[...]->send(x, y);
         and receive data using
          the respective callback function
      */

      //process communication
      slay2.task(); //this method must be called cyclically
   }

   ...

Driver Files

Some details of the project structure.

Base

  • slay2.cpp/.h
  • slay2_buffer.cpp/.h
  • slay2_scheduler.cpp./h

Target Adaptions

  • slay2_nullmodem.cpp/.h (this is an dummy target implementation interconnecting TX an RX (like a nullmode cable does))
  • slay2_linux.cpp/.h (target implementation for linux)

Test and Demo

  • slay2_buffer_test.cpp (this is a separate "main" that only tests the buffer implementation)
  • main.cpp (this is a demo application using the nullmodem target)

Usage

To use slay2 add the base files to your project. Also add one of the provided targed adaptions or implement your own.

Build demo

Build process of the demo application is based on CMake. On a linux computer follow this procedure:

  1. Checkout this project and cd to it (e.g. cd slay2)
  2. Create a build directory (e.g. mkdir build)
  3. Enter the build directory (e.g. cd build)
  4. Within the build directory execute cmake ..
  5. Within the build directory execute make
  6. Execute the demo usin ./slay2

slay2's People

Contributors

minlux avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

slay2's Issues

Make fails on linux

/slay2/build$ make
[  4%] Building CXX object CMakeFiles/slay2_win32_test.dir/test/slay2_win32_test.cpp.o
/slay2/test/slay2_win32_test.cpp:1:10: fatal error: windows.h: No such file or directory
    1 | #include <windows.h>
      |          ^~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/slay2_win32_test.dir/build.make:82: CMakeFiles/slay2_win32_test.dir/test/slay2_win32_test.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:101: CMakeFiles/slay2_win32_test.dir/all] Error 2

Commenting/deleting the add_executable(slay2_win32_test part in the root CMakeLists.txt fixes this.

for example:

cmake_minimum_required (VERSION 2.6)
project(slay2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11")

include_directories(src test)

add_executable(slay2_test
   test/main.cpp
   src/crc32.c
   src/slay2_buffer.cpp
   src/slay2_scheduler.cpp
   src/slay2.cpp
   src/slay2_nullmodem.cpp
)

add_executable(slay2_buffer_test
   test/slay2_buffer_test.cpp
   src/crc32.c
   src/slay2_buffer.cpp
)


# Platform-specific executables
if(UNIX AND NOT APPLE)  # Linux
    add_executable(slay2_linux_test
       test/slay2_linux_test.cpp
       src/crc32.c
       src/slay2_buffer.cpp
       src/slay2_scheduler.cpp
       src/slay2.cpp
       src/slay2_linux.cpp
    )
    target_link_libraries(slay2_linux_test pthread)
endif()

if(WIN32)  # Windows
    add_executable(slay2_win32_test
       test/slay2_win32_test.cpp
       src/crc32.c
       src/slay2_buffer.cpp
       src/slay2_scheduler.cpp
       src/slay2.cpp
       src/slay2_win32.cpp  # Assuming this should be slay2_win32.cpp instead of slay2_linux.cpp
    )
endif()

If you want, I can make a PR out of this

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.