-
Notifications
You must be signed in to change notification settings - Fork 105
Methods
startUsbService
stopUsbService
getDeviceList
connectDevice
disconnect
isOpen
isSupported
isServiceStarted
writeString
writeBase64
writeHexString
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:
No param
//1st way
try {
const isOpen = await RNSerialport.isOpen();
if(isOpen)
console.log("Is open?", "yes");
else
console.log("Is open?", "no");
} catch(err) {
console.log(err);
}
//2st way
RNSerialport.isOpen().then(isOpen => {
if(isOpen) {
console.log("Is open?", "yes");
} else {
console.log("Is oprn?", "no");
}
}).catch(err => {
console.log(err);
});
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(isSupported => {
if(isSupported) {
console.log("Is supported?", "yes");
} else {
console.log("Is supported?", "no");
}
}).catch(err => {
console.log(err);
});
Returns service status
No param
//1st way
try {
const isServiceStarted = await RNSerialport.isServiceStarted();
if(isServiceStarted)
console.log("Is ServiceStarted?", "yes");
else
console.log("Is ServiceStarted?", "no");
} catch(err) {
}
//2st way
RNSerialport.isServiceStarted().then(isServiceStarted => {
if(isServiceStarted) {
console.log("Is service started?", "yes");
} else {
console.log("Is service started?", "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=");
Writes data to serial port
Note: Make sure the text has a valid hexadecimal number system! Otherwise this does nothing.
Name | TYPE | REQUIRED |
---|---|---|
data | string | yes for call |
RNSerialport.writeHexString("0F"); // 1 byte
RNSerialport.writeHexString("FF0F"); // 2 btye
RNSerialport.writeHexString("48454C4C4F"); // 5 byte
//The following are not recommended.
RNSerialport.writeHexString("F");
RNSerialport.writeHexString("FFF");
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
Why it is necessary? Using createUsbSerialDevice method specifying 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();