@pamoja/serial 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 +87 -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,87 @@
|
|
|
1
|
+
# @pamoja/serial
|
|
2
|
+
|
|
3
|
+
SLIP and COBS byte stuffing with streaming decoders, so a UART byte stream carries discrete packets. 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_serial.html)
|
|
6
|
+
[](https://pamoja.molex.cloud/docs/guides/serial.html)
|
|
7
|
+
[](https://pamoja.molex.cloud/docs/)
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @pamoja/serial
|
|
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/serial.ts`](https://github.com/molexxxx/pamoja/blob/main/bindings/node/guides/serial.ts):
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import {
|
|
25
|
+
COBS_DELIMITER_BYTE,
|
|
26
|
+
SLIP_END_BYTE,
|
|
27
|
+
SLIP_ESC_BYTE,
|
|
28
|
+
SlipDecoder,
|
|
29
|
+
cobs,
|
|
30
|
+
slip,
|
|
31
|
+
} from '@pamoja/serial'
|
|
32
|
+
|
|
33
|
+
// A UART carries bytes, not packets, so a framing has to mark where one packet ends.
|
|
34
|
+
// SLIP reserves two byte values for that, and the package names both: the end byte closes
|
|
35
|
+
// a frame, the escape byte carries a value that would otherwise look like one.
|
|
36
|
+
const payload = Buffer.concat([Buffer.from('lvl='), Buffer.from([SLIP_END_BYTE, SLIP_ESC_BYTE])])
|
|
37
|
+
const framed = slip.encode(payload)
|
|
38
|
+
console.log(`slip ${payload.length} payload bytes framed as ${framed.length}`)
|
|
39
|
+
|
|
40
|
+
// Decoding gives the payload back unchanged, reserved bytes and all.
|
|
41
|
+
const restored = slip.decode(framed)
|
|
42
|
+
console.log(`slip decoded back to ${restored.length} bytes`)
|
|
43
|
+
|
|
44
|
+
// COBS trades that escaping for one code byte per run of up to 254 non-zero bytes, each
|
|
45
|
+
// run led by its own length, so a frame never grows by more than a byte per 254. Zero is
|
|
46
|
+
// the delimiter, and never appears inside a frame.
|
|
47
|
+
const packet = Buffer.concat([Buffer.from('lvl='), Buffer.from([COBS_DELIMITER_BYTE]), Buffer.from('7')])
|
|
48
|
+
const cobsFramed = cobs.encode(packet)
|
|
49
|
+
console.log(`cobs ${packet.length} payload bytes framed as ${cobsFramed.length}`)
|
|
50
|
+
|
|
51
|
+
// A read from a port returns whatever arrived, which is rarely one whole frame. This
|
|
52
|
+
// chunk holds two good frames with a truncated one between them; the decoder hands over
|
|
53
|
+
// the good ones and discards only the bad frame.
|
|
54
|
+
const decoder = new SlipDecoder()
|
|
55
|
+
const chunk = Buffer.concat([
|
|
56
|
+
Buffer.from('ok'),
|
|
57
|
+
Buffer.from([SLIP_END_BYTE]),
|
|
58
|
+
Buffer.from([SLIP_ESC_BYTE]), // a frame that ends before its escape pair completes
|
|
59
|
+
Buffer.from([SLIP_END_BYTE]),
|
|
60
|
+
Buffer.from('go'),
|
|
61
|
+
Buffer.from([SLIP_END_BYTE]),
|
|
62
|
+
])
|
|
63
|
+
const frames = decoder.feed(chunk)
|
|
64
|
+
for (const frame of frames) {
|
|
65
|
+
console.log(`received ${frame.toString()}`)
|
|
66
|
+
}
|
|
67
|
+
console.log(`discarded ${decoder.discarded} frame the stream mangled`)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## The same capability in every language
|
|
71
|
+
|
|
72
|
+
| Language | Package | Reference |
|
|
73
|
+
| --- | --- | --- |
|
|
74
|
+
| Rust | [`pamoja-serial`](https://crates.io/crates/pamoja-serial) | [reference](https://pamoja.molex.cloud/docs/reference/rust/pamoja_serial/index.html), [docs.rs](https://docs.rs/pamoja-serial) |
|
|
75
|
+
| TypeScript | [`@pamoja/serial`](https://www.npmjs.com/package/@pamoja/serial) | [reference](https://pamoja.molex.cloud/docs/reference/node/modules/_pamoja_serial.html) |
|
|
76
|
+
| Python | [`pamoja-serial`](https://pypi.org/project/pamoja-serial/) | [reference](https://pamoja.molex.cloud/docs/reference/python/pamoja/serial.html) |
|
|
77
|
+
| C# | [`Pamoja.Serial`](https://www.nuget.org/packages/Pamoja.Serial) | [reference](https://pamoja.molex.cloud/docs/reference/dotnet/api/Pamoja.Serial.html) |
|
|
78
|
+
|
|
79
|
+
## Documentation
|
|
80
|
+
|
|
81
|
+
- [`@pamoja/serial` reference](https://pamoja.molex.cloud/docs/reference/node/modules/_pamoja_serial.html), every class, function, and type this package exports.
|
|
82
|
+
- [The Serial framing guide](https://pamoja.molex.cloud/docs/guides/serial.html), with the same example in Rust, Python, and C#.
|
|
83
|
+
- [Every capability](https://pamoja.molex.cloud/docs/), and the [install page](https://pamoja.molex.cloud/docs/install.html).
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pamoja/serial",
|
|
3
|
+
"version": "0.1.15",
|
|
4
|
+
"description": "SLIP and COBS byte stuffing with streaming decoders, so a UART byte stream carries discrete packets.",
|
|
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/serial"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://pamoja.molex.cloud/docs/guides/serial.html",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"pamoja",
|
|
17
|
+
"iot",
|
|
18
|
+
"robotics",
|
|
19
|
+
"serial"
|
|
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
|
+
}
|