ksksue / PhysicaloidLibrary

Android Library for communicating with physical-computing boards (e.g.Arduino, mbed)
http://www.physicaloid.com/
357 stars 151 forks source link

mPhysicaloid.open() never open #25

Open lucas-pedroso-developer opened 8 years ago

lucas-pedroso-developer commented 8 years ago

I start use this library in my project two months ago, it worked very well, but yesterday the library stop work, in this condition "if(mPhysicaloid.open())" don't open, i don't know why this happen.

My manifest code

<uses-feature android:name="android.hardware.usb.host" />

<activity
        android:name=".view.MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>
        <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
            android:resource="@xml/device_filter" />
    </activity>

My Activity code

 mPhysicaloid = new Physicaloid(this);
    if (mPhysicaloid.open()) {
        Toast.makeText(context, "USB Conection Open", Toast.LENGTH_LONG).show();
        String str = dataToSend + "\r\n";
        byte[] buf = str.getBytes();
        mPhysicaloid.write(buf, buf.length);
    } else {
        Toast.makeText(context, "Error Open USB Conection", Toast.LENGTH_LONG).show();
    }
HarveyHepburn commented 7 years ago

I have the same issue.

xxxajk commented 7 years ago

Try mine, it actually works ;-) https://github.com/xxxajk/PhysicaloidLibrary

cpbridges commented 7 years ago

@xxxajk Could you please turn it into a .jar file with your ammendments? Many thanks!

xxxajk commented 7 years ago

Certainly! I'll do that some time later today.

On Tue, Mar 28, 2017 at 7:32 PM, Chris notifications@github.com wrote:

@xxxajk https://github.com/xxxajk Could you please turn it into a .jar file with your ammendments? Many thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ksksue/PhysicaloidLibrary/issues/25#issuecomment-289936715, or mute the thread https://github.com/notifications/unsubscribe-auth/ADskzLtVSsQU6Shw_59c1yrl5Vlewn6Uks5rqZiTgaJpZM4KjcHp .

-- Visit my github for awesome Arduino code @ https://github.com/xxxajk

cpbridges commented 7 years ago

Thanks @xxxajk - I've build an android project that uses your library as a module and I'm getting some odd behaviour. I've a bunch of odd behaviours that I hope you (or someone could help with):

1: The readSize often returns zero? So I hit a read button and immediately TextView some values. Why is this? If we hit read again, it comes up with the data fine.

2: I've taking in ascii that I'm using scanner to convert to doubles to use in GraphView - the scanner works fine, but double conversion fails (!). Any ideas?

Happy to share code: essentially, it take Arduino uno data to a Samsung tablet to read/view ascii, then plot via GraphView. Thoughts/comments/insults welcome! C

MainActivity.java.txt

xxxajk commented 7 years ago

< 0 should be an error 0 == no bytes and > 0 == number of bytes that came in

cpbridges commented 7 years ago

Thanks - So is it usual to have to run the method twice to get data out? We're at 9600 baud as we find that bits are often missed at higher rates, and we can see the LEDs light up saying that data is coming. But perhaps it's triggering filling a buffer, and it doesn't get 'transferred' until the next read. Not sure.

I fixed point 2, using String[] s = str.split(","); etc.

xxxajk commented 7 years ago

What you may want to do is run the data gathering in a separate thread at a low priority that sleeps for 1ms, and has a way to be told to quit. While it loops you store data as you need it someplace, perhaps a queue, or a structure, or whatever you need. For a structure, when you have the amount of data you want, you can wake up the task to tell it so, or set a flag, or whatever. That is how I use it for structured data.

Yes it is normal for it to return no data-- obviously it has not arrived yet, so there is nothing to read.

cpbridges commented 7 years ago

I see. Is there a sample you could share on how that should work? That would be an excellent addition to your fork! It takes 4s-20s (worst case) to complete collection. Again, super helpful! C

xxxajk commented 7 years ago

Sure, here's something brief. It has the nice advantage that you get a double buffer, which helps prevent data loss. Let's pretend you want 3 bytes at a time...

import com.physicaloid.lib.usb.driver.uart.ReadLisener;
import com.physicaloid.misc.RingBuffer;

private RingBuffer mBuffer = new RingBuffer(1024); // Adjust to your needs
private ReadLisener ReadL;

then in your class...

        public boolean check_data() {
                int i = mBuffer.getBufferdLength();
                if(i > 2) {
                        byte[] buf = new byte[3];
                        mBuffer.get(buf, 3);
                        // do something with the three bytes...
                        // This allows the target to tell us to close.
                        // if(buf == some kind of pattern for exit) return true;
                }
                return false; // keep running.
        }
        public Runnable _check_data = new Runnable() {
                @Override
                public void run() {
                        // Don't hog the CPU. Only run on Idle time.
                        // This nice trick gives this thread the lowest possible
                        // priority. You may or may not want to raise it a little bit,
                        // but that depends on how much CPU you want to waste in this thread.
                        android.os.Process.setThreadPriority(-20);
                        do {
                                if(check_data()) {
                                        // Exit this thread
                                        break;
                                }
                        } while(ReadL != null);
                }
        };

After serial is started, perhaps in your onCreate() or someplace else...

        ReadL = new ReadLisener() {
                public void onRead(int i) {
                        byte[] buf = new byte[i];
                        mSerial.read(buf, i);
                        if((mBuffer.getBufferdLength() + i) > mBuffer.getRingBufferSize()) {
                                i = mBuffer.getRingBufferSize() - mBuffer.getBufferdLength();
                        }
                        if(i > 0) {
                                mBuffer.add(buf, i);
                        }
                }
        };

        mSerial.addReadListener(ReadL);
        new Thread(_check_data).start();

Two ways that this shuts down the thread:

  1. MCU sends a special message.
  2. ReadL becomes NULL

HTH 8-)

ahmedbejaoui commented 7 years ago

Hey guys, I used this library in my project , it worked very well with arduino Uno , but this time I used the arduino mini pro 5V 328 `mPhysicaloid = new Physicaloid(this); mPhysicaloid.upload(Boards.ARDUINO_PRO_5V_328, "/sdcard/arduino/Blink.hex"); mPhysicaloid.setBaudrate(57600); if(mPhysicaloid.open() ) {

mPhysicaloid.addReadListener(new ReadLisener() {
    @Override
    public void onRead(int size) {
        byte[] buf = new byte[size];
        mPhysicaloid.read(buf, size);
    }
});

},` but connection won't open, I installed other applications and i can read the output of the arduino mini but i can't in my app, i don't know why this happen.

matheusfillipe commented 6 years ago

@xxxajk could you please post the link for your .jar or give more details of how to use that library with Android studio?

xxxajk commented 6 years ago

https://github.com/xxxajk/PhysicaloidLibrary

There will be more interesting improvements coming up, such as connection via ESP8266. That will give you OTA on android to ESP8266 and/or/arduino connected to ESP.

matheusfillipe commented 6 years ago

Nice @xxxajk I appreciate this nice job but isn't there a packet .jar file that i can use on Android studio? I am not really skilled with java and I a unsure of how to import this library there if not by dragging the jar to the app libs folder. I found this d2xx.jar there but I'm not sure if it is the right jar.

Sorry for the newbie questions, I am aware i could simply make my own .jar somehow or just use the way it is there possibly.

xxxajk commented 6 years ago

Well, first off, I never use android studio. From my experience, you should be able to add it as a library project and it should build when you build your android project.