xseignard / cordovarduino

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

CH340 driver not working #44

Closed randynwalsh closed 8 years ago

randynwalsh commented 8 years ago

Sorry, I'm new to this, and may be asking this in the wrong place. I'd like to use this plugin, however I am only familiar with cordova's phonegap build, which requires plugins put on npm. Any chance someone who knows how to package this plugin using plugman? and can put it on npm? Would be much appreciated.

https://cordova.apache.org/announcements/2015/04/21/plugins-release-and-move-to-npm.html

xseignard commented 8 years ago

here we are! https://www.npmjs.com/package/cordovarduino

please let me know if it is ok for you!

regards

randynwalsh commented 8 years ago

Thanks Xavier,

I was able to load the plugin, but when I attempt to do a serial.requestPermission, I get an error "No device found!"

I am attempting to connect to a nano arduino. The version I purchased uses a CH340G instead of the FT232RL. I think this may be the issue. Other Android program (Serial I/O programs) work, but most fail.. That is, some see the Serial connection and some don't. Is this something that can be fixed? Any hints?

On Wed, Nov 18, 2015 at 12:18 PM, Xavier Seignard notifications@github.com wrote:

here we are! https://www.npmjs.com/package/cordovarduino

please let me know if it is ok for you!

regards

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-157827741 .

xseignard commented 8 years ago

Try to open it with vid/pid, see https://github.com/xseignard/cordovarduino#your-device-is-not-yet-known

It's a real challenge to debug such plugin because I cannot reproduce the bugs users have since I don't have their hardware...

randynwalsh commented 8 years ago

After playing with this for a while, I was able to get the request to work with the vid/pid. However, my application crashes when I attempt the open. Get the phoega message "Unfortunately the application has stopped". I've encased the open in a try..catch but even that doesn't catch the error. I'm guessing the error is down in java somewhere and out of reach of the javascript. Again, any ideas?

Not to worry if you don't. I'm going to try communicating with the bluetooth and see if that works.

Thanks again,

Randy

On Wed, Nov 18, 2015 at 1:36 PM, Xavier Seignard notifications@github.com wrote:

Try to open it with vid/pid, see https://github.com/xseignard/cordovarduino#your-device-is-not-yet-known

It's a real challenge to debug such plugin because I cannot reproduce the bugs users have since I don't have their hardware...

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-157855621 .

xseignard commented 8 years ago

Could you show me your code?

randynwalsh commented 8 years ago

Hi Xavier,

Yes, of course. I've attached boh the ZIP file I upload to http://build.phonegap.com, and the resultant APK ( with the extensions removed to get through email )

my config.xml:

<?xml version="1.0" encoding="utf-8" ?> <widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap=" http://phonegap.com/ns/1.0" id="com.phonegap.iframe" versionName="Beta" versionCode="3" >

Serial Bug _My html file:_ Serial Bug On Thu, Nov 19, 2015 at 6:36 AM, Xavier Seignard notifications@github.com wrote: > Could you show me your code? > > — > Reply to this email directly or view it on GitHub > https://github.com/xseignard/cordovarduino/issues/44#issuecomment-158058483 > .
xseignard commented 8 years ago

Hello @randynwalsh I never tried to use phonegap build Did you try cordova?

randynwalsh commented 8 years ago

Hi Xavier,

Well... it took me most of the day to install and understand cordova from the command line, and git. But I got it all installed, and built the apk. Same result :(

My tablet is Android 5.1.1, and my cell phone is the same version. So, I wondering if it might be something in this version of Android. Don't know

On Mon, Nov 30, 2015 at 7:43 AM, Xavier Seignard notifications@github.com wrote:

Hello @randynwalsh https://github.com/randynwalsh I never tried to use phonegap build Did you try cordova?

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-160649392 .

xseignard commented 8 years ago

Hello @randynwalsh, I hope the learning curve for cordova is not too steep! Glad to hear you managed to use it. Could you try the following code?

www/index.js:

var app = {
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {
        var potText = document.getElementById('pot');
        var delta = document.getElementById('delta');
        var on = document.getElementById('on');
        var off = document.getElementById('off');
        var open = false;
        var str = '';
        var lastRead = new Date();

        var errorCallback = function(message) {
            alert('Error: ' + message);
        };
        // request permission first
        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
        );

        on.onclick = function() {
            console.log('click');
            if (open) serial.write('1');
        };
        off.onclick = function() {
            if (open) serial.write('0');
        }
    }
};

app.initialize();

then this as your index.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <div class="app">
            <h1>Potentiometer value</h1>
            <p>Value <span id="pot">...</span></p>
            <p id="delta">...</p>
            <button id="on">On</button>
            <button id="off">Off</button>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

and this as your arduino code, with a potentiometer on A0 and a led on 13

#define POT A0
#define LED 13

unsigned long previousMillis;
int interval = 50;

void setup() {
    Serial.begin(9600);
    pinMode(POT, INPUT);
    pinMode(LED, OUTPUT);
}

void loop() {
    if (Serial.available() > 0) {
        char i = Serial.read();
        switch (i) {
            case '0':
                digitalWrite(LED, LOW);
                break;
            case '1':
                digitalWrite(LED, HIGH);
                break;
        }
    }
    if (millis() - previousMillis >= interval) {
        previousMillis = millis();
        int value = analogRead(POT);
        Serial.println(value);
    }
}

It's the 'reference' example, let's check if it works.

Regards

randynwalsh commented 8 years ago

Hi Xavier,

Tied your code... same problem. I also tried the same code on another tablet with Android 4.4.2. Same result.

I'm using an Arduino Nano. I've ordered an Arduino Uno. When it arrives I'll try it. Other than that, maybe it's something in the config.xml?

