This capacitor plugin allows basic serial-over-usb functionality
npm install capacitor-usb-serial-c7bash
npm install capacitor-usb-serial
npx cap sync
`
API
* getDeviceHandlers()
* getDeviceConnections()
* openConnection(...)
* getActivePorts()
* endConnection(...)
* endConnections(...)
* write(...)
* read(...)
* Interfaces
* Type Aliases
Defines the interface for USB serial communication plugin
$3
`typescript
getDeviceHandlers() => Promise
`
Returns an array of DeviceHandler objects for all connected USB devices.
This helper function provides a simplified interface for interacting with the plugin methods. It is recommended for most use cases unless more granular control is required.
Each DeviceHandler object includes methods for connecting, disconnecting, writing to, and reading from a specific device.
Returns: Promise<DeviceHandler[]>
--------------------
$3
`typescript
getDeviceConnections() => Promise<{ devices: DeviceInfo[]; }>
`
Returns all connected devices
Returns: Promise<{ devices: DeviceInfo[]; }>
--------------------
$3
`typescript
openConnection(options: FullConnectionParams) => Promise<{ portKey: string; }>
`
Connect to a device using its deviceId
| Param | Type | Description |
| ------------- | --------------------------------------------------------------------- | ------------------------------------------ |
| options | FullConnectionParams | - Connection parameters including deviceId |
Returns: Promise<{ portKey: string; }>
--------------------
$3
`typescript
getActivePorts() => Promise<{ ports: string[]; }>
`
Returns all active ports
Returns: Promise<{ ports: string[]; }>
--------------------
$3
`typescript
endConnection(options: { key: string; }) => Promise
`
Disconnect from a device using its assigned portKey
| Param | Type | Description |
| ------------- | ----------------------------- | ------------------------------- |
| options | { key: string; } | - Object containing the portKey |
--------------------
$3
`typescript
endConnections(options?: { keys?: string[] | undefined; } | undefined) => Promise
`
Disconnect from all devices or specified devices
| Param | Type | Description |
| ------------- | --------------------------------- | --------------------------------------------------------------- |
| options | { keys?: string[]; } | - Optional object containing an array of portKeys to disconnect |
--------------------
$3
`typescript
write(options: { key: string; message: string; noRead?: boolean }) => Promise
`
Write a message to a device using its assigned portKey and performs a quick read straight afterwards.
If noRead is passed as true the read would be skipped.
| Param | Type | Description |
| ------------- | ---------------------------------------------- | ---------------------------------------------------- |
| options | { key: string; message: string; noRead?: boolean } | - Object containing the portKey and message to write |
--------------------
$3
`typescript
read(options: { key: string; }) => Promise
`
Read a message from a device using its assigned portKey
| Param | Type | Description |
| ------------- | ----------------------------- | ------------------------------- |
| options | { key: string; } | - Object containing the portKey |
Returns: Promise<ReadResponse>
--------------------
$3
#### DeviceHandler
Provides a simplified interface for handling a specific device
| Method | Type | Description |
| -----------------| ---------------------------------------------------------- | --------------------------------- |
| connect | (options?: ConnectionParams) => Promise | Connect to the device |
| disconnect | () => Promise | Disconnect from the device |
| write | (message: string) => Promise | Write a message to the device |
| read | () => Promise | Read a message from the device |
#### DeviceInfo
Represents information about a connected device
| Prop | Type | Description |
| ---------------- | ------------------- | --------------------------------- |
| deviceKey | string | Unique identifier used internally |
| deviceId | number | Numeric identifier for the device |
| productId | number | Product ID of the device |
| vendorId | number | Vendor ID of the device |
| deviceName | string | Human-readable name of the device |
#### FullConnectionParams
Extends ConnectionParams to include deviceId
| Prop | Type | Description |
| -------------- | ------------------- | -------------------------------- |
| deviceId` | number | Unique identifier for the device |
{ data: string, bytesRead: number }