Skip to content

Commit d319162

Browse files
author
karurochari
committed
list serials, fix optional cfg
1 parent 6f6a09f commit d319162

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
- [ ] ls to search for all available serials
1+
- [x] open a serial via file and lock it
2+
- [x] ls to search for all available serials
23
- [ ] reconfiguration of the serial

src/ts/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ type serial_cfg_t = Static<typeof serial_cfg_schema>
2222

2323
const core = globalThis[Symbol.for('tjs.internal.core')];
2424

25-
export function open(file: string, cfg: serial_cfg_t) : tjs.FileHandle {
25+
/**
26+
* Open a serial device and lock it.
27+
* @param file the path for the file mapped serial.
28+
* @param cfg the configuration of the serial.
29+
* @returns a normal file handle. Just use it as you normally would.
30+
*/
31+
export function open(file: string, cfg?: serial_cfg_t) : tjs.FileHandle {
2632
const pcfg = Value.Default(serial_cfg_schema, cfg)
2733
//@ts-ignore
2834
const fd = core.$__MODULE__.configure(file, {
@@ -36,6 +42,19 @@ export function open(file: string, cfg: serial_cfg_t) : tjs.FileHandle {
3642
else throw new Error(`Unable to use serial connection with [${file}]`);
3743
}
3844

45+
/**
46+
*
47+
* @returns The list of available files mapping serial devices.
48+
*/
49+
export async function list() : Promise<string[]>{
50+
const names = []
51+
const dirIter = await tjs.readdir('/dev/serial/by-id/');
52+
for await (const item of dirIter) {
53+
names.push(`/dev/serial/by-id/${item.name}`);
54+
}
55+
return names;
56+
}
57+
3958
export {
4059
serial_cfg_schema, type serial_cfg_t,
4160
}

0 commit comments

Comments
 (0)