Skip to content
This repository was archived by the owner on Dec 24, 2021. It is now read-only.

Commit f0ee369

Browse files
Joshuabaker2ArekSredzki
authored andcommitted
Adding documentation on how to use the Binary Mode with acknowledgment bytes, checkups, and SLIP encoding.
1 parent 6280f94 commit f0ee369

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

README.md

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
An NPM module which makes it easy to safely interface with your Arduino over serial.
33
It allows you to safely update it too. Great for remote mission critical deployments.
44

5-
Want to make sure that your messages are received correctly? No problem! NMEA checksums are supported natively!
5+
It has two modes: Binary mode and String mode.
6+
7+
Binary mode supports SLIP encoding, checksums, and one-way (computer-to-arduino) message confirmation checking for extremely robust communication.
8+
9+
String mode supports NMEA checking to make sure that your messages are received correctly.
610

711
## Usage
812
Get it through NPM by running: `npm install arduino-interface`
@@ -13,7 +17,7 @@ Compatible with node 6.x
1317
### Troubleshooting
1418
If your Arduino isn't being detected, you may need to add the productId to the boards.js file in [arduino-scanner](https://github.com/UBCSailbot/arduino-scanner). If this happens, please make a pull request to that repo have the boards.js updated so we can improve the module!
1519

16-
### Usage Example
20+
### Basic Usage Example
1721

1822
```node
1923
var Arduino = require('arduino-interface');
@@ -38,6 +42,47 @@ arduino.on('data', function(message) {
3842
});
3943
```
4044

45+
### Binary Mode Example
46+
```node
47+
var Arduino = require('arduino-interface');
48+
var arduino = new Arduino({
49+
baudrate: 57600,
50+
binary: true,
51+
acknowledgment: true,
52+
nmea: true, // this adds a checksum and byte stuffing with SLIP
53+
debug: true,
54+
});
55+
56+
var options = {
57+
attempts: 20, // Number of times to re-attempt sending. Default is 10.
58+
priority: 'high', // 'high' or 'low'. Message re-attempts only occur if priority 'high'
59+
timeout: 300 // Amount of time to wait for acknowledgment before resending. Default is 200 ms
60+
};
61+
62+
var data = Buffer.from("this will be a binary message");
63+
arduino.writeAndWaitForAcknowledgement(data, writeCallback, options);
64+
```
65+
66+
`writeAndWaitForAcknowledgment` will add the message to a queue of messages to send. This ensures that the messages
67+
are sent in a proper order. Only if a message succeeds to send and gets an acknowledgement, or fails
68+
to send after the amount of times specified in the "attempts" will it be dequeued.
69+
70+
The checksum is calculated by the XOR of each byte of your message, and is the very last byte of the message (before being SLIP encoded).
71+
72+
The acknowledgment byte is added automatically by this library if `acknowledgment` is true, and is the sequence number of the message in the queue.
73+
It is added to the end of the message, before the checksum (and so is included in the checksum). This byte must be sent back from the Arduino
74+
within the allotted `timeout` field in the `options` object or else the acknowledgment will have been considered a fail and the message will be resent.
75+
Note that the acknowledgment byte sent back must also be SLIP encoded and have a checksum if `nmea` is enabled.
76+
77+
We recommend using the excellent [Packet Serial](https://github.com/bakercp/PacketSerial) library for Arduino
78+
SLIP support, and adding your own checksum checker and acknowledgment sender on top of that
79+
(we may release our own implementation at a later date).
80+
81+
We recommend [Google's Protobuf](https://github.com/google/protobuf) tool to serialize your data, but any serial encoding/decoding should work.
82+
83+
When Binary Mode is enabled, all incoming messages will be run through a SLIP decoding before being surfaced by `arduino.on('data', () => {})`,
84+
so make sure that your Arduino encodes the data with SLIP before sending it back (can use the above Packet Serial for that as well).
85+
4186
## Reference Guide
4287
### Constructor
4388
Search for an Arduino and connect once it's found.
@@ -104,6 +149,16 @@ Writes to Arduino and waits for it to finish transmitting before calling the cb.
104149
If the NMEA option was set to true then this message will be sent in accordance
105150
with the NMEA protocol.
106151

152+
#### .writeAndWaitForAcknowledgement(data, writeCallback, options)
153+
Write to the arduino and waits for the acknowledgment byte to be returned. Currently only has support
154+
for Binary Mode, so the data must be a Buffer.
155+
156+
var options = {
157+
attempts: 20, // Number of times to re-attempt sending. Default is 10.
158+
priority: 'high', // 'high' or 'low'. Message re-attempts only occur if priority 'high'
159+
timeout: 300 // Amount of time to wait for acknowledgment before resending. Default is 200 ms
160+
};
161+
107162
##### message
108163
The data to send to the Arduino.
109164
##### callback(err)

index.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -498,18 +498,6 @@ Arduino.prototype.writeAndWaitForAcknowledgement = function (message, cb = () =>
498498
self._writeAndWaitForAcknowledgementHelper();
499499
};
500500

501-
/**
502-
* Write to the arduino and waits for the acknowledgment bit to be returned. Currently only has support
503-
* for binary sending.
504-
* @param message {Buffer} - Binary buffer with message to send
505-
* @param callback {Function} - Callback
506-
* @param options {Object} - Options for sending
507-
* @param options.attempts {Number} - Number of times to re-attempt sending. Default is 10.
508-
* 0 means won't send.
509-
* @param options.priority {String} - Priority: 'high' or 'low'
510-
* @param sequenceNumber {Number} - The sequence number of the message we are sending. Used internally. Do not set.
511-
* @param options.timeout {Number} - Amount of time to wait for acknowledgment before resending. Default is 200 ms
512-
*/
513501
Arduino.prototype._writeAndWaitForAcknowledgementHelper = function () {
514502
var self = this;
515503

0 commit comments

Comments
 (0)