Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read data from serial #7

Open
nightmareindustries opened this issue Sep 22, 2017 · 7 comments
Open

Read data from serial #7

nightmareindustries opened this issue Sep 22, 2017 · 7 comments

Comments

@nightmareindustries
Copy link

Hello,

Are you planning on implementing serial read as well? It would make the library extremely useful and complete.

Thanks for your great work

@stefan-beyer
Copy link

How about just giving the port.read() method a fixed buffer? Or we specify the buffersize via param.
The following code is completely untested:

    private static final int READ_BUFFER_SIZE = 256;
    ...
    public void readAsync(Promise promise) {
        if (port != null) {
            byte[] buffer = new byte[READ_BUFFER_SIZE];
            try {
                int bytesRead = port.read(buffer, SERIAL_TIMEOUT);
                String byteString = new String(buffer, 0, bytesRead);
                promise.resolve(byteString);
            } catch (IOException e) {
                promise.reject(e);
            }
        } else {
            promise.resolve(getNoPortErrorMessage());
        }
    }

@wil93
Copy link

wil93 commented Jun 8, 2018

I studied the source code of this project a little bit and it looks like, for writing data, it just calls this:

return this.UsbSerialModule.writeInDeviceAsync(this.id, value);

Is it much harder to change this to 'read'? What exactly needs to be done to support reading from serial?

@gabrielhpugliese
Copy link

Hi guys, have anyone implemented it already? I'm willing to pay for it if needed.

@GreenGI
Copy link

GreenGI commented Jul 31, 2018

Hey, yeah, we have a working fork for reading data. Nothing pretty, but it gets the job done:
https://github.com/edvijaka/react-native-usbserial

@gabrielhpugliese
Copy link

Hey @GreenGI thank you.
Is this usage correct?

        DeviceEventEmitter.addListener('newData', (e) => {
          console.log('newData', e);
        });

@GreenGI
Copy link

GreenGI commented Aug 1, 2018

Basically - yes. This is how I use it on a component after mounting it:

componentWillMount() {
  this.getDeviceAsync().then(device => {
    if (device) {
      DeviceEventEmitter.addListener('newData', (e) => {
        console.log('newData', e);
      });
      this.readDevice(device.id);
    }
  });
}

getDeviceListAsync = () => {
  return UsbSerialModule.getDeviceListAsync();
};

readDevice = (id) => {
  UsbSerialModule.readDeviceAsync(id);
};

@omatrot
Copy link

omatrot commented Aug 26, 2019

Hi @GreenGI !
Is the code above still working in your fork?
Could you provide the code for getDeviceAsync()?
Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants