From 93dc6b36e034b070ec1d741bd721c7154c98cd60 Mon Sep 17 00:00:00 2001 From: Melih Date: Mon, 29 Apr 2019 12:37:18 +0300 Subject: [PATCH 1/2] 1.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c81edb5..604ea90 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-serialport", - "version": "1.2.0", + "version": "1.3.0", "description": "This library is for usb serial port communication on android platforms", "main": "index.js", "scripts": { From 1266344af9fd4fdfa101124d118f7a87ef2c10ed Mon Sep 17 00:00:00 2001 From: Melih Date: Mon, 29 Apr 2019 16:26:49 +0300 Subject: [PATCH 2/2] 'getDeviceList' now returns a Promise --- .../rnserialport/RNSerialportModule.java | 13 +- doc-wiki/Manual-Connection.md | 31 +- doc-wiki/Methods.md | 362 +++++++++--------- types/index.d.ts | 171 ++++++--- 4 files changed, 322 insertions(+), 255 deletions(-) diff --git a/android/src/main/java/com/melihyarikkaya/rnserialport/RNSerialportModule.java b/android/src/main/java/com/melihyarikkaya/rnserialport/RNSerialportModule.java index 146070c..19c8843 100644 --- a/android/src/main/java/com/melihyarikkaya/rnserialport/RNSerialportModule.java +++ b/android/src/main/java/com/melihyarikkaya/rnserialport/RNSerialportModule.java @@ -276,9 +276,9 @@ public void stopUsbService() { } @ReactMethod - public void getDeviceList(Callback callback) { + public void getDeviceList(Promise promise) { if(!usbServiceStarted) { - callback.invoke(createError(Definitions.ERROR_USB_SERVICE_NOT_STARTED, Definitions.ERROR_USB_SERVICE_NOT_STARTED_MESSAGE)); + promise.reject(String.valueOf(Definitions.ERROR_USB_SERVICE_NOT_STARTED), Definitions.ERROR_USB_SERVICE_NOT_STARTED_MESSAGE); return; } @@ -287,7 +287,8 @@ public void getDeviceList(Callback callback) { HashMap devices = manager.getDeviceList(); if(devices.isEmpty()) { - callback.invoke(createError(Definitions.ERROR_DEVICE_NOT_FOUND, Definitions.ERROR_DEVICE_NOT_FOUND_MESSAGE)); + //promise.reject(String.valueOf(Definitions.ERROR_DEVICE_NOT_FOUND), Definitions.ERROR_DEVICE_NOT_FOUND_MESSAGE); + promise.resolve(Arguments.createArray()); return; } @@ -303,11 +304,7 @@ public void getDeviceList(Callback callback) { deviceList.pushMap(map); } - WritableMap map = Arguments.createMap(); - map.putBoolean("status", true); - map.putArray("devices", deviceList); - - callback.invoke(map); + promise.resolve(deviceList); } @ReactMethod diff --git a/doc-wiki/Manual-Connection.md b/doc-wiki/Manual-Connection.md index caeeed6..e4960f0 100644 --- a/doc-wiki/Manual-Connection.md +++ b/doc-wiki/Manual-Connection.md @@ -57,7 +57,7 @@ componentDidMount() { **Some precautions** ```javascript -componentWillUnmount = async() => { +componentWillUnmount = async () => { DeviceEventEmitter.removeAllListeners(); const isOpen = await RNSerialport.isOpen(); if (isOpen) { @@ -65,7 +65,7 @@ componentWillUnmount = async() => { RNSerialport.disconnect(); } RNSerialport.stopUsbService(); -} +}; ``` _Installation Successfuly_ @@ -163,7 +163,7 @@ class ManualConnection extends Component { RNSerialport.setReturnedDataType(this.state.returnedDataType); RNSerialport.setAutoConnect(false); RNSerialport.startUsbService(); - }; + } stopUsbListener = async () => { DeviceEventEmitter.removeAllListeners(); @@ -235,18 +235,23 @@ class ManualConnection extends Component { } this.setState({ output: data }); } - fillDeviceList() { - RNSerialport.getDeviceList(response => { - if (response.status) { - this.setState({ deviceList: response.devices }); + fillDeviceList = async () => { + try { + const deviceList = await RNSerialport.getDeviceList(); + if (deviceList.length > 0) { + this.setState({ deviceList }); } else { - Alert.alert( - "Error from getDeviceList()", - response.errorCode + " " + response.errorMessage - ); + this.setState({ + deviceList: [{ name: "Device Not Found", placeholder: true }] + }); } - }); - } + } catch (err) { + Alert.alert( + "Error from getDeviceList()", + err.errorCode + " " + err.errorMessage + ); + } + }; devicePickerItems() { return this.state.deviceList.map((device, index) => !device.placeholder ? ( diff --git a/doc-wiki/Methods.md b/doc-wiki/Methods.md index d588b81..cce47ae 100644 --- a/doc-wiki/Methods.md +++ b/doc-wiki/Methods.md @@ -1,4 +1,3 @@ - [startUsbService](https://github.com/melihyarikkaya/react-native-serialport/wiki/Methods/#startUsbService) [stopUsbService](https://github.com/melihyarikkaya/react-native-serialport/wiki/Methods/#stopUsbService) [getDeviceList](https://github.com/melihyarikkaya/react-native-serialport/wiki/Methods/#getDeviceList) @@ -25,82 +24,87 @@ [loadDefaultConnectionSetting](https://github.com/melihyarikkaya/react-native-serialport/wiki/Methods/#loadDefaultConnectionSetting) ### startUsbService - - _Starts the service and usb broadcast receivers_ + +_Starts the service and usb broadcast receivers_ No Params ```javascript -RNSerialport.startUsbService() +RNSerialport.startUsbService(); ``` -*** +--- ### stopUsbService - - _Stops the service and usb broadcast receivers_ + +_Stops the service and usb broadcast receivers_ No Params ```javascript -RNSerialport.stopUsbService() +RNSerialport.stopUsbService(); ``` -*** +--- ### getDeviceList - _Receives device list_ - - Params: +_Receives device list_ -|TYPE|REQUIRED| -| - | - | -| callback: fn | yes for call | +No Param ```javascript -RNSerialport.getDeviceList((response) => { - if(!response.status) { - response.log("Error from getDeviceList()", response.errorCode + " " + response.errorMessage) - return; +try { + const deviceList = await RNSerialport.getDeviceList(); + if (deviceList.length > 0) { + console.log(deviceList); + } else { + console.log("Device Not Found"); } - console.log(response.devices)//list -}); +} catch (err) { + Alert.alert( + "Error from getDeviceList()", + err.errorCode + " " + err.errorMessage + ); +} ``` -*** +--- ### connectDevice - _Use to manual connection_ - Params: +_Use to manual connection_ -|Name|TYPE|REQUIRED| -| - | - | - | -| deviceName | string | yes for call | -| baudRate | number | yes for call | +Params: + +| Name | TYPE | REQUIRED | +| ---------- | ------ | ------------ | +| deviceName | string | yes for call | +| baudRate | number | yes for call | ```javascript RNSerialport.connectDevice("deviceName", 9600); ``` -*** +--- ### disconnect - _Closes the connection_ - No Params +_Closes the connection_ + +No Params ```javascript -RNSerialport.disconnect() +RNSerialport.disconnect(); ``` -*** +--- ### isOpen - _Returns connection status_ - Params: +_Returns connection status_ + +Params: _No param_ @@ -109,133 +113,131 @@ _No param_ try { const isOpen = await RNSerialport.isOpen(); - if(isOpen) - console.log("Is open?", "yes"); - else - console.log("Is open?", "no"); - -} catch(err) { + 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); -}); +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: +_Returns support status_ + +Params: -|Name|TYPE|REQUIRED| -| - | - | - | -| deviceName | string | yes for call | +| Name | TYPE | REQUIRED | +| ---------- | ------ | ------------ | +| deviceName | string | yes for call | ```javascript //1st way try { const isSupported = await RNSerialport.isSupported("deviceName"); - if(isSupported) - console.log("Is supported?", "yes"); - else - console.log("Is supported?", "no"); - -} catch(err) { - -} + 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); -}); +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 +_Returns service status_ + +No param ```javascript //1st way try { const isServiceStarted = await RNSerialport.isServiceStarted(); - if(isServiceStarted) - console.log("Is ServiceStarted?", "yes"); - else - console.log("Is ServiceStarted?", "no"); - -} catch(err) { - -} + 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); -}); +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 | +_Writes data to serial port_ + +| Name | TYPE | REQUIRED | +| ---- | ------ | ------------ | +| data | string | yes for call | ```javascript RNSerialport.writeString("HELLO"); ``` -*** +--- ### writeBase64 - _Writes data to serial port_ - |Name|TYPE|REQUIRED| -| - | - | - | -| data | string | yes for call | +_Writes data to serial port_ + +| Name | TYPE | REQUIRED | +| ---- | ------ | ------------ | +| data | string | yes for call | ```javascript RNSerialport.writeBase64("SEVMTE8="); ``` -*** +--- ### writeHexString - _Writes data to serial port_ - `Note: Make sure the text has a valid hexadecimal number system! Otherwise this does nothing.` +_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 | +| Name | TYPE | REQUIRED | +| ---- | ------ | ------------ | +| data | string | yes for call | ```javascript RNSerialport.writeHexString("0F"); // 1 byte @@ -245,20 +247,21 @@ 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_ + +_Changes the data type in "ON_READ_DATA" event_ + > Default: INTARRAY Params: -|TYPE|REQUIRED| -| - | - | -| number | yes for call | +| TYPE | REQUIRED | +| ------ | ------------ | +| number | yes for call | ```javascript import { definitions } from "react-native-serialport @@ -268,13 +271,14 @@ RNSerialport.setReturnedDataType( // or RNSerialport.setReturnedDataType( definitions.RETURNED_DATA_TYPES.INTARRAY -) +) ``` -*** +--- ### setDriver - _Changes the driver_ + +_Changes the driver_ [Why it is necessary? Using createUsbSerialDevice method specifying the driver](https://github.com/felHR85/UsbSerial/wiki/3.-Create-UsbSerialDevice#using-createusbserialdevice-method-specifying-the-driver) @@ -282,147 +286,163 @@ RNSerialport.setReturnedDataType( Params: -|TYPE|REQUIRED| -| - | - | -| string | yes for call| +| TYPE | REQUIRED | +| ------ | ------------ | +| string | yes for call | ```javascript import { definitions } from "react-native-serialport RNSerialport.setDriver(definitions.DRIVER_TYPES.AUTO) ``` -*** +--- ### setInterface - _Changes the serial interface_ + +_Changes the serial interface_ + > Default: -1 Params: -|TYPE|REQUIRED| -| - | - | -| number | yes for call | +| TYPE | REQUIRED | +| ------ | ------------ | +| number | yes for call | ```javascript -RNSerialport.setInterface(1) +RNSerialport.setInterface(1); ``` -*** +--- ### setAutoConnectBaudRate - _Changes baud rate to be used on automatic connection_ - + +_Changes baud rate to be used on automatic connection_ + `Used before starting the service.` > Default: 9600 Params: -|TYPE|REQUIRED| -| - | - | -| number | yes for call | +| TYPE | REQUIRED | +| ------ | ------------ | +| number | yes for call | ```javascript -RNSerialport.setAutoConnectBaudRate(115200) +RNSerialport.setAutoConnectBaudRate(115200); ``` -*** +--- ### setAutoConnect - _Turns automatic connection on or off_ + +_Turns automatic connection on or off_ + > Default: off Params: -|TYPE|REQUIRED| -| - | - | -| boolean | yes for call | +| TYPE | REQUIRED | +| ------- | ------------ | +| boolean | yes for call | ```javascript -RNSerialport.setAutoConnect(true) +RNSerialport.setAutoConnect(true); ``` -*** +--- ### setDataBit - _Changes the data bit_ + +_Changes the data bit_ + > Default: DATA_BITS_8 Params: -|TYPE|REQUIRED| -| - | - | -| number | yes for call | +| TYPE | REQUIRED | +| ------ | ------------ | +| number | yes for call | ```javascript import { definitions } from "react-native-serialport RNSerialport.setDataBit(definitions.DATA_BITS.DATA_BITS_8) ``` -*** +--- ### setStopBit - _Changes the stop bit_ + +_Changes the stop bit_ + > Default: STOP_BITS_1 Params: -|TYPE|REQUIRED| -| - | - | -| number | yes for call | +| TYPE | REQUIRED | +| ------ | ------------ | +| number | yes for call | ```javascript -import { definitions } from "react-native-serialport" -RNSerialport.setStopBit(definitions.STOP_BITS.STOP_BITS_1) +import { definitions } from "react-native-serialport"; +RNSerialport.setStopBit(definitions.STOP_BITS.STOP_BITS_1); ``` -*** +--- ### setParity - _Changes the parity_ + +_Changes the parity_ + > Default: PARITY_NONE Params: -|TYPE|REQUIRED| -| - | - | -| number | yes for call | +| TYPE | REQUIRED | +| ------ | ------------ | +| number | yes for call | ```javascript -import { definitions } from "react-native-serialport" -RNSerialport.setParity(definitions.PARITIES.PARITY_NONE) +import { definitions } from "react-native-serialport"; +RNSerialport.setParity(definitions.PARITIES.PARITY_NONE); ``` -*** +--- ### setFlowControl - _Changes the flow control mode_ - > Default: FLOW_CONTROL_OFF + +_Changes the flow control mode_ + +> Default: FLOW_CONTROL_OFF Params: -|TYPE|REQUIRED| -| - | - | -| number | yes for call | +| TYPE | REQUIRED | +| ------ | ------------ | +| number | yes for call | ```javascript -import { definitions } from "react-native-serialport" -RNSerialport.setFlowControl(definitions.FLOW_CONTROLS.FLOW_CONTROL_OFF) +import { definitions } from "react-native-serialport"; +RNSerialport.setFlowControl(definitions.FLOW_CONTROLS.FLOW_CONTROL_OFF); ``` -*** +--- ### loadDefaultConnectionSetting - _Loads the default settings_ + +_Loads the default settings_ + > Defaults: > DATA_BIT: DATA_BITS_8 > STOP_BIT: STOP_BITS_1 > PARITY: PARITY_NONE -> FLOW_CONTROL: FLOW_CONTROL_OFF +> FLOW_CONTROL: FLOW_CONTROL_OFF No Params + ```javascript RNSerialport.loadDefaultConnectionSetting(); ``` -*** \ No newline at end of file +--- diff --git a/types/index.d.ts b/types/index.d.ts index e915042..f801253 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,18 +1,11 @@ -export interface GetDeviceListResponseSuccess { - status: boolean; - devices: Array; -} -export interface GetDeviceListResponseError { - status: boolean; - errorCode: number; - errorMessage: string; -} -export interface GetDeviceListResponseDevices { +export interface IDevice { name: string; vendorId: number; productId: number; } +export type Devices = Array | null; + export interface IOnReadData { pyload: string | Array } @@ -26,10 +19,6 @@ export interface IOnServiceStarted { deviceAttached: boolean } -type GetDeviceListResponse = { - [P in keyof T]: GetDeviceListResponseSuccess | GetDeviceListResponseError -}; - interface DefinitionsStatic { DATA_BITS: { DATA_BITS_5: number @@ -38,130 +27,167 @@ interface DefinitionsStatic { DATA_BITS_8: number; }; STOP_BITS: { - STOP_BITS_1 : number; + STOP_BITS_1: number; STOP_BITS_15: number; - STOP_BITS_2 : number; + STOP_BITS_2: number; }; PARITIES: { - PARITY_NONE : number; - PARITY_ODD : number; - PARITY_EVEN : number; - PARITY_MARK : number; + PARITY_NONE: number; + PARITY_ODD: number; + PARITY_EVEN: number; + PARITY_MARK: number; PARITY_SPACE: number; }; FLOW_CONTROLS: { - FLOW_CONTROL_OFF : number; - FLOW_CONTROL_RTS_CTS : number; - FLOW_CONTROL_DSR_DTR : number; - FLOW_CONTROL_XON_XOFF : number; + FLOW_CONTROL_OFF: number; + FLOW_CONTROL_RTS_CTS: number; + FLOW_CONTROL_DSR_DTR: number; + FLOW_CONTROL_XON_XOFF: number; }; RETURNED_DATA_TYPES: { - INTARRAY : number; + INTARRAY: number; HEXSTRING: number; }; DRIVER_TYPES: { - AUTO : string, - CDC : string, - CH34x : string, - CP210x : string, - FTDI : string, - PL2303 : string + AUTO: string, + CDC: string, + CH34x: string, + CP210x: string, + FTDI: string, + PL2303: string }; } export var definitions: DefinitionsStatic; interface ActionsStatic { - ON_SERVICE_STARTED : string, - ON_SERVICE_STOPPED : string, - ON_DEVICE_ATTACHED : string, - ON_DEVICE_DETACHED : string, - ON_ERROR : string, - ON_CONNECTED : string, - ON_DISCONNECTED : string, - ON_READ_DATA : string + ON_SERVICE_STARTED: string, + ON_SERVICE_STOPPED: string, + ON_DEVICE_ATTACHED: string, + ON_DEVICE_DETACHED: string, + ON_ERROR: string, + ON_CONNECTED: string, + ON_DISCONNECTED: string, + ON_READ_DATA: string } export var actions: ActionsStatic; type DataBits = 5 | 6 | 7 | 8; type StopBits = 1 | 2 | 3; -type Parities = 0 | 1 | 2 | 3 | 4; +type Parities = 0 | 1 | 2 | 3 | 4; type FlowControls = 0 | 1 | 2 | 3; type ReturnedDataTypes = 1 | 2; type Drivers = "AUTO" | "cdc" | "ch34x" | "cp210x" | "ftdi" | "pl2303"; interface RNSerialportStatic { - /** * Starts the service and Usb listener + * + * @memberof RNSerialportStatic */ startUsbService(): void; - /** * Stops the service and Usb listener + * + * @memberof RNSerialportStatic */ stopUsbService(): void; /** - * @returns status boolean on Promise + * Returns status via Promise + * + * @returns {Promise} + * @memberof RNSerialportStatic */ isOpen(): Promise - + /** - * @returns status boolean on Promise + * Returns status boolean via Promise + * + * @returns {Promise} + * @memberof RNSerialportStatic */ isServiceStarted(): Promise - + /** * Returns support status - * @param deviceName String - * @returns status boolean on Promise then method + * + * @param {string} deviceName + * @returns {Promise} + * @memberof RNSerialportStatic */ - isSupported(deviceName : string) : Promise; + isSupported(deviceName: string): Promise; //Begin setter methods /** * Set the returned data type + * + * @param {ReturnedDataTypes} type + * @memberof RNSerialportStatic */ setReturnedDataType(type: ReturnedDataTypes): void; /** * Set the interface + * + * @param {number} iFace + * @memberof RNSerialportStatic */ setInterface(iFace: number): void; /** * Set the data bit + * + * @param {DataBits} bit + * @memberof RNSerialportStatic */ setDataBit(bit: DataBits): void; /** * Set the stop bit + * + * @param {StopBits} bit + * @memberof RNSerialportStatic */ setStopBit(bit: StopBits): void; /** * Set the parity + * + * @param {Parities} parity + * @memberof RNSerialportStatic */ setParity(parity: Parities): void; /** - * Set the flow control + * Set the flow control + * + * @param {FlowControls} control + * @memberof RNSerialportStatic */ setFlowControl(control: FlowControls): void; /** * Set the auto connection baudrate + * + * @param {number} baudRate + * @memberof RNSerialportStatic */ setAutoConnectBaudRate(baudRate: number): void; /** * Set the auto connection status + * + * @param {boolean} status + * @memberof RNSerialportStatic */ setAutoConnect(status: boolean): void; /** * Set the driver type + * + * @param {Drivers} driver + * @memberof RNSerialportStatic */ setDriver(driver: Drivers): void; @@ -169,55 +195,74 @@ interface RNSerialportStatic { /** * Load the default connection settings + * + * @memberof RNSerialportStatic */ loadDefaultConnectionSetting(): void; /** - * Get the device list + * Returns the device list via Promise + * + * @returns {Promise} + * @memberof RNSerialportStatic */ - getDeviceList(callback: (response: GetDeviceListResponse) => void): void; + getDeviceList(): Promise; /** * Connect to device with device name and baud rate - * @param deviceName Device Name - * @param baudRate Baud Rate + * + * @param {string} deviceName + * @param {number} baudRate + * @memberof RNSerialportStatic */ connectDevice(deviceName: string, baudRate: number): void; /** * Closes the connection + * + * @memberof RNSerialportStatic */ disconnect(): void; /** * Writes string to port - * @param data String to write + * + * @param {string} data + * @memberof RNSerialportStatic */ writeString(data: string): void; /** * Writes Base64 string to port - * @param data Base64 string to write + * + * @param {string} data + * @memberof RNSerialportStatic */ writeBase64(data: string): void; /** * Writes hex string to port - * @param data String to write + * + * @param {string} data + * @memberof RNSerialportStatic */ writeHexString(data: string): void /** - * Hex string convert to Utf8 string - * @param intArray Array - * @return utf8 String + * Integer array convert to Utf16 string + * + * @param {Array} intArray + * @returns {string} + * @memberof RNSerialportStatic */ intArrayToUtf16(intArray: Array): string /** - * Hex string convert to Utf8 string - * @param hex String - * @retrun utf8 String + * Hex string convert to Utf16 string + * + * @param {string} hex + * @returns {string} + * @memberof RNSerialportStatic */ hexToUtf16(hex: string): string }