Serial port on raspberry pi 4 not working.

366 views
Skip to first unread message

John Gaby

unread,
Aug 1, 2020, 8:04:06 PM8/1/20
to Pi4J
I am trying to use the serial port on a raspberry pi 4. When I do, the program crashes as follows. I have the latest version of wiringpi and pi4j. Note that this code works on a raspberry pi 3. Any help would be appreciated.

java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.ah.java.remotevmlauncher.launcher.Launcher.startApplication(Launcher.java:519)
at org.ah.java.remotevmlauncher.launcher.Launcher$4.run(Launcher.java:280)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: com.pi4j.io.gpio.exception.UnsupportedBoardType: Unsupported/unknown board type; unable to detect hardware.
at com.pi4j.io.serial.SerialPort.getDefaultPort(SerialPort.java:108)
at com.pi4j.io.serial.SerialPort.getDefaultPort(SerialPort.java:49)
at robotCore.ArduinoConnection.Start(ArduinoConnection.java:583)
at robotCore.ArduinoConnection.GetInstance(ArduinoConnection.java:192)
at robotCore.RobotBase.<init>(RobotBase.java:382)
at robotCore.IterativeRobot.<init>(IterativeRobot.java:43)
at robot.Robot.<init>(Robot.java:16)
at robot.Robot.main(Robot.java:110)
... 7 more

GeertVc

unread,
Aug 2, 2020, 1:36:45 AM8/2/20
to Pi4J
Means you're using wiringPi 2.52 and Pi4J develop/2.0?  If not, pls. specify...

Never used serial port on whatever RPi, but when looking in the code of the SerialPort.java (even for other branches than Pi4J 2.0) you can see there's no support (yet?) for the RPi 4 board.  Here's the code for Pi4J 2.0 (but as I said before, other branches have similar code):

    public static String getDefaultPort(SystemInfo.BoardType board) throws UnsupportedBoardType {
        switch (board){
            // ------------------------
            // ALL RASPBERRY PI MODELS
            // (except Model 3B)
            // ------------------------
            case RaspberryPi_A:
            case RaspberryPi_B_Rev1:
            case RaspberryPi_B_Rev2:
            case RaspberryPi_A_Plus:
            case RaspberryPi_B_Plus:
            case RaspberryPi_ComputeModule:
            case RaspberryPi_2B:
            case RaspberryPi_Zero:
            case RaspberryPi_ComputeModule3:
            case RaspberryPi_ZeroW:
            case RaspberryPi_Alpha:
            case RaspberryPi_Unknown: {
                return RaspberryPiSerial.DEFAULT_COM_PORT;
            }

            // ---------------------------
            // RASPBERRY PI MODEL 3B, 3B+
            // ---------------------------
            case RaspberryPi_3B:
            case RaspberryPi_3B_Plus: {
                // if the /dev/ttyS0 port exists, then use it as the default serial port
                File s0ComPort = new File(RaspberryPiSerial.S0_COM_PORT);
                if((s0ComPort.exists())){
                    return RaspberryPiSerial.S0_COM_PORT;
                }
                return RaspberryPiSerial.DEFAULT_COM_PORT;
            }

            // ------------------------
            // UNKNOWN
            // ------------------------
case UNKNOWN:
break;
default:
break;
        }

        // unknown board type, return null
        throw new UnsupportedBoardType();
}

AFAICS, for a RPi4 B board, the code will indeed throw an "UnsupportedBoardType()" exception (last statement in the above code, since no other condition before is matching) which is defined in com/pi4j/io/gpio/exception/UnsupportedBoardType.java with the following defined:

public class UnsupportedBoardType extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public UnsupportedBoardType() {
        super("Unsupported/unknown board type; unable to detect hardware.");
    }
}


Exactly what you see.

PS: I couldn't find any branch in the Pi4J Git repo that has a call to getDefaultPort() at lines 49 and 108...  Unless you added stuff yourself into that file, it's important to mention what Pi4J branch you use(d).

John Gaby

unread,
Aug 12, 2020, 1:00:24 AM8/12/20
to Pi4J
Thanks for the reply. You say that I should be using Pi4J 2.0 but I see the latest version as 1.2 which I have installed. I am not using source and have not changed the code.

GeertVc

unread,
Aug 12, 2020, 1:43:10 AM8/12/20
to Pi4J
Hold on a moment...  I didn't say that you should use 2.0, I was questioning if you were using Pi4J 2.0.  Pi4J 2.0 is still in a very early stage and until you want to do some bleeding edge stuff, I don't think it's wise to use it already.
1.2 is indeed the latest official release to date but I saw there's also a Pi4J 1.4 in the pipeline, so that one might be officially released far before the release of 2.0 (which is quite an overhaul).

But then again, when looking to the source code of SerialPort.java in Pi4J 1.2 (checked again in the GitHub repo of Pi4J, branch release/1.2), I still can't map your stack trace output with the lines in the source code.  And I'm wondering why...

Nevertheless, the cause of your problem is the same: RPi4 is not in the list of supported boards yet for the serial communication.  And Pi4J 1.2 is still supporting lots of other boards too (which has changed from version 1.4 onwards, where all support for none-RPi boards has been dropped).

John Gaby

unread,
Aug 12, 2020, 5:14:41 AM8/12/20
to Pi4J
"But then again, when looking to the source code of SerialPort.java in Pi4J 1.2 (checked again in the GitHub repo of Pi4J, branch release/1.2), I still can't map your stack trace output with the lines in the source code.  And I'm wondering why..."

Yes, that is the mystery. I understand that the RP4 is not supported, but I am having the same problem with the RP3B+ which is. Clearly I am using the wrong pi4j-core.jar file, but I cannot figure out how that is possible. I am doing remote development so the program is written on a PC using eclipse and then the executable are copied to the raspberry pi. This includes a copy of pi4j-core.jar, but after I run the program, I can go to the pi and search the entire file system for pi4j-core.jar files and they only ones found are identical with the one in /opt/pi4j/lib, except for a set found in /usr/share/bluej/userlib/. I have tried removing that copy but it does not seem to have any effect.

However, if I copy my entire eclipse project to the pi and build it using a version of eclipse on the pi, it runs without problem (i.e. it is able successfully connect to the serial port). I am at a complete loss as to what might be going on here.

Note that all of this works properly on a RP3B and a RP Zero.

Do you have any suggestions as to where I might look?

Thanks.

John Gaby

unread,
Aug 12, 2020, 10:38:56 AM8/12/20
to Pi4J
Ok, I have this working again, but I am still at a complete loss as to what was going on. I found that even though my program did not run correctly when I launched it from the PC, if I went to the pi and launched the code that was downloaded manually from the pi, it worked just fine, which meant that the downloaded pi4j-core.jar file WAS the correct one. That meant that somehow the remote launcher was launching it incorrectly and connecting to some other version of pi4j. However, the remote launcher had nothing to do with pi4j so I really didn't understand what it could be doing. I then dug up my old source for the remote launcher and built it on the pi so I could try and debug what was going on, and lo and behold, the newly built version worked properly.

I still don't understand what version of pi4j-core.jar the old launcher was using (or why). As I said, I searched the entire file system for that file and did not find any old copies, so it seem like it will forever remain a mystery.

I really do appreciate your help in looking into this. Thanks much.

taa...@gmail.com

unread,
Aug 12, 2020, 10:40:01 AM8/12/20
to Pi4J
There are multiple questions on this post.   The first is the unsupportedBoard
Pi4j 1.2 on a raspberry4 board requires wiringpi 2.52 . This post explains the verification and update steps.

GeertVc

unread,
Aug 12, 2020, 11:55:07 AM8/12/20
to Pi4J
You're welcome and glad to hear you have your system up and running again on an RPi3B.

One thing you could do to search for a possible duplicate of pi4j-core.jar on your RP:i distro is to start a recursive find on your RPi, starting at the very top.  On the command line, type the following command 'sudo find / -name pi4j-core.jar'.  
This might take a while (normally not, but I don't know if you have a RPi light distro installed or not) but it will give you at least an idea if there are/were multiple versions of pi4j-core.jar on your system that were leading to drawing wrong conclusions.  You should find at least (and preferably also at most) one instance.

Nothing more annoying than a problem for which you can/coulndn't find the cause.  Chances are you might run into the same issues again some time later...

Reply all
Reply to author
Forward
0 new messages