WiringPi 2.52 and Pi4J 1.2

62 views
Skip to first unread message

greg....@gmail.com

unread,
Sep 25, 2020, 5:38:40 PM9/25/20
to Pi4J
I am using this combination on a Pi3B+. GPIO seems to work fine. Serial communication to an Arduino via USB seems to work, but slowly, almost 50x what the jSerialComm obtains for the same interaction. 

I decided to "drop down" to the wiringPi level in Pi4J to try the same thing -- write 3 bytes and then read 2 bytes. Here is the major parts of the code.

             import com.pi4j.wiringpi.Serial;          
            final int fd = Serial.serialOpen("/dev/ttyACM1", 115200);
            Serial.serialFlush(fd);
            for (int i = 0; i < num; i++) {
                System.out.println("writing");
                long start = System.nanoTime();
                byte[] buffer = new byte[3];
                buffer[0] = (byte) 11;
                buffer[1] = (byte) (6 + 2 * i);
                buffer[2] = (byte) (8 + 4 * i);
                Serial.serialPutBytes(fd, buffer);
                System.out.println("waiting ....");
                while (Serial.serialDataAvail(fd) != 2) {
                    Thread.sleep(1);
                }
                byte[] res = Serial.serialGetBytes(fd, 2);
                long stop = System.nanoTime();
                for (byte b : res) {
                    System.out.println("got a byte: " + b);
                }
            }

On occasion, the first iteration of the loop will work and then hang at "waiting" in the second iteration. Sometimes it hangs at the first iteration "waiting". Are there any examples around?

I must be doing something wrong, but I can't determine what.

I have another, somewhat related, question. The javadoc for com.pi4j.wiringpi.Serial talks about file handles and "standard read(), write(), etc. system calls". That would seem to make sense if you are writing in C, but I'm not sure what that means in Java; maybe something to do with Runtime? I've searched and found nothing.

Any help appreciated.
Reply all
Reply to author
Forward
0 new messages