https://github.com/mattfors/ngx-web-serial
Angular Library for connectin to serial devices using Web Serial API and RxJS
https://github.com/mattfors/ngx-web-serial
angular serial
Last synced: 5 months ago
JSON representation
Angular Library for connectin to serial devices using Web Serial API and RxJS
- Host: GitHub
- URL: https://github.com/mattfors/ngx-web-serial
- Owner: mattfors
- License: mit
- Created: 2024-11-18T21:38:17.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-27T01:51:05.000Z (over 1 year ago)
- Last Synced: 2025-10-17T06:39:24.521Z (8 months ago)
- Topics: angular, serial
- Language: TypeScript
- Homepage:
- Size: 149 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/mattfors/ngx-web-serial/actions/workflows/build.yml)
[](https://codecov.io/gh/mattfors/ngx-web-serial)
# NgxWebSerial
NgxWebSerial is an angular library for connecting to serial devices with the Web Serial API and RxJS.
Connecting, transmitting and receiving data are abstracted to RxJS observables which will be familiar to anyone using Angular.
## Installation
```shell
npm i ngx-web-serial
```
## Usage
Below demonstrates the basic usage. A pipe is used to accumulate the raw data from the serial port.
```typescript
import { Component } from '@angular/core';
import { NgxWebSerial, provideNgxWebSerial } from 'ngx-web-serial';
import { Observable, scan } from 'rxjs';
import { AsyncPipe } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [AsyncPipe],
providers: [provideNgxWebSerial()],
template: `
Open
`
})
export class AppComponent {
data$: Observable;
constructor(private serial: NgxWebSerial) {
this.data$ = this.serial.read().pipe(
scan((acc, value) => acc + value, '')
);
}
open(): void {
this.serial.open().subscribe()
}
write(value: string): void {
this.serial.write(value).subscribe();
}
}
```
## Mock serial device
The module can be used with a mock serial device for testing or if you do not have a real serial device. Provide a function which takes and returns a string. In this example, the text transmitted to the serial device will be echoed back with 'Hello'.
```typescript
providers: [provideNgxWebSerialTest(i => `Hello ${i}!\n`)]
```