# @canboat/ts-pgns 中文文档

规范地址：https://npmcn.com/package/%40canboat/ts-pgns/

版本：4\.1\.0
npm 元数据更新时间：2026-11-05T11:28:31.675Z
中文译文更新时间：2026-11-09T00:48:53.146Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40canboat/ts-pgns
原文对照：https://npmcn.com/package/%40canboat/ts-pgns/?lang=original
项目主页：https://github.com/canboat/ts-pgns#readme
源码仓库：https://github.com/canboat/ts-pgns

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：6.7 MB，58 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2025-07-01T17:31:16.811Z
- 最新版本发布：2026-11-05T11:28:31.180Z
- 近 30 天下载量：27507（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：51（近 12 个月 16 个）

## 中文简介

类型脚本 Canboat PGN 定义

## 安装

```sh
npm install @canboat/ts-pgns
```

也可以使用：`pnpm add @canboat/ts-pgns`、`yarn add @canboat/ts-pgns`、`bun add @canboat/ts-pgns`

## README

# @ canboat/ ts- pgns (英语)

[![npm version](https://img.shields.io/npm/v/@canboat/ts-pgns.svg)](https://www.npmjs.com/@canboat/ts-pgns) [![Node.js CI & Test](https://github.com/canboat/ts-pgns/actions/workflows/test.yml/badge.svg)](https://github.com/canboat/ts-pgns/actions/workflows/test.yml)

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

## 目录

-   [概览](https://npmcn.com/package/%40canboat/ts-pgns/#readme-overview)
-   [特征](https://npmcn.com/package/%40canboat/ts-pgns/#readme-features)
-   [安装](https://npmcn.com/package/%40canboat/ts-pgns/#readme-installation)
-   [快速启动](https://npmcn.com/package/%40canboat/ts-pgns/#readme-quick-start)
-   [API 文档](https://npmcn.com/package/%40canboat/ts-pgns/#readme-api-documentation)
-   [用法实例](https://npmcn.com/package/%40canboat/ts-pgns/#readme-usage-examples)
-   [PGN 类](https://npmcn.com/package/%40canboat/ts-pgns/#readme-pgn-classes)
-   [公用事业](https://npmcn.com/package/%40canboat/ts-pgns/#readme-utilities)
-   [类型安全](https://npmcn.com/package/%40canboat/ts-pgns/#readme-type-safety)
-   [来源建筑](https://npmcn.com/package/%40canboat/ts-pgns/#readme-building-from-source)
-   [捐款](https://npmcn.com/package/%40canboat/ts-pgns/#readme-contributing)
-   [版本政策](https://npmcn.com/package/%40canboat/ts-pgns/#readme-version-policy)
-   [许可证](https://npmcn.com/package/%40canboat/ts-pgns/#readme-license)
-   [相关项目](https://npmcn.com/package/%40canboat/ts-pgns/#readme-related-projects)

## 概览

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

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

## 特征

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

## 安装

```bash
npm install @canboat/ts-pgns
```

## 快速启动

```typescript
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 文档](https://canboat.github.io/ts-pgns/index.html)

## 用法实例

### 使用PGN定义

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

### 使用数字

```typescript
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 场操作

```typescript
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
```

### 创建组函数信件

```typescript
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 支持 :

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

## 来源建筑

```bash
# 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
```

## 捐款

我们欢迎捐款! 请看我们的 [贡献准则](https://npmcn.com/package/%40canboat/ts-pgns/CONTRIBUTING.md) 详细情况.

### 发展 工作流量

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稳定下来,将只在主要释放中引入突破性变化.

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

## 许可证

根据Apache许可证第2.0版获得许可. 见 [自由主义](https://npmcn.com/package/%40canboat/ts-pgns/LICENSE) 详细情况.

## 相关项目

-   **[罐装船](https://github.com/canboat/canboat)** - 核心罐头
-   **[罐装js](https://github.com/canboat/canboatjs)** - 核心 Canboat JavaScript 库
-   **[信号服务器](https://github.com/SignalK/signalk-server-node)** - 信号 K 服务器执行

* * *

由Canboat项目捐助者制作

## 常见问题

### @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 和提交记录判断。

