xseignard / cordovarduino

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

registerReadCallback not receiving any data #112

Closed seppanenjp closed 5 years ago

seppanenjp commented 5 years ago

Describe the bug When using SiLabs UART USB device with settings:

serial.requestPermission({ vid: '10c4', pid: '800a', driver: 'Cp21xxSerialDriver' });

serial.open({ baudRate: 38400, dataBits: 8, stopBits: 1, parity: 0, dtr: false, rts: false, sleepOnPause: false, });

registerReadCallback should return incoming data when card is inserted to reader. Like this: {"type":"Buffer","data":[2,232,6,0,10,15,140,104,46,49,22,3]}

registerReadCallback will send data only once if card is already in reader when function is called first time.

To Reproduce

  1. Cordova version: 8.1.2
  2. ID 10c4:800a Silicon Laboratories, Inc. SPORTident USB to UART Bridge Controller Serial: 508221
  3. Huawei P30 Pro or any other Android device

Expected behavior registerReadCallback should return data when card is in the reader and Cp21xxSerialDriver is selected.

Additional context Tested card reader with https://www.npmjs.com/package/serialport and data is received like it should when using registerReadCallback.

darran-kelinske-fivestars commented 4 years ago

Were you able to figure out why this didn't work?

seppanenjp commented 4 years ago

Well kind of yes. If I remember correctly the problem was mainly that Ionic does not detect changes if you are subscribed to registerReadCallback. You have to manually call changeDectorRef.detectChanges() to update the view when new data is coming from serial.

darran-kelinske-fivestars commented 4 years ago

I was able to get it to working with RS232 USB with the following options:

{baudRate: 9600, dtr: true, rts: true},

Full implementation

`

    serial.requestPermission(
        // if user grants permission
        function(successMessage) {
            // open serial port
            serial.open(
                {baudRate: 9600, dtr: true, rts: true},
                // if port is successfully opened
                function(successMessage) {
                    open = true;
                    // register the read callback
                    serial.registerReadCallback(
                        function success(data){
                        var receivedMessage = "";
                            // decode the received message
                            var view = new Uint8Array(data);

                             if (view.length >= 1) {

                                                                for (var j = 0; j < view.length; j++) {

                                                                    var temp_str2 = String.fromCharCode(view[j]);
                                                                    var str_esc2 = escape(temp_str2);

                                                                    receivedMessage += str_esc2;

                                                                }

                                                            }
                                                            console.log(receivedMessage);

                        },
                        // error attaching the callback
                        errorCallback
                    );
                },
                // error opening the port
                errorCallback
            );
        },
        // user does not grant permission
        errorCallback
    );

`