A comprehensive TypeScript library providing strongly-typed NMEA 2000 Parameter Group Number (PGN) definitions and utilities for marine electronics communication.
This library provides TypeScript definitions and utilities for working with NMEA 2000 Parameter Group Numbers (PGNs), which are standardized data messages used in marine electronics networks. Built on top of the Canboat project's PGN definitions, it offers type-safe access to hundreds of marine data message formats.
What are PGNs? Parameter Group Numbers are standardized message formats in the NMEA 2000 protocol that define how different types of marine data (GPS position, engine data, weather information, etc.) are structured and transmitted over the network.
npm install @canboat/ts-pgns
import { PGN_129029, PGN_127488 } from '@canboat/ts-pgns'
import { pgnToActisenseSerialFormat } from '@canboat/canboatjs'
// Create a GPS Position PGN (129029)
const gpsPosition = new PGN_129029({
latitude: 47.6062, // Seattle latitude
longitude: -122.3321, // Seattle longitude
altitude: 56.0,
gnssType: 3, // GPS
method: 1, // GNSS fix
integrity: 0, // No integrity checking
numberOfSvs: 8, // 8 satellites
hdop: 1.2, // Horizontal dilution of precision
geoidalSeparation: -34.0
})
// Create an Engine Parameters PGN (127488)
const engineData = new PGN_127488({
engineInstance: 0,
oilPressure: 275000, // 275 kPa
oilTemperature: 363.15, // 90°C in Kelvin
temperature: 353.15, // 80°C coolant temp
alternatorPotential: 14.2,
fuelRate: 15.5, // L/h
totalEngineHours: 1250.5,
coolantPressure: 95000,
fuelPressure: 280000
})
console.log(
`GPS Position: ${gpsPosition.fields.latitude}, ${gpsPosition.fields.longitude}`
)
console.log(`Engine Oil Pressure: ${engineData.fields.oilPressure} Pa`)
//Generate actisense serial formated position
const n2k = pgnToActisenseSerialFormat(gpsPosition)
console.log(`actisense posistion ${n2k}`)
import {
getPGNWithNumber,
getPGNWithId,
findMatchingDefinition
} from '@canboat/ts-pgns'
// Get PGN definition by number
const windDefs = getPGNWithNumber(130306) // Wind Data
if (windDefs) {
console.log(`Found ${windDefs.length} wind data variants`)
}
// Get specific PGN definition by ID
const gpsPosition = getPGNWithId('gnssPositionData')
if (gpsPosition) {
console.log(`PGN Description: ${gpsPosition.Description}`)
console.log(`Field count: ${gpsPosition.Fields.length}`)
}
import {
getEnumerationValue,
getEnumerationName,
getBitEnumerationValue
} from '@canboat/ts-pgns'
// Get numeric value for enumeration
const gnssType = getEnumerationValue('GNSS_TYPE', 'GPS')
console.log(`GPS type value: ${gnssType}`) // 3
// Get name from numeric value
const methodName = getEnumerationName('GNSS_METHOD', 1)
console.log(`Method: ${methodName}`) // "GNSS fix"
// Work with bit enumerations
const alertBit = getBitEnumerationValue('ENGINE_ALERT', 'Warning')
import { mapCamelCaseKeys, mapNameKeysToCamelCase } from '@canboat/ts-pgns'
// Convert PGN field IDs to camelCase names
const pgn = new PGN_129029({
/* ... */
})
const camelCasePgn = mapCamelCaseKeys(pgn)
// Now you can access fields by name instead of ID
console.log(camelCasePgn.fields.latitude) // instead of fields.latitude
console.log(camelCasePgn.fields.longitude) // instead of fields.longitude
import {
createNmeaGroupFunction,
GroupFunction,
SeatalkPilotMode16,
PGN_65379_SeatalkPilotMode
} from '@canboat/ts-pgns'
const pgn = new PGN_65379_SeatalkPilotMode({
pilotMode: SeatalkPilotMode16.TrackMode,
subMode: 0xffff
})
// Create a command group function to change this data
const requestMsg = createNmeaGroupFunction(
GroupFunction.Command,
pgn,
42 // destination address
)
The library includes type-safe classes for hundreds of NMEA 2000 PGNs. Some commonly used ones include:
PGN_129029
- GNSS Position DataPGN_129025
- Position, Rapid UpdatePGN_129026
- COG & SOG, Rapid UpdatePGN_127250
- Vessel HeadingPGN_130306
- Wind DataPGN_127488
- Engine Parameters, Rapid UpdatePGN_127489
- Engine Parameters, DynamicPGN_127505
- Fluid LevelPGN_130312
- TemperaturePGN_127508
- Battery StatusPGN_127506
- DC Detailed StatusPGN_127513
- Battery Configuration StatusPGN_130310
- Environmental ParametersPGN_130311
- Environmental ParametersPGN_130316
- Temperature, Extended RangeEach PGN class provides:
createPGN(id, fields, dst?)
- Create PGN instances by IDfindMatchingDefinition(pgn)
- Find the matching definition for a PGNgetPGNWithNumber(num)
- Get PGN definitions by numbergetPGNWithId(id)
- Get PGN definition by IDmapCamelCaseKeys(pgn)
- Convert field IDs to camelCase namesmapNameKeysToCamelCase(pgn)
- Convert field names to camelCase IDsisMatch(pgn, matchFields)
- Check if PGN matches specific field valuesgetEnumeration(name)
/ getEnumerations()
- Access enumerationsgetEnumerationValue(enumName, value)
- Get numeric value from namegetEnumerationName(enumName, value)
- Get name from numeric valuegetBitEnumeration(name)
- Access bit enumerationsgetBitEnumerationValue(enumName, value)
- Get bit value from nameconvertCamelCase(pluginApp, pgn)
- Convert keys based on server capabilityconvertNamesToCamel(pluginApp, pgn)
- Convert names based on server capabilityThis library provides comprehensive TypeScript support:
// Strongly typed fields
const gps = new PGN_129029({
latitude: 47.6062, // number
longitude: -122.3321, // number
gnssType: 3, // number (enumeration)
method: 1 // number (enumeration)
})
// TypeScript will catch field type errors
const badGps = new PGN_129029({
latitude: 'invalid', // ❌ TypeScript error: string not assignable to number
gnssType: 'GPS' // ❌ TypeScript error: string not assignable to number
})
// Proper enumeration usage
import { GnssType } from '@canboat/ts-pgns'
const correctGps = new PGN_129029({
latitude: 47.6062,
longitude: -122.3321,
gnssType: GnssType.Gps, // ✅ Type-safe enumeration
method: 1
})
# Clone the repository
git clone https://github.com/canboat/ts-pgns.git
cd ts-pgns
# Install dependencies
npm install
# Build the project
npm run build
# Generate the typescript files if you have changed canboat.json
npm run generate
npm run build
# Run tests
npm test
# Format code
npm run format
We welcome contributions! Please see our Contributing Guidelines for details.
git checkout -b feature/amazing-feature
npm test
npm run format
git commit -m 'Add amazing feature'
git push origin feature/amazing-feature
Important Note: This library is currently in active development. Minor releases may include breaking changes. Please pin to specific minor versions (e.g., 1.10.x
) in your dependencies.
Once the API stabilizes, breaking changes will only be introduced in major releases.
{
"dependencies": {
"@canboat/ts-pgns": "~1.10.0" // Pin to minor version
}
}
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Made with ⚓ by the Canboat project contributors