Java-Arduino-Communication-Library icon indicating copy to clipboard operation
Java-Arduino-Communication-Library copied to clipboard

raspberry pi serialRead doesn't work

Open qweasd7924 opened this issue 7 years ago • 2 comments

Hello, I have same error, link below

https://www.raspberrypi.org/forums/viewtopic.php?t=223658

I call serailRead(int x) and after thap app is looping forever(serialWrite work fine)

    Arduino a = new Arduino("/dev/ttyUSB0",115200);
    boolean isOpen = a.openConnection();
    Thread.sleep(1500);
    if (isOpen) {
        System.out.println("Start,led on");
        a.serialWrite('1');
        System.out.println(a.serialRead());
        wait1(1000);
        a.serialWrite('0');
        wait1(1000);
        a.closeConnection();
    }else {
        System.out.println("No connection");
    }

qweasd7924 avatar Nov 28 '19 22:11 qweasd7924

hi @qweasd7924! Thanks for your question. It seems like in your code snippet, you use a.serialRead() not a.serialRead(int x). As warned in the instructions, its possible for a.serialRead() to enter into an infinite loop.

Please use a.serialRead(int x) - it is implemented in a semi_blocking manner that ensures that the thread will wait for AT LEAST ONE character and will read at most x characters in that function call.

Thanks also for linking the discussion thread on Raspberry Pi forums - I understand that a read+timeout function is definitely missing in my library - I'm considering adding such functionality when I have some free time - also happy to accept Pull Requests with the same!

Let me know if you have any other questions!

HirdayGupta avatar Dec 13 '19 04:12 HirdayGupta

Thank you. I very often made a two ways communication . So it solves my problem.

azaslavskis avatar Jan 14 '20 15:01 azaslavskis