5.7.20 • Published 9 months ago

serialconnectioncapacitor v5.7.20

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

SerialConnectionCapacitor

A Capacitor plugin for serial port communication in web and mobile applications.

Installation

npm install serialconnectioncapacitor
npx cap sync
for 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 NameDescriptionData Structure
connectionOpenedWhen connection is established{ message: string }
connectionErrorWhen connection fails{ error: string }
nativeWriteSuccessWhen data is written successfully{ message: string }
writeErrorWhen write operation fails{ error: string }
dataReceivedWhen data is received{ data: string }
readErrorWhen read operation fails{ error: string }
readingStoppedWhen reading is stopped{ message: string }
connectionClosedWhen 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

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.

ParamTypeDescription
optionsSerialPortOptionsConnection options including port path and baud rate.

Returns: Promise<any>


openUsbSerial(...)

openUsbSerial(options: SerialPortOptions) => Promise<any>

Opens a USB serial port connection.

ParamTypeDescription
optionsSerialPortOptionsConnection options including port path and baud rate.

Returns: Promise<any>


write(...)

write(options: SerialPortWriteOptions) => Promise<any>

Writes data to the serial port.

ParamTypeDescription
optionsSerialPortWriteOptionsWrite options containing the command to send.

Returns: Promise<any>


writeVMC(...)

writeVMC(options: SerialPortWriteOptions) => Promise<any>

Writes data to the serial port.

ParamTypeDescription
optionsSerialPortWriteOptionsWrite 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> & PluginListenerHandle

Add listener for serial port events

ParamTypeDescription
eventNameSerialPortEventTypesThe event to listen for
listenerFunc(...args: any[]) => voidCallback function when event occurs

Returns: Promise<PluginListenerHandle> & PluginListenerHandle


Interfaces

SerialPortListResult

Result of listing available serial ports.

PropTypeDescription
ports{ key: string: number; }Available serial ports.

SerialPortOptions

Options for opening a serial port connection.

PropTypeDescription
portNamestringPath to the serial port (e.g., /dev/ttyUSB0 or COM3).
baudRatenumberBaud rate for the serial port connection.
dataBitsnumber
stopBitsnumber
paritystring
bufferSizenumber
flagsnumber

SerialPortWriteOptions

Options for writing to a serial port.

PropTypeDescription
datastringCommand to send to the serial port (hex string or text).

PluginListenerHandle

PropType
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

5.7.20

9 months ago

5.7.18

9 months ago

5.7.17

9 months ago

5.7.16

9 months ago

5.7.15

9 months ago

5.7.14

9 months ago

5.7.13

9 months ago

5.7.12

9 months ago

5.7.11

9 months ago

5.7.10

9 months ago

5.7.8

9 months ago

5.7.7

9 months ago

5.7.6

9 months ago

5.7.5

9 months ago

5.7.4

9 months ago

5.7.2

9 months ago

5.7.1

9 months ago

5.7.0

9 months ago

0.0.107

9 months ago

0.0.106

9 months ago

0.0.105

9 months ago

0.0.104

9 months ago

0.0.103

9 months ago

0.0.102

9 months ago

0.0.101

9 months ago

0.0.100

9 months ago

0.0.99

9 months ago

0.0.98

9 months ago

0.0.97

9 months ago

0.0.96

9 months ago

0.0.95

9 months ago

0.0.94

9 months ago

0.0.93

9 months ago

0.0.92

9 months ago

0.0.91

9 months ago

0.0.90

9 months ago

0.0.89

9 months ago

0.0.88

9 months ago

0.0.87

9 months ago

0.0.86

9 months ago

0.0.85

9 months ago

0.0.84

9 months ago

0.0.83

9 months ago

0.0.82

9 months ago

0.0.81

9 months ago

0.0.80

9 months ago

0.0.79

9 months ago

0.0.78

9 months ago

0.0.77

9 months ago

0.0.76

9 months ago

0.0.75

9 months ago

0.0.74

9 months ago

0.0.73

9 months ago

0.0.72

9 months ago

0.0.71

9 months ago

0.0.70

9 months ago

0.0.69

9 months ago

0.0.67

9 months ago

0.0.66

9 months ago

0.0.65

9 months ago

0.0.64

9 months ago

0.0.63

9 months ago

0.0.62

9 months ago

0.0.61

9 months ago

0.0.60

9 months ago

0.0.58

9 months ago

0.0.57

9 months ago

0.0.56

9 months ago

0.0.55

9 months ago

0.0.52

9 months ago

0.0.51

9 months ago

0.0.50

9 months ago

0.0.49

9 months ago

0.0.48

9 months ago

0.0.47

9 months ago

0.0.46

9 months ago

0.0.45

9 months ago

0.0.44

9 months ago

0.0.43

9 months ago

0.0.42

9 months ago

0.0.41

9 months ago

0.0.39

9 months ago

0.0.38

9 months ago

0.0.37

9 months ago

0.0.36

9 months ago

0.0.35

9 months ago

0.0.34

9 months ago

0.0.33

9 months ago

0.0.31

9 months ago

0.0.30

9 months ago

0.0.29

9 months ago

0.0.27

9 months ago

0.0.25

10 months ago

0.0.24

10 months ago

0.0.23

10 months ago

0.0.22

10 months ago

0.0.21

10 months ago

0.0.20

10 months ago

0.0.19

10 months ago

0.0.18

10 months ago

0.0.16

10 months ago

0.0.15

10 months ago

0.0.14

10 months ago

0.0.13

10 months ago

0.0.12

10 months ago

0.0.11

10 months ago

0.0.10

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago