Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 50 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@

> A node.js/javascript parser of NMEA0183 sentences. Sentences are parsed to [Signal K delta](http://signalk.org/specification/master/data_model.html#delta-format) format.


### Supported sentences
## Supported sentences

- [ALK - Seatalk](https://en.wikipedia.org/wiki/Seatalk)
- [APB - Autopilot Sentence "B"](http://www.catb.org/gpsd/NMEA.html#_apb_autopilot_sentence_b)
- [DBT - Depth Below Transducer](http://www.catb.org/gpsd/NMEA.html#_dbt_depth_below_transducer)
- [DPT - Depth of Water](http://www.catb.org/gpsd/NMEA.html#_dpt_depth_of_water)
- [DSC - Digital Selective Calling Class-D Radios](http://continuouswave.com/whaler/reference/DSC_Datagrams.html)
- [GGA - Global Positioning System Fix Data](http://www.catb.org/gpsd/NMEA.html#_gga_global_positioning_system_fix_data)
- [GLL - Geographic Position - Latitude/Longitude](http://www.catb.org/gpsd/NMEA.html#_gll_geographic_position_latitude_longitude)
- [HDG - Heading - Deviation & Variation](http://www.catb.org/gpsd/NMEA.html#_hdg_heading_deviation_amp_variation)
- [HDM - Heading - Magnetic](http://www.catb.org/gpsd/NMEA.html#_hdm_heading_magnetic)
- [HDT - Heading - True](http://www.catb.org/gpsd/NMEA.html#_hdt_heading_true)
- KEP - NKE Performance data
- [MTW - Mean Temperature of Water](http://catb.org/gpsd/NMEA.html#_mtw_mean_temperature_of_water)
- [MWV - Wind Speed and Angle](http://www.catb.org/gpsd/NMEA.html#_mwv_wind_speed_and_angle)
- [RMB - Recommended Minimum Navigation Information](http://www.catb.org/gpsd/NMEA.html#_rmb_recommended_minimum_navigation_information)
- [RMC - Recommended Minimum Navigation Information](http://www.catb.org/gpsd/NMEA.html#_rmc_recommended_minimum_navigation_information)
- [ROT - Rate of Turn](http://www.catb.org/gpsd/NMEA.html#_rot_rate_of_turn)
- [RPM - Revolutions](http://www.catb.org/gpsd/NMEA.html#_rpm_revolutions)
Expand All @@ -32,37 +35,34 @@
- [VWR - Relative Wind Speed and Angle](http://www.catb.org/gpsd/NMEA.html#_vwr_relative_wind_speed_and_angle)
- [ZDA - UTC day, month, and year, and local time zone offset](http://www.trimble.com/oem_receiverhelp/v4.44/en/NMEA-0183messages_ZDA.html)

## Usage

### Usage
### JavaScript API

```javascript
const Parser = require('@signalk/nmea0183-signalk')
const parser = new Parser()

parser.on('error', error => {
console.error(`[error] ${error.message}`)
})

parser.on('warning', warning => {
console.warn(`[warning] ${warning.message}`)
})

parser.on('signalk:delta', delta => {
console.log(`[delta] ${JSON.stringify(delta, null, 2)}`)
})

// Parse sentence
parser.parse('$SDDBT,17.0,f,5.1,M,2.8,F*3E')
try {
const delta = parser.parse('$SDDBT,17.0,f,5.1,M,2.8,F*3E')
if (delta !== null) {
console.log(`[delta] ${JSON.stringify(delta, null, 2)}`)
}
}
catch (e) {
console.error(`[error] ${e.message}`)
}
```

### Command line

In addition to usage in your code, the parser can be used on the command-line if installed globally (`npm install --global`). This allows you to pipe data from one program into the parser directly, without using a Signal K server. The parser holds no Signal K tree in memory (a big change vs. 1.x), so the output will be stringified [Signal K delta](http://signalk.org/specification/master/data_model.html#delta-format) messages.

```bash
$ echo '$SDDBT,17.0,f,5.1,M,2.8,F*3E' | nmea0183-signalk
```


### NMEA0183v4 tag blocks
## NMEA0183v4 tag blocks

This parser has (limited) support of [NMEA0183v4 tag blocks](http://www.nmea.org/Assets/may%2009%20rtcm%200183_v400.pdf) (e.g. `\s:airmar dst800,c:1438489697*13\$SDDBT,17.0,f,5.1,M,2.8,F*3E`).
Keep in mind that, since NMEA uses the backslash `\` as the start and end character of the tag block, you need to escape these characters *before* parsing them.
Expand All @@ -74,25 +74,42 @@ Example:
const Parser = require('@signalk/nmea0183-signalk')
const parser = new Parser()

parser.on('error', error => {
console.error(`[error] ${error.message}`)
})

parser.on('warning', warning => {
console.warn(`[warning] ${warning.message}`)
})

parser.on('signalk:delta', delta => {
console.log(`[delta] ${JSON.stringify(delta, null, 2)}`)
})
try {
const delta = parser.parse('\\s:airmar dst800,c:1438489697*13\\$SDDBT,17.0,f,5.1,M,2.8,F*3E')
if (delta !== null) {
console.log(`[delta] ${JSON.stringify(delta, null, 2)}`)
}
}
catch (e) {
console.error(`[error] ${e.message}`)
}
```

parser.parse('\\s:airmar dst800,c:1438489697*13\\$SDDBT,17.0,f,5.1,M,2.8,F*3E')
Output:
```json
[delta] {
"updates": [
{
"source": {
"sentence": "DBT",
"talker": "airmar dst800",
"type": "NMEA0183"
},
"timestamp": "2015-08-02T04:28:17.000Z",
"values": [
{
"path": "environment.depth.belowTransducer",
"value": 5.1
}
]
}
]
}
```

**Note:** *at this time, the checksum of the tag block (`c:1438489697*13`) is not validated.*


### License
## License

```
Copyright 2016/2017 Signal K and Fabian Tollenaar <fabian@signalk.org>.
Expand Down
18 changes: 8 additions & 10 deletions bin/nmea0183-signalk
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ process.stdin.pipe(require('split')()).on('data', data => {
return
}

data = data.trim()
parser
.parse(data)
.then(result => {
if (result != null) {
console.log(JSON.stringify(result.delta))
}
})
.catch(e => {
try {
const delta = parser.parse(data.trim())
if (delta !== null) {
console.log(JSON.stringify(delta))
}
}
catch(e) {
console.error('Encountered an error:', e.message)
})
}
})

process.stdin.on('error', err => {
Expand Down
12 changes: 4 additions & 8 deletions hooks/ALK.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

'use strict'

const utils = require('@signalk/nmea0183-utilities')
const loadSubHooks = require('../lib/loadSubhooks')
const path = require('path').join(__dirname, './seatalk')
const folderName = 'seatalk'
/*
0 1 2 3
| | | |
Expand All @@ -32,13 +28,13 @@ STALK Raymarine Seatalk1 datagram sentence
3 hex Checksum
*/

const subHooks = loadSubHooks(folderName)
const seatalkHooks = require('./seatalk')

module.exports = function(parser, input) {
module.exports = function(input) {
const { id, sentence, parts, tags } = input
const key = '0x' + parseInt(parts[0],16).toString(16).toUpperCase()
if (key in subHooks){
return require(`${path}/${key}`)(parser, input)
if (typeof seatalkHooks[key] === 'function'){
return seatalkHooks[key](input)
} else {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion hooks/APB.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where:

*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts } = input

if(parts[0].toUpperCase() == 'V') {
Expand Down
2 changes: 1 addition & 1 deletion hooks/DBT.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Field Number:
6. Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

if ((typeof parts[2] !== 'string' && typeof parts[2] !== 'number') || (typeof parts[2] === 'string' && parts[2].trim() === '')) {
Expand Down
2 changes: 1 addition & 1 deletion hooks/DPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Field Number:
2. Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

if (((typeof parts[0] !== 'string' || parts[0].trim() == '') && typeof parts[0] !== 'number') ||
Expand Down
2 changes: 1 addition & 1 deletion hooks/DSC.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function parsePosition(line) {
return { 'longitude': lon_dec, 'latitude': lat_dec }
}

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input
var values = [];

Expand Down
2 changes: 1 addition & 1 deletion hooks/GGA.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function isEmpty(mixed) {
return ((typeof mixed !== 'string' && typeof mixed !== 'number') || (typeof mixed === 'string' && mixed.trim() === ''))
}

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

const empty = parts.reduce((e, val) => {
Expand Down
2 changes: 1 addition & 1 deletion hooks/GLL.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function isEmpty(mixed) {
return ((typeof mixed !== 'string' && typeof mixed !== 'number') || (typeof mixed === 'string' && mixed.trim() === ''))
}

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

let valid = parts.reduce((v, part) => {
Expand Down
2 changes: 1 addition & 1 deletion hooks/HDG.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function isEmpty(mixed) {
return ((typeof mixed !== 'string' && typeof mixed !== 'number') || (typeof mixed === 'string' && mixed.trim() === ''))
}

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

const values = []
Expand Down
2 changes: 1 addition & 1 deletion hooks/HDM.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const utils = require('@signalk/nmea0183-utilities')
* 2. Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

if ((typeof parts[0] !== 'string' && typeof parts[0] !== 'number') || (typeof parts[0] === 'string' && parts[0].trim() === '')) {
Expand Down
2 changes: 1 addition & 1 deletion hooks/HDT.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Field Number:
2. Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

if ((typeof parts[0] !== 'string' && typeof parts[0] !== 'number') || (typeof parts[0] === 'string' && parts[0].trim() === '')) {
Expand Down
2 changes: 1 addition & 1 deletion hooks/KEP.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function isEmpty(mixed) {
)
}

module.exports = function(parser, input) {
module.exports = function(input) {
const { id, sentence, parts, tags } = input

var delta
Expand Down
2 changes: 1 addition & 1 deletion hooks/MTW.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

const delta = {
Expand Down
2 changes: 1 addition & 1 deletion hooks/MWV.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function convertToWindAngle(angle) {
return numAngle
}

module.exports = function(parser, input) {
module.exports = function(input) {
const { id, sentence, parts, tags } = input

if(!parts[4] || parts[4].toUpperCase() !== 'A') {
Expand Down
2 changes: 1 addition & 1 deletion hooks/RMB.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ values:

*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

let latitude = -1
Expand Down
2 changes: 1 addition & 1 deletion hooks/RMC.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ values:
- *6A The checksum data, always begins with *
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

let latitude = -1
Expand Down
2 changes: 1 addition & 1 deletion hooks/ROT.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const utils = require('@signalk/nmea0183-utilities')
#
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

if (String(parts[1]).toUpperCase() !== 'A') {
Expand Down
2 changes: 1 addition & 1 deletion hooks/RPM.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# astern 4) Status, A means data is valid 5) Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

const delta = {
Expand Down
4 changes: 2 additions & 2 deletions hooks/VDM.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const msgTypeToPrefix = {
24: "vessels."
}

module.exports = function (parser, input) {
module.exports = function (input, session) {
const { id, sentence, parts, tags } = input
const data = new Decoder(sentence, parser.session)
const data = new Decoder(sentence, session)
const values = []

if (data.valid === false) {
Expand Down
2 changes: 1 addition & 1 deletion hooks/VDR.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Field Number:
6 - Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

const delta = {
Expand Down
2 changes: 1 addition & 1 deletion hooks/VHW.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Field Number:
8. Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
var velocityValue
const { id, sentence, parts, tags } = input
var pathValues = []
Expand Down
2 changes: 1 addition & 1 deletion hooks/VLW.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Field Number:

*/

module.exports = function (parser, input) {
module.exports = function (input) {
var velocityValue
const { id, sentence, parts, tags } = input
var pathValues = []
Expand Down
2 changes: 1 addition & 1 deletion hooks/VPW.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Field Number:
4 - Checksum
*/

module.exports = function (parser, input) {
module.exports = function (input) {
var velocityValue
const { id, sentence, parts, tags } = input
if (parts[2]){
Expand Down
2 changes: 1 addition & 1 deletion hooks/VTG.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function isEmpty(mixed) {
return ((typeof mixed !== 'string' && typeof mixed !== 'number') || (typeof mixed === 'string' && mixed.trim() === ''))
}

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

if (parts[2] === '' && parts[0] === '' && parts[6] === '' && parts[4] === '') {
Expand Down
2 changes: 1 addition & 1 deletion hooks/VWR.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $--VWR,x.x,a,x.x,N,x.x,M,x.x,K*hh<CR><LF>
return ((typeof mixed !== 'string' && typeof mixed !== 'number') || (typeof mixed === 'string' && mixed.trim() === ''))
}

module.exports = function (parser, input) {
module.exports = function (input) {
const { id, sentence, parts, tags } = input

const empty = parts.reduce((count, part) => { count += (isEmpty(part) ? 1 : 0); return count; }, 0)
Expand Down
Loading