Code Monkey home page Code Monkey logo

arduino-snap's Introduction

Arduino implementation of SNAP protocol ( http://www.hth.com/snap/ ).

Based on alx work ( https://github.com/alx/reprap-arduino-firmware/tree/master/library/SNAP )

Description

This implemention of SNAP protocol is compatible for :

  • master or slave node
  • HardwareSerial and SoftwareSerial
  • hardware that need a pin for controlling txMode (such as MAX485 chip). In example above set txPin to -1 if you don't need this feature.

HardwareSerial example

// create a channel based on HardwareSerial, use Serial3
SNAPChannelHardwareSerial snapChannelMaster = SNAPChannelHardwareSerial(&Serial3);
// create a SNAP object with a buffer of 16 bytes, SNAP address of this node is 1, use pin 24 as txMode pin (this pin is set to HIGH when transmitting)
SNAP<16> snapMaster = SNAP<16>(&snapChannelMaster, 1, 24);

void setup() {
  // init channel at 57600 bauds
  snapChannelMaster.begin(57600);
}

SoftwareSerial example

First of all, read SoftwareSerial limitations on https://www.arduino.cc/en/Reference/SoftwareSerial

// create a channel based on SoftwareSerial, use pin 10 as rxPin, pin 11 as txPin
SNAPChannelSoftwareSerial snapChannelSlave = SNAPChannelSoftwareSerial(10, 11);
// create a SNAP object with a buffer of 16 bytes, SNAP address of this node is 2, use pin 26 as txMode pin
SNAP<16> snapSlave = SNAP<16>(&snapChannelSlave, 2, 26);

void setup() {
  // init channel at 57600 bauds
  snapChannelSlave.begin(57600);
}

Send data

// declare a new message to destination node 2, this node have 20ms to send back an ACK
snapMaster.sendStart(2, 20);
// first byte to send
snapMaster.sendDataByte(B01010000);
// second byte to send
snapMaster.sendDataByte(B00000010);
// send as much as you want in the limit of 16 bytes (the buffer size defined earlier)
// send message now
snapMaster.sendMessage();

// wait for ACK
snapMaster.waitForAck()
// if ACK is not received in the limit on 20ms (defined earlier) or if NACK is received, message will be resend up to 2 times

Receive message

// test if new message is available
if (snapSlave.checkForPacket()) {
	// loop other all buffer size
	for (byte i = 0; i < 16; i++) {
    	// read a byte
        snapSlave.getByte(i);
    }
    // clear buffered message and signal to snapSlave that it can receive another message
    snapSlave.releaseReceive();
}

Complete example

For a complete example see examples/allInOne/main.cpp : a single arduino MEGA wich communicate with itself over its differents Serial.

arduino-snap's People

Contributors

per1234 avatar pharoz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

arduino-snap's Issues

Is this really supposed to work?!?

I can't believe this. There so much unbelievable errors inside...

  • it's not possible to include this into the Arduino IDE...
  • functions without members...
  • private declaration in the class ... using as public/friend in setup()...
  • etc. pp. ...

It's really sad. I'm searching for a good protocol for using with RS485, but this code here is not useable. Definitely not under the Arduino IDE and not in other IDE's for Arduino hardware... unfortunately...

Please delete this trash or fix it... or made a HowTo who shows how this works...

THX

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.