serialconnectioncapacitor v5.7.20
SerialConnectionCapacitor
A Capacitor plugin for serial port communication in web and mobile applications.
Installation
npm install serialconnectioncapacitor
npx cap syncfor Android Only , 24 and above, serial port need a rooted android
capacitor 5.0.1 special version for VMC using polling and timing method
if you need another version please find the versions
it uses native libs libserial_port.so , I have built for many architures , thanks grok that guided me along!Android Setup
Add the following permission to your AndroidManifest.xml:
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />Events
The plugin emits the following events:
| Event Name | Description | Data Structure |
|---|---|---|
connectionOpened | When connection is established | { message: string } |
connectionError | When connection fails | { error: string } |
nativeWriteSuccess | When data is written successfully | { message: string } |
writeError | When write operation fails | { error: string } |
dataReceived | When data is received | { data: string } |
readError | When read operation fails | { error: string } |
readingStopped | When reading is stopped | { message: string } |
connectionClosed | When connection is closed | { message: string } |
Event Handling Example
import { serialConnectionCapacitor } from 'serialconnectioncapacitor';
// Connection events
serialConnectionCapacitor.addEvent('connectionOpened', (event) => {
console.log('Connection established:', event.message);
});
// Data events
serialConnectionCapacitor.addEvent('dataReceived', (event) => {
console.log('Received data:', event.data);
});
// Error handling
serialConnectionCapacitor.addEvent('readError', (event) => {
console.error('Read error:', event.error);
});
serialConnectionCapacitor.addEvent('connectionError', (event) => {
console.error('Connection error:', event.error);
});
// Cleanup
serialConnectionCapacitor.addEvent('connectionClosed', (event) => {
console.log('Connection closed:', event.message);
});API
listPorts()openSerial(...)openUsbSerial(...)write(...)writeVMC(...)startReading()startReadingVMC()stopReading()close()addListener(SerialPortEventTypes, ...)- Interfaces
- Type Aliases
Plugin interface for serial port communication.
listPorts()
listPorts() => Promise<SerialPortListResult>Lists available serial ports.
Returns: Promise<SerialPortListResult>
openSerial(...)
openSerial(options: SerialPortOptions) => Promise<any>Opens a serial port connection.
| Param | Type | Description |
|---|---|---|
options | SerialPortOptions | Connection options including port path and baud rate. |
Returns: Promise<any>
openUsbSerial(...)
openUsbSerial(options: SerialPortOptions) => Promise<any>Opens a USB serial port connection.
| Param | Type | Description |
|---|---|---|
options | SerialPortOptions | Connection options including port path and baud rate. |
Returns: Promise<any>
write(...)
write(options: SerialPortWriteOptions) => Promise<any>Writes data to the serial port.
| Param | Type | Description |
|---|---|---|
options | SerialPortWriteOptions | Write options containing the command to send. |
Returns: Promise<any>
writeVMC(...)
writeVMC(options: SerialPortWriteOptions) => Promise<any>Writes data to the serial port.
| Param | Type | Description |
|---|---|---|
options | SerialPortWriteOptions | Write options containing the command to send. |
Returns: Promise<any>
startReading()
startReading() => Promise<any>Start reading data from the serial port.
Returns: Promise<any>
startReadingVMC()
startReadingVMC() => Promise<any>Start reading data from the serial port.
Returns: Promise<any>
stopReading()
stopReading() => Promise<any>Stop reading data from the serial port.
Returns: Promise<any>
close()
close() => Promise<any>Closes the serial port connection.
Returns: Promise<any>
addListener(SerialPortEventTypes, ...)
addListener(eventName: SerialPortEventTypes, listenerFunc: (...args: any[]) => void) => Promise<PluginListenerHandle> & PluginListenerHandleAdd listener for serial port events
| Param | Type | Description |
|---|---|---|
eventName | SerialPortEventTypes | The event to listen for |
listenerFunc | (...args: any[]) => void | Callback function when event occurs |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
Interfaces
SerialPortListResult
Result of listing available serial ports.
| Prop | Type | Description |
|---|---|---|
ports | { key: string: number; } | Available serial ports. |
SerialPortOptions
Options for opening a serial port connection.
| Prop | Type | Description |
|---|---|---|
portName | string | Path to the serial port (e.g., /dev/ttyUSB0 or COM3). |
baudRate | number | Baud rate for the serial port connection. |
dataBits | number | |
stopBits | number | |
parity | string | |
bufferSize | number | |
flags | number |
SerialPortWriteOptions
Options for writing to a serial port.
| Prop | Type | Description |
|---|---|---|
data | string | Command to send to the serial port (hex string or text). |
PluginListenerHandle
| Prop | Type |
|---|---|
remove | () => Promise<void> |
Type Aliases
SerialPortEventTypes
'portsListed' | 'serialOpened' | 'usbSerialOpened' | 'connectionClosed' | 'usbWriteSuccess' | 'dataReceived' | 'readingStarted' | 'readingStopped' | 'serialWriteSuccess' | 'commandAcknowledged' | 'commandQueued'
Example Usage
import { serialConnectionCapacitor } from 'serialconnectioncapacitor';
class SerialPortManager {
private isReading = false;
async initialize() {
try {
const { ports } = await serialConnectionCapacitor.listPorts();
if (Object.keys(ports).length === 0) {
throw new Error('No serial ports available');
}
const portName = Object.keys(ports)[0];
await serialConnectionCapacitor.open({
portName,
baudRate: 115200
});
this.setupEvents();
return true;
} catch (error) {
console.error('Initialization failed:', error);
return false;
}
}
private setupEvents() {
// Connection events
serialConnectionCapacitor.addEvent('connectionOpened', (event) => {
console.log('Connected:', event.message);
});
serialConnectionCapacitor.addEvent('connectionClosed', (event) => {
console.log('Disconnected:', event.message);
});
// Data events
serialConnectionCapacitor.addEvent('dataReceived', (event) => {
console.log('Received:', event.data);
});
serialConnectionCapacitor.addEvent('nativeWriteSuccess', (event) => {
console.log('Write successful:', event.message);
});
// Error events
serialConnectionCapacitor.addEvent('readError', (event) => {
console.error('Read error:', event.error);
});
serialConnectionCapacitor.addEvent('connectionError', (event) => {
console.error('Connection error:', event.error);
});
}
async startReading() {
if (!this.isReading) {
await serialConnectionCapacitor.startReading();
this.isReading = true;
}
}
async stopReading() {
if (this.isReading) {
await serialConnectionCapacitor.stopReading();
this.isReading = false;
}
}
async sendCommand(command: string) {
await serialConnectionCapacitor.write({ command });
}
async cleanup() {
await this.stopReading();
await serialConnectionCapacitor.close();
}
}
// Usage
const serialManager = new SerialPortManager();
await serialManager.initialize();
await serialManager.startReading();
await serialManager.sendCommand('TEST');
// ... later
await serialManager.cleanup();License
MIT
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago