@canboat/ts-pgns

中文文档
4.1.0发布于 2026/11/05Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 2.8万

类型脚本 Canboat PGN 定义

中文使用文档
文档目录28 节
  1. 目录
  2. 概览
  3. 特征
  4. 安装
  5. 快速启动
  6. API 文档
  7. 用法实例
  8. 使用PGN定义
  9. 使用数字
  10. PGN 场操作
  11. 创建组函数信件
  12. PGN 类
  13. 导航定位( P)
  14. 引擎推进( P)
  15. 电气系统
  16. 环境
  17. 公用事业
  18. 核心公用事业
  19. 外地转换
  20. 编号帮助
  21. 信号K 服务器集成
  22. 类型安全
  23. 来源建筑
  24. 捐款
  25. 发展 工作流量
  26. 版本政策
  27. 许可证
  28. 相关项目

@ canboat/ ts- pgns (英语)

npm version Node.js CI & Test

一个综合的TypeScript库,为海洋电子通讯提供强型NMEA 2000参数组号(PGN)定义和公用事业.

目录

概览

该库提供TypeScript定义和公用事业,用于与NMEA 2000参数组编号(PGNs)合作,这是海洋电子网络中使用的标准化数据电文. 它建于Canboat项目PGN定义之上,提供数百个海洋数据电文格式的型号安全访问.

什么是PGNs吗? 参数组号是NMEA 2000协议中的标准化消息格式,定义不同类型海洋数据(GPS位置,引擎数据,气象信息等)如何在网络中构建和传输.

特征

  • QQ 类型安全: 有 TypeScript 接口的完整打入的 PGN 类
  • 全面覆盖: 支持数百个标准 NMEA 2000 PGN
  • 功用函数: PGN 操作和转换的辅助功能
  • QQ 编号支持: NMEA 2000 计数和比特字段的内置支持
  • 实地验证: 类型安全、有适当验证的外地访问
  • QQ 记录良好: 综合联合来文文件文件和实例
  • * 转换公用事业: 在不同的 PGN 表达式中转换
  • 性能优化: 高效查找和创建 PGN 对象

安装

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}`)

API 文档

完成 API 文档

用法实例

使用PGN定义

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')

PGN 场操作

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
)

PGN 类

该图书馆包括数百个NMEA 2000 PGN的型安全课. 一些常用的包括:

导航定位( P)

  • PGN_129029 - 全球导航卫星系统位置数据
  • PGN_129025 - 位置,快速更新
  • PGN_129026 - COG & SOG, 快速更新
  • PGN_127250 - 船行驶
  • PGN_130306 - 风能数据

引擎推进( P)

  • PGN_127488 - 引擎参数,快速更新
  • PGN_127489 - 动力引擎参数
  • PGN_127505 - 流体水平
  • PGN_130312 - 温度

电气系统

  • PGN_127508 - 电池状况
  • PGN_127506 - DC详细状况
  • PGN_127513 - 电池配置状态

环境

  • PGN_130310 - 环境参数
  • PGN_130311 - 环境参数
  • PGN_130316 - 温度,扩展范围

每个PGN类提供:

  • 类型安全实地访问
  • 合适的类型脚本接口
  • 内建验证
  • 获取基本的PGN定义

公用事业

核心公用事业

  • createPGN(id, fields, dst?) - 通过 ID 创建 PGN 实例
  • findMatchingDefinition(pgn) - 找到PGN的匹配定义
  • getPGNWithNumber(num) - 按数字获得PGN定义
  • getPGNWithId(id) - 通过身份识别获得PGN定义

外地转换

  • mapCamelCaseKeys(pgn) - 将字段ID转换为骆驼Case名称
  • mapNameKeysToCamelCase(pgn) - 将字段名称转换为骆驼Case ID
  • isMatch(pgn, matchFields) - 检查 PGN 是否匹配特定的字段值

编号帮助

  • getEnumeration(name) 编号 : getEnumerations() - 查阅查点
  • getEnumerationValue(enumName, value) - 从名称中获取数字值
  • getEnumerationName(enumName, value) - 从数值中获取名称
  • getBitEnumeration(name) - 访问比特点数
  • getBitEnumerationValue(enumName, value) - 从名称中获取比特值

信号K 服务器集成

  • convertCamelCase(pluginApp, pgn) - 根据服务器能力转换密钥
  • convertNamesToCamel(pluginApp, pgn) - 根据服务器能力转换名称

类型安全

此库提供了全面的 TypeScript 支持 :

// 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

捐款

我们欢迎捐款! 请看我们的 贡献准则 详细情况.

发展 工作流量

  1. 叉子存储器
  2. 创建特性分支 : git checkout -b feature/amazing-feature
  3. 做你的改变
  4. 运行测试 : npm test
  5. 运行更漂亮和提示 : npm run format
  6. 提交更改 : git commit -m 'Add amazing feature'
  7. 推到树枝上: git push origin feature/amazing-feature
  8. 打开拉请求

版本政策

重要说明: 该图书馆目前正在积极发展。 微量释放可能包括破解更改. 请钉入特定的次要版本(例如, 1.10.x在你的依赖.

一旦API稳定下来,将只在主要释放中引入突破性变化.

{
  "dependencies": {
    "@canboat/ts-pgns": "~1.10.0" // Pin to minor version
  }
}

许可证

根据Apache许可证第2.0版获得许可. 见 自由主义 详细情况.


由Canboat项目捐助者制作

版本与发布记录

@canboat/ts-pgns 共发布 51 个版本,首次发布于 2025/07/02;过去 12 个月发布了 16 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.1.0latest2026/11/053 天前
4.0.02026/11/054 天前
3.1.02026/10/2911 天前
3.0.02026/10/2020 天前
2.0.02026/10/021 个月前
1.11.182026/05/315 个月前

其他发布通道:beta → 1.11.16-beta.1。

关于 @canboat/ts-pgns 的常见问题

@canboat/ts-pgns 是什么?

类型脚本 Canboat PGN 定义

如何安装 @canboat/ts-pgns?

在项目目录运行 npm install @canboat/ts-pgns,或使用 pnpm add / yarn add / bun add。

@canboat/ts-pgns 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@canboat/ts-pgns 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@canboat/ts-pgns 可以商用吗?

@canboat/ts-pgns 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@canboat/ts-pgns 的下载量有多少?

根据 npm 官方下载统计,@canboat/ts-pgns 在 2026/10/09 – 2026/11/07 这 30 天内被下载 27,507 次,日均约 917 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@canboat/ts-pgns 还在维护吗?

最新版本 4.1.0 发布于 2026/11/05(3 天前);至今共发布 51 个版本,其中过去 12 个月发布了 16 个;首次发布于 2025/07/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。