Skip to content

Methods

Melih Yarıkkaya edited this page Apr 29, 2019 · 15 revisions

startUsbService
stopUsbService
getDeviceList
connectDevice
disconnect
isOpen
isSupported
isServiceStarted
writeString
writeBase64
writeHexString

Setter Methods

setReturnedDataType
setDriver
setInterface
setAutoConnect
setAutoConnectBaudRate
setDataBit
setStopBit
setParity
setFlowControl
loadDefaultConnectionSetting

startUsbService

Starts the service and usb broadcast receivers

No Params

RNSerialport.startUsbService()

stopUsbService

Stops the service and usb broadcast receivers

No Params

RNSerialport.stopUsbService()

getDeviceList

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
});

connectDevice

Use to manual connection

Params:

Name TYPE REQUIRED
deviceName string yes for call
baudRate number yes for call
RNSerialport.connectDevice("deviceName", 9600);

disconnect

Closes the connection

No Params

RNSerialport.disconnect()

isOpen

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);
});

isSupported

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);
});

isServiceStarted

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);
});

writeString

Writes data to serial port

Name TYPE REQUIRED
data string yes for call
RNSerialport.writeString("HELLO");

writeBase64

Writes data to serial port

Name TYPE REQUIRED
data string yes for call
RNSerialport.writeBase64("SEVMTE8=");

writeHexString

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");

setReturnedDataType

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
) 

setDriver

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)

setInterface

Changes the serial interface

Default: -1

Params:

TYPE REQUIRED
number yes for call
RNSerialport.setInterface(1)

setAutoConnectBaudRate

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)

setAutoConnect

Turns automatic connection on or off

Default: off

Params:

TYPE REQUIRED
boolean yes for call
RNSerialport.setAutoConnect(true)

setDataBit

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)

setStopBit

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)

setParity

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)

setFlowControl

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)

loadDefaultConnectionSetting

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();