Skip to content

Commit 0ded250

Browse files
committed
Specify readBufferSize in open method
1 parent 0506c0a commit 0ded250

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

android/src/main/java/com/bastengao/serialport/SerialPortApiModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void getSuPath(final Callback callback) {
6565
}
6666

6767
@ReactMethod
68-
public void open(final String path, int baudRate, int parity, int dataBits, int stopBits, Promise promise) {
68+
public void open(final String path, int baudRate, int parity, int dataBits, int stopBits, int readBufferSize, Promise promise) {
6969
if (serialPorts.containsKey(path)) {
7070
promise.resolve(serialPorts.get(path).toJS());
7171
return;
@@ -85,7 +85,7 @@ public void remove() {
8585
.stopBits(stopBits)
8686
.build();
8787

88-
SerialPortWrapper wrapper = new SerialPortWrapper(path, serialPort, this, remover);
88+
SerialPortWrapper wrapper = new SerialPortWrapper(path, readBufferSize, serialPort, this, remover);
8989

9090
// TODO: handle previous value
9191
serialPorts.put(path, wrapper);

android/src/main/java/com/bastengao/serialport/SerialPortWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class SerialPortWrapper {
2626

2727
private AtomicBoolean closed = new AtomicBoolean(false);
2828

29-
public SerialPortWrapper(String path, SerialPort serialPort, final EventSender sender, Remover remover) {
29+
public SerialPortWrapper(final String path, final int readBufferSize, SerialPort serialPort, final EventSender sender, Remover remover) {
3030
this.path = path;
3131
this.serialPort = serialPort;
3232
this.sender = sender;
@@ -37,7 +37,7 @@ public SerialPortWrapper(String path, SerialPort serialPort, final EventSender s
3737
this.readThread = new Thread(new Runnable() {
3838
@Override
3939
public void run() {
40-
byte[] buffer = new byte[64];
40+
byte[] buffer = new byte[readBufferSize];
4141
while (!closed.get()) {
4242
try {
4343
int size;

src/API.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default class API {
2222
* @property {number} [parity=0] 0: none, 1: odd, 2: even
2323
* @property {number} [dataBits=8] 5~8
2424
* @property {number} [stopBits=1] 1 or 2
25+
* @property {number} [readBufferSize=64] set read buffer size
2526
*/
2627

2728
/**
@@ -77,9 +78,9 @@ export default class API {
7778
* @param {openOptions} options
7879
* @returns {Promise<SerialPort>} connected serial port
7980
*/
80-
static open(devicePath: string, {baudRate, parity = 0, dataBits = 8, stopBits = 1}: { baudRate: number, parity?: number, dataBits?: number, stopBits?: number }): Promise<SerialPort> {
81+
static open(devicePath: string, {baudRate, parity = 0, dataBits = 8, stopBits = 1, readBufferSize = 64}: { baudRate: number, parity?: number, dataBits?: number, stopBits?: number, readBufferSize?: number }): Promise<SerialPort> {
8182
if (Platform.OS !== 'android') throw new Error(`Not support ${Platform.OS}`)
82-
return SerialPortAPI.open(devicePath, baudRate, parity, dataBits, stopBits)
83+
return SerialPortAPI.open(devicePath, baudRate, parity, dataBits, stopBits, readBufferSize)
8384
.then((serialPort: SerialPortWrapper) => {
8485
return Promise.resolve(new SerialPort(serialPort, eventEmitter!));
8586
})

0 commit comments

Comments
 (0)