1.0.2 • Published 10 months ago

@adeunis/capacitor-serial v1.0.2

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

capacitor-serial

Manage serial connections to Android devices for Capacitor projects.

NB: Does not work for Web or iOS.

Install

npm install @adeunis/capacitor-serial
npx cap sync

Basic usage

    //Connection
    Serial.requestSerialPermissions({vendorId: '1111', productId: '2222', driver: SerialDriverEnum.CDC_ACM_SERIAL_DRIVER})
    .then((permissionResponse) => {
        if (!permissionResponse.granted) {
            return Promise.reject('Permission refused');
        }
        return Promise.resolve();

    })
    .then(() => Serial.openConnection({baudRate: 115200}))
    .then(() => log.info('Serial connection opend'))
    .catch((error) => log.error(error));

    //Write
    Serial.write({data: 'my_message'})
        .then(() => log.info('Data sent'))
        .catch((error: SerialError ) => {
            log.error(`error : ${error}`);
        });
    
    
    //Read
    Serial.read({readRaw: false}).then((message) => log.info(message.data));

    //Read callback 
    Serial.registerReadCallback((message, error) => {
        if (message !== undefined && message !== null) {
            log.info(message.data);

        } else if (error !== undefined && error !== null) {
            log.error(`error : ${error}`);
        }
    })

    Serial.closeConnection().then(() => log.info('Serial connection closed'));

API

requestSerialPermissions(...)

requestSerialPermissions(parameters?: SerialPermissionsParameters | undefined) => Promise<SerialPermissions>

Request permissions to connect to a device over a serial connection

ParamTypeDescription
parametersSerialPermissionsParametersParameters used to request permissions for a specific productId and vendorId (integer value or hexadecimal string), and a specific driver

Returns: Promise<SerialPermissions>


openConnection(...)

openConnection(parameters: SerialConnectionParameters) => Promise<void>

Open a serial connection to a device

ParamTypeDescription
parametersSerialConnectionParametersParameters used to open a serial connection

closeConnection()

closeConnection() => Promise<void>

Close the serial connection


write(...)

write(message: SerialMessage) => Promise<void>

Write a message to a serial connection

ParamTypeDescription
messageSerialMessagecontains the data string to write to the serial connection

writeHexadecimal(...)

writeHexadecimal(message: SerialMessage) => Promise<void>

Write a hexadecimal message to a serial connection

ParamTypeDescription
messageSerialMessagecontains the data string in hexadecimal to write to the serial connection

read(...)

read(parameters: SerialReadParameters) => Promise<SerialMessage>

Read from a serial connection

ParamTypeDescription
parametersSerialReadParametersspecify if the read data should be sent back as 'raw', meaning the byte array encoded in base64 string, or as a UTF-8 decoded string

Returns: Promise<SerialMessage>


registerReadCallback(...)

registerReadCallback(callback: SerialReadCallback) => Promise<string>

Register a callback to receive the incoming data from the serial connection

ParamTypeDescription
callbackSerialReadCallbackthe callback called each time there is incoming data from the serial connection, the data being a UTF-8 decoded string

Returns: Promise<string>


unregisterReadCallback()

unregisterReadCallback() => Promise<void>

Unregister the read callback


registerReadRawCallback(...)

registerReadRawCallback(callback: SerialReadCallback) => Promise<string>

Register a callback to receive the incoming raw data from the serial connection

ParamTypeDescription
callbackSerialReadCallbackthe callback called each time there is incoming data from the serial connection, the data being the byte array encoded in base64 string

Returns: Promise<string>


unregisterReadRawCallback()

unregisterReadRawCallback() => Promise<void>

Unregister the read raw callback


Interfaces

SerialPermissions

PropType
grantedboolean

SerialPermissionsParameters

PropType
vendorIdstring | number
productIdstring | number
driverSerialDriverEnum

SerialConnectionParameters

PropType
baudRatenumber
dataBitsnumber
stopBitsnumber
paritynumber
dtrboolean
rtsboolean

SerialMessage

PropType
datastring

SerialReadParameters

PropType
readRawboolean

Type Aliases

SerialReadCallback

(message: SerialMessage, error?: SerialError): void

Enums

SerialDriverEnum

MembersValue
FTDI_SERIAL_DRIVER"FtdiSerialDriver"
CDC_ACM_SERIAL_DRIVER"CdcAcmSerialDriver"
CP21XX_SERIAL_DRIVER"Cp21xxSerialDriver"
PROLIFIC_SERIAL_DRIVER"ProlificSerialDriver"
CH34X_SERIAL_DRIVER"Ch34xSerialDriver"

SerialError

MembersValue
UNKNOWN_DRIVER_ERROR"UNKNOWN_DRIVER_ERROR"
NO_DEVICE_ERROR"NO_DEVICE_ERROR"
PARAMETER_ERROR"PARAMETER_ERROR"
CONNECTION_ERROR"CONNECTION_ERROR"
PORT_CLOSED_ERROR"PORT_CLOSED_ERROR"
1.0.2

10 months ago

1.0.1

11 months ago