@pamoja/can 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-MIT +21 -0
- package/README.md +108 -0
- package/package.json +39 -0
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Anthony Wiedman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# @pamoja/can
|
|
2
|
+
|
|
3
|
+
CAN 2.0 and CAN-FD frames with 11- and 29-bit identifiers, plus J1939 decode and compose. One capability of [pamoja](https://github.com/molexxxx/pamoja), one memory-safe Rust core with bindings for TypeScript, Python, and C#.
|
|
4
|
+
|
|
5
|
+
[](https://pamoja.molex.cloud/docs/reference/node/modules/_pamoja_can.html)
|
|
6
|
+
[](https://pamoja.molex.cloud/docs/guides/can.html)
|
|
7
|
+
[](https://pamoja.molex.cloud/docs/)
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @pamoja/can
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This pulls in `@pamoja/native`, the compiled engine. `npm install pamoja` is the whole framework in one package.
|
|
16
|
+
|
|
17
|
+
## Example
|
|
18
|
+
|
|
19
|
+
The test that runs in CI, spliced here as it ran.
|
|
20
|
+
|
|
21
|
+
From [`bindings/node/guides/can.ts`](https://github.com/molexxxx/pamoja/blob/main/bindings/node/guides/can.ts):
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import {
|
|
25
|
+
NOT_AVAILABLE,
|
|
26
|
+
broadcastJ1939,
|
|
27
|
+
composeJ1939,
|
|
28
|
+
decodeJ1939,
|
|
29
|
+
fdFrame,
|
|
30
|
+
frame,
|
|
31
|
+
priority,
|
|
32
|
+
signals,
|
|
33
|
+
signalsFrom,
|
|
34
|
+
} from '@pamoja/can'
|
|
35
|
+
|
|
36
|
+
// The nodes on this bus, by the address each answers to, and the two parameter groups
|
|
37
|
+
// in play. J1939 publishes both, so naming them is what makes the traffic readable.
|
|
38
|
+
const ENGINE = 0
|
|
39
|
+
const GATEWAY = 1
|
|
40
|
+
const GEARBOX = 33
|
|
41
|
+
const ENGINE_CONTROLLER_1 = 61_444 // carries engine speed
|
|
42
|
+
const REQUEST = 59_904 // asks another node for a parameter group
|
|
43
|
+
|
|
44
|
+
// Where engine speed sits inside that group, and the scale the standard fixes for it.
|
|
45
|
+
// Naming both is what stops a sender and a receiver disagreeing about either.
|
|
46
|
+
const ENGINE_SPEED_AT = 3
|
|
47
|
+
const RPM_PER_BIT = 0.125
|
|
48
|
+
|
|
49
|
+
// J1939 keeps its addressing inside the CAN identifier: a priority, the parameter
|
|
50
|
+
// group, and the address of whatever sent it. A broadcast has no destination, so it is
|
|
51
|
+
// its own constructor rather than a magic address a caller has to know.
|
|
52
|
+
const speedId = broadcastJ1939(priority.control, ENGINE_CONTROLLER_1, ENGINE)
|
|
53
|
+
const speed = decodeJ1939(speedId)!
|
|
54
|
+
console.log(`broadcast pgn ${speed.pgn} at priority ${speed.priority}`)
|
|
55
|
+
|
|
56
|
+
// A parameter group below the PDU1 limit is addressed rather than broadcast, so those
|
|
57
|
+
// eight identifier bits carry a destination instead of extending the group number.
|
|
58
|
+
const requestId = composeJ1939(priority.default, REQUEST, GATEWAY, GEARBOX)
|
|
59
|
+
console.log(`request pgn ${decodeJ1939(requestId)!.pgn} addressed to node ${GEARBOX}`)
|
|
60
|
+
|
|
61
|
+
// Reading one back off the bus is the same thing in reverse, so a receiver never
|
|
62
|
+
// unpacks 29 bits by hand.
|
|
63
|
+
const heard = decodeJ1939(requestId)!
|
|
64
|
+
console.log(`heard from node ${heard.source} for node ${heard.destination}`)
|
|
65
|
+
|
|
66
|
+
// The payload. Every signal starts marked not available, and this controller reports
|
|
67
|
+
// only engine speed, so that is the only one it writes.
|
|
68
|
+
const reported = signals()
|
|
69
|
+
reported.setU16(ENGINE_SPEED_AT, 1000 / RPM_PER_BIT)
|
|
70
|
+
const eec1 = frame(speedId, reported.bytes, true)
|
|
71
|
+
|
|
72
|
+
// The receiving node reads the same offset back, so neither end slices the payload.
|
|
73
|
+
const rpm = signalsFrom(eec1.data).u16(ENGINE_SPEED_AT)! * RPM_PER_BIT
|
|
74
|
+
console.log(`engine ${rpm} rpm, carried in ${eec1.dlc} bytes`)
|
|
75
|
+
|
|
76
|
+
// Above eight bytes CAN-FD encodes the length in steps rather than exactly, and a
|
|
77
|
+
// classic frame still refuses a ninth byte.
|
|
78
|
+
console.log(`32 bytes carries length code ${fdFrame(speedId, new Uint8Array(32), true).dlc}`)
|
|
79
|
+
try {
|
|
80
|
+
frame(speedId, new Uint8Array(9), true)
|
|
81
|
+
console.log('a classic frame took nine bytes, which should never happen')
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.log(`classic refused nine bytes: ${(error as Error).message}`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// J1939 never rides an 11-bit identifier, so a standard frame is not one of its
|
|
87
|
+
// messages however its bits happen to line up.
|
|
88
|
+
console.log(`an 11-bit identifier is J1939: ${decodeJ1939(291, false) !== null}`)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## The same capability in every language
|
|
92
|
+
|
|
93
|
+
| Language | Package | Reference |
|
|
94
|
+
| --- | --- | --- |
|
|
95
|
+
| Rust | [`pamoja-can`](https://crates.io/crates/pamoja-can) | [reference](https://pamoja.molex.cloud/docs/reference/rust/pamoja_can/index.html), [docs.rs](https://docs.rs/pamoja-can) |
|
|
96
|
+
| TypeScript | [`@pamoja/can`](https://www.npmjs.com/package/@pamoja/can) | [reference](https://pamoja.molex.cloud/docs/reference/node/modules/_pamoja_can.html) |
|
|
97
|
+
| Python | [`pamoja-can`](https://pypi.org/project/pamoja-can/) | [reference](https://pamoja.molex.cloud/docs/reference/python/pamoja/can.html) |
|
|
98
|
+
| C# | [`Pamoja.Can`](https://www.nuget.org/packages/Pamoja.Can) | [reference](https://pamoja.molex.cloud/docs/reference/dotnet/api/Pamoja.Can.html) |
|
|
99
|
+
|
|
100
|
+
## Documentation
|
|
101
|
+
|
|
102
|
+
- [`@pamoja/can` reference](https://pamoja.molex.cloud/docs/reference/node/modules/_pamoja_can.html), every class, function, and type this package exports.
|
|
103
|
+
- [The CAN and J1939 guide](https://pamoja.molex.cloud/docs/guides/can.html), with the same example in Rust, Python, and C#.
|
|
104
|
+
- [Every capability](https://pamoja.molex.cloud/docs/), and the [install page](https://pamoja.molex.cloud/docs/install.html).
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pamoja/can",
|
|
3
|
+
"version": "0.1.15",
|
|
4
|
+
"description": "CAN 2.0 and CAN-FD frames with 11- and 29-bit identifiers, plus J1939 decode and compose.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/molexxxx/pamoja.git",
|
|
12
|
+
"directory": "bindings/node/packages/can"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://pamoja.molex.cloud/docs/guides/can.html",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"pamoja",
|
|
17
|
+
"iot",
|
|
18
|
+
"robotics",
|
|
19
|
+
"can"
|
|
20
|
+
],
|
|
21
|
+
"main": "dist/index.js",
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist/",
|
|
31
|
+
"LICENSE-MIT"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">= 16"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@pamoja/native": "0.1.15"
|
|
38
|
+
}
|
|
39
|
+
}
|