-
Notifications
You must be signed in to change notification settings - Fork 105
Methods
startUsbService
stopUsbService
getDeviceList
connectDevice
disconnect
isOpen
isSupported
writeString
writeBase64
setReturnedDataType
setDriver
setInterface
setAutoConnect
setAutoConnectBaudRate
setDataBit
setStopBit
setParity
setFlowControl
loadDefaultConnectionSetting
Starts the service and usb broadcast receivers
No Params
RNSerialport.startUsbService()
Stops the service and usb broadcast receivers
No Params
RNSerialport.stopUsbService()
Receives device list
Params:
TYPE | REQUIRED |
---|---|
callback: fn | yes for call |
RNSerialport.getDeviceList((response) => {
if(!response.status) {
response.log("Error from getDeviceList()", response.errorCode + " " + response.errorMessage)
return;
}
console.log(response.devices)//list
});
Use to manual connection
Params:
Name | TYPE | REQUIRED |
---|---|---|
deviceName | string | yes for call |
baudRate | number | yes for call |
RNSerialport.connectDevice("deviceName", 9600);
Closes the connection
No Params
RNSerialport.disconnect()
Returns connection status
Params:
Name | TYPE | REQUIRED |
---|---|---|
callback | function | yes for call |
RNSerialport.isOpen((status /* boolean */) => {
if(status) {
console.log("Is open?", "yes");
} else {
console.log("Is open?", "no");
}
})
Returns support status
Params:
Name | TYPE | REQUIRED |
---|---|---|
deviceName | string | yes for call |
//1st way
try {
const isSupported = await RNSerialport.isSupported("deviceName");
if(isSupported)
console.log("Is supported?", "yes")
else
console.log("Is supported?", "no");
} catch(err) {
}
//2st way
RNSerialport.isSupported("deviceName").then(status => {
if(isSupported) {
console.log("Is supported?", "yes");
} else {
console.log("Is supported?", "no");
}
}).catch(err => {
console.log(err);
});
Writes data to serial port
Name | TYPE | REQUIRED |
---|---|---|
data | string | yes for call |
RNSerialport.writeString("HELLO");
Writes data to serial port
Name | TYPE | REQUIRED |
---|---|---|
data | string | yes for call |
RNSerialport.writeBase64("SEVMTE8=");
Changes the data type in "ON_READ_DATA" event
Default: INTARRAY
Params:
TYPE | REQUIRED |
---|---|
number | yes for call |
import { definitions } from "react-native-serialport
RNSerialport.setReturnedDataType(
definitions.RETURNED_DATA_TYPES.HEXSTRING
)
// or
RNSerialport.setReturnedDataType(
definitions.RETURNED_DATA_TYPES.INTARRAY
)
Changes the driver
Default: AUTO
Params:
TYPE | REQUIRED |
---|---|
string | yes for call |
import { definitions } from "react-native-serialport
RNSerialport.setDriver(definitions.DRIVER_TYPES.AUTO)
Changes the serial interface
Default: -1
Params:
TYPE | REQUIRED |
---|---|
number | yes for call |
RNSerialport.setInterface(1)
Changes baud rate to be used on automatic connection
Used before starting the service.
Default: 9600
Params:
TYPE | REQUIRED |
---|---|
number | yes for call |
RNSerialport.setAutoConnectBaudRate(115200)
Turns automatic connection on or off
Default: off
Params:
TYPE | REQUIRED |
---|---|
boolean | yes for call |
RNSerialport.setAutoConnect(true)
Changes the data bit
Default: DATA_BITS_8
Params:
TYPE | REQUIRED |
---|---|
number | yes for call |
import { definitions } from "react-native-serialport
RNSerialport.setDataBit(definitions.DATA_BITS.DATA_BITS_8)
Changes the stop bit
Default: STOP_BITS_1
Params:
TYPE | REQUIRED |
---|---|
number | yes for call |
import { definitions } from "react-native-serialport"
RNSerialport.setStopBit(definitions.STOP_BITS.STOP_BITS_1)
Changes the parity
Default: PARITY_NONE
Params:
TYPE | REQUIRED |
---|---|
number | yes for call |
import { definitions } from "react-native-serialport"
RNSerialport.setParity(definitions.PARITIES.PARITY_NONE)
Changes the flow control mode
Default: FLOW_CONTROL_OFF
Params:
TYPE | REQUIRED |
---|---|
number | yes for call |
import { definitions } from "react-native-serialport"
RNSerialport.setFlowControl(definitions.FLOW_CONTROLS.FLOW_CONTROL_OFF)
Loads the default settings
Defaults: DATA_BIT: DATA_BITS_8
STOP_BIT: STOP_BITS_1
PARITY: PARITY_NONE
FLOW_CONTROL: FLOW_CONTROL_OFF
No Params
RNSerialport.loadDefaultConnectionSetting();