xseignard / cordovarduino

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

serial.registerReadCallback With USB Modem via AT Commands #121

Open Mamdouh-Freelancer opened 4 years ago

Mamdouh-Freelancer commented 4 years ago

Hello all authors,

I tried to connect USB modem to send AT Commands and the problem I'm facing that when I use serial.registerReadCallback after serial.open it's receiving unlimited of buffers (app crashed after few seconds) and all of zeros [0,0,0,0,0,0,0,0,0] witch nothing after decoded and I don't know what's wrong with that.

What/when is the right way to use serial.registerReadCallback and serial.read? What's the right way to handle this unusual received buffers?

Note: AT Commands tested on PC and working well.

Simple AT Commands && also tried without \r\n serial.write('AT+CMGF=0\r\n'); // set modem to PDU mode
serial.write('AT+COPS?\r\n'); // get Operator Name

Same as:

 serial.requestPermission(
            // if user grants permission
            function(successMessage) {
                // open serial port
                serial.open(
                    {baudRate: 9600},
                    // if port is succesfuly opened
                    function(successMessage) {
                        open = true;
                        // register the read callback
                        serial.registerReadCallback(
                            function success(data){
                                // decode the received message
                                var view = new Uint8Array(data);
                                if(view.length >= 1) {
                                    for(var i=0; i < view.length; i++) {
                                        // if we received a \n, the message is complete, display it
                                        if(view[i] == 13) {
                                            // check if the read rate correspond to the arduino serial print rate
                                            var now = new Date();
                                            delta.innerText = now - lastRead;
                                            lastRead = now;
                                            // display the message
                                            var value = parseInt(str);
                                            pot.innerText = value;
                                            str = '';
                                        }
                                        // if not, concatenate with the begening of the message
                                        else {
                                            var temp_str = String.fromCharCode(view[i]);
                                            var str_esc = escape(temp_str);
                                            str += unescape(str_esc);
                                        }
                                    }
                                }
                            },
                            // error attaching the callback
                            errorCallback
                        );
                    },
                    // error opening the port
                    errorCallback
                );
            },
            // user does not grant permission
            errorCallback
        );