xseignard / cordovarduino

Cordova/Phonegap plugin for USB host serial communication from an Android device.
MIT License
166 stars 110 forks source link

Partial data missing. #88

Closed gitsad closed 6 years ago

gitsad commented 6 years ago

Hi,

I've got a little problem, but first here is my code:

cordova code

 concatData = (data) => {
        this.setState({
            arrayMessage: this.state.arrayMessage.concat(data)
        });

    };
    removeData = () => {
        this.setState({
            arrayMessage: []
        });
    };
    sendDataToReducer = () => {
        this.props.addDataFromArduino(this.state.arrayMessage.join(""));
    };
    connect = () => {
        const errorCallback = (message) => alert('Error: ' + message);
        serial.requestPermission(
            (successMessage) => {
                if (!'TextDecoder' in window) {
                    alert('Your browser does not support the Encoding API.');
                }
                alert(successMessage);
                serial.open(
                    {
                        baudRate: 9600
                    },
                    () => {
                        serial.registerReadCallback(
                            (data) => {
                                const view = new Uint8Array(data);
                                const enc = new TextDecoder();
                                const decodedString = enc.decode(view);
                                if(decodedString.length > 1) {
                                    for(let i = 0; i < decodedString.length; i++) {
                                        if(parseInt(decodedString[i].charCodeAt(0)) !== 13) {
                                            this.concatData(decodedString[i]);
                                        } else {
                                            this.sendDataToReducer();
                                            this.removeData();
                                        }
                                    }
                                } else {
                                    if(parseInt(decodedString[i].charCodeAt(0)) !== 13) {
                                        this.concatData(decodedString);
                                    } else {
                                       this.sendDataToReducer();
                                       this.removeData();
                                    }
                                }
                            },
                            function error(){
                                new Error("Failed to register read callback");
                            });
                    },
                    errorCallback
                );
            },
            errorCallback
        )
    };

arduino code

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(2000);
  Serial.println(12123);
}

The problem is that when my android is receiving data, some of this data is missing Screenshot below: issue

As you can see, in this picture the data is the number 12123. The string initial data is hardcoded string in an array to which I push received data.

At the beginning after "initial data" you can notice(I underlined this with red line) that there is no 1 before2 which means that somehow the first digit missed.

I thought that is little issue with which I can live but after receiving another same data(12123), the problem appeared again in different combination. For instance 213 which I underlined with a green line and other like 2123(before that 213) or 212(in the end).

I tried to resolve this but I ended without success. If someone knows how to fix it, I will be grateful to that person for helping me.

gitsad commented 6 years ago

Ok I resolved this by changing this condition: decodedString.length > 1 into: decodedString.length >= 1

xseignard commented 6 years ago

Hello @gitsad it seems logical to me to use >= instead of > because with the latter you would miss messages of length === 1. Does it fixed both issues (underlined in green and in red) ?

Should I consider this issue fixed? I'm curious for what you're using this project, could you tell me more?

Regards

gitsad commented 6 years ago

Thank you for your response. Yeah, I've fixed it by changing this condition above. Both issues are fixed because they were connected with this missing partial data :smile: .

I'm building small measure device. This project includes small voltage detector, Arduino board, and mobile app in cordova+react+onsen to read and save data. I was little unsure when it came to building a mobile app because my second option was to build an app in react native but my hardware(Arduino) knowledge isn't as big as I want and also I didn't find library in React Native which would be directly related to connecting Arduino with mobile app :thinking: . For now I'm glad that I chose cordova+react apart from little problems which I had. Your lib helps me a lot :+1:

xseignard commented 6 years ago

Great to hear! I have in mind to port this to react native, but I cannot find the time!

Have fun!