<?xml version='1.0' encoding='utf-8'?>

HelloWorld
<description>
    A sample Apache Cordova application that responds to the

deviceready event.

Apache Cordova Team
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<plugin name="cordovarduino" />

On Tue, Dec 1, 2015 at 2:55 AM, Xavier Seignard notifications@github.com wrote:

Hello @randynwalsh https://github.com/randynwalsh, I hope the learning curve for cordova is not too steep! Glad to hear you managed to use it. Could you try the following code?

var app = { initialize: function() { document.addEventListener('deviceready', this.onDeviceReady, false); }, onDeviceReady: function() { var potText = document.getElementById('pot'); var delta = document.getElementById('delta'); var on = document.getElementById('on'); var off = document.getElementById('off'); var open = false; var str = ''; var lastRead = new Date();

    var errorCallback = function(message) {
        alert('Error: ' + message);
    };
    // request permission first
    serial.requestPermission(
        { vid: '1a86', pid: '7523' },
        // 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
    );

    on.onclick = function() {
        console.log('click');
        if (open) serial.write('1');
    };
    off.onclick = function() {
        if (open) serial.write('0');
    }
}

}; app.initialize();

then this as your index.html

<!DOCTYPE html>

Hello World

Potentiometer value

Value ...

...

and this as your arduino code, with a potentiometer on A0 and a led on 13

define POT A0

define LED 13

unsigned long previousMillis;int interval = 50; void setup() { Serial.begin(9600); pinMode(POT, INPUT); pinMode(LED, OUTPUT); } void loop() { if (Serial.available() > 0) { char i = Serial.read(); switch (i) { case '0': digitalWrite(LED, LOW); break; case '1': digitalWrite(LED, HIGH); break; } } if (millis() - previousMillis >= interval) { previousMillis = millis(); int value = analogRead(POT); Serial.println(value); } }

It's the 'reference' example, let's check if it works.

Regards

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-160917573 .

xseignard commented 8 years ago

Hello @randynwalsh I'm affraid that CH340G ain't supported. To be sure I uploaded a starter project with cordovarduino here: https://github.com/xseignard/demoCordovarduino it is tested with Arduino Uno and Arduino Mega.

What would be nice is to list known devices to work with this project. I'll give a try with a Leonardo, Duemilanove, Yun and FTDI cable to extend the knowledge of working/non working devices.

Thanks for being patient!

Regards

xseignard commented 8 years ago

See https://github.com/mik3y/usb-serial-for-android/pull/92

xseignard commented 8 years ago

I'm in touch with the dev of the underlying android lib, let's hope the merge of CH340G drivers is coming soon!

randynwalsh commented 8 years ago

I got my device with the FTDI chip. It works great. So, pretty sure the problem is with the CH340. Hope the CH340 drivers are coming soon too!

On Thu, Dec 3, 2015 at 2:23 AM, Xavier Seignard notifications@github.com wrote:

I'm in touch with the dev of the underlying android lib, let's hope the merge of CH340G drivers is coming soon!

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-161561814 .

xseignard commented 8 years ago

Gld to hear you're not stuck! Did it work with phonegap too? I'll be glad to know.

Happy coding :pig_nose:

randynwalsh commented 8 years ago

Yes. Phonegap build works On Dec 4, 2015 4:20 PM, "Xavier Seignard" notifications@github.com wrote:

Gld to hear you're not stuck! Did it work with phonegap too? I'll be glad to know.

Happy coding [image: :pig_nose:]

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-162108202 .

randynwalsh commented 8 years ago

Hi Xavier,

I happened to stumble across something that might be a fix for this CH341 issue. A guy in a google group says that changing the value of REQTYPE_HOST_TO_DEVICE to 0x40 fixes the CH341 problem, this value in the usb-serial-for-android code Cp21xxSerialDriver.java

I thought it might be worth changing this value from 0x41 to 0x40 and see what happens. What do you think?

Here is link what the guys says: https://groups.google.com/forum/#!topic/usb-serial-for-android/KrcTEldNO9Y

On Fri, Dec 4, 2015 at 5:54 PM, Randy Walsh randynwalsh@gmail.com wrote:

Yes. Phonegap build works On Dec 4, 2015 4:20 PM, "Xavier Seignard" notifications@github.com wrote:

Gld to hear you're not stuck! Did it work with phonegap too? I'll be glad to know.

Happy coding [image: :pig_nose:]

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-162108202 .

xseignard commented 8 years ago

So I added the CH340 driver. Could you try the v0.0.4 with the code I gave you here https://github.com/xseignard/cordovarduino/issues/44#issuecomment-160917573?

Let me know! I tried myself since I just found a CH340 based arduino nano in my mess, and it works OK.

Regards

randynwalsh commented 8 years ago

Awesome. I'll send you a beer via paypal!

Thanks for getting this working for me.

Randy

On Tue, Dec 8, 2015 at 6:40 PM, Xavier Seignard notifications@github.com wrote:

So I added the CH340 driver. Could you try the v0.0.4 with the code I gave you?

Since the driver is implemented, you need to remove { vid: '1a86', pid: '7523' }, from the code.

Let me know! I tried myself since I just found a CH340 based arduino nano in my mess, and it works OK.

Regards

— Reply to this email directly or view it on GitHub https://github.com/xseignard/cordovarduino/issues/44#issuecomment-163079148 .

xseignard commented 8 years ago

Wow that's very appreciated! Be sure I'll port a toast to you when I'll drink this beer! :beers: Thank you!

I hope you'll have fun creating your app, let me know if you face other issues.

Thanks for the feedback and the beer :)