error: unreported exception UnsupportedBusNumberException; must be caught or declared to be thrown

36 views
Skip to first unread message

Ronnie76

unread,
Jun 8, 2019, 3:27:23 AM6/8/19
to Pi4J
Hello,

I'm using a Raspberry Pi 3 Model B Rev 1.2 with gpio version: 2.50 installed.
I'm trying to address a PCB with 8x MCP23017 (=128 GPIO's) that is behind a PCB with a PCA9544 hub (4 x I2C ports) 
With the set up as described above I'm running so that I can address my GPIO's succesfully and having LED's blinking on the designated GPIO. (it was without the use of com.pi4j.io.gpio library !) 

The thing is that output is not a problem any more but now I want to read the GPIO port states.
Since I got stuck with the code I used so far concerning input I came across some code example using the com.pi4j.io.gpio library, I've downloaded the .jar and tried to run this code:

import com.pi4j.gpio.extension.mcp.MCP23017GpioProvider;
import com.pi4j.gpio.extension.mcp.MCP23017Pin;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory.UnsupportedBusNumberException;
import com.pi4j.wiringpi.I2C;
import com.pi4j.wiringpi.Gpio;
import java.io.IOException;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author burak.dogancay
 */
public class MCP23017_InputTest {
    
   
    public static int ldrval; 
    public static int incnumber;
    public static int incnumberl;
    public static void I2csetup() throws UnsupportedBusNumberException, IOException{
        
         final GpioController gpio = GpioFactory.getInstance();
   
   /*  
          final MCP23017GpioProvider provider = new MCP23017GpioProvider(I2CBus.BUS_1, 0x71);
      
  
        GpioPinDigitalInput myinputs= gpio.provisionDigitalInputPin(provider, MCP23017Pin.GPIO_A0, "MyInput-A0", PinPullResistance.PULL_UP);
       GpioPinDigitalOutput myoutput[] = {
        gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_A1, "MyOutput-A1", PinState.LOW),
        gpio.provisionDigitalOutputPin(provider, MCP23017Pin.GPIO_B1, "MyOutput-B1", PinState.LOW)
                };
       
       while(true){
        try {
            ldrval = gpio.getState(myinputs).getValue();
            if (ldrval==1){
                gpio.setState(true, myoutput);
                System.out.println("The ldr value is correctly read : %d"+ldrval);
            }
            else{
                gpio.setState(false, myoutput);
                System.out.println("The ldr value is correctly read : %d"+ldrval);
            }
            if((incnumber>=15)&&(incnumberl==incnumber)){
                break;
            }
            incnumber+=1;
            incnumberl=incnumber;
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            //Logger.getLogger(Pi4jI2Control.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }
*/
        
       gpio.shutdown();
    }
}


As you see I've commented the main programmlines out and only trying to initiate the gpio through 'final GpioController gpio = GpioFactory.getInstance();'
But running this I got the following error: 

pi@raspberrypi:~/I2C $ javac -cp pi4j-core.jar:pi4j-gpio-extension-0.0.5-sources.jar:. MCP23017_InputTest.java 
pi4j-gpio-extension-0.0.5-sources.jar(com/pi4j/gpio/extension/mcp/MCP23017GpioProvider.java):102: error: unreported exception UnsupportedBusNumberException; must be caught or declared to be thrown
        bus = I2CFactory.getInstance(busNumber);
                                                   ^
1 error




Having ofcourse pi4j-core.jar & pi4j-gpio-extension-0.0.5-sources.jar in this same directory and ' pi@raspberrypi:~/I2C $ gpio readall' giving me a table with the port information.

What goes wrong here and how can I resolve this ? Has this got to do with the PCA9544 that is in front of the 8x MCP23017  ??


Thomas Aarts

unread,
Jun 15, 2019, 1:59:38 PM6/15/19
to Pi4J
Don't know if you answered your own question already.

You used the word running, but posted a compile error.   I was able to successfully  compile the example /opt/pi4j/examples> build MCP23017GpioExample.java    Its' source contains the same statement you identified, as a problem,  I do not  have your test case to compile it.

Also, you questioned the fact your GPIO controller is behind a 9544.   I believe the 23017 tests cases assume the controller is at address 0x20 attached to the bus.  So in your case you must enable the appropriate 9544 bus so the controller is connected.   After this is done you can execute the test.

GeertVc

unread,
Jul 14, 2019, 3:38:51 AM7/14/19
to Pi4J
I see the OP is using RPi 3B in combination with an I2c multiplexer (PCA9544).  Know that the kernel used in the latest RPi distro's (IIRC, from Jessie onwards) has built-in capabilities to control the I2c MUX from within the kernel directly which means this is transparent for you.  The only thing you have to do is to add a line to the /boot/config.txt file like so: dtoverlay=i2c-mux,pca9544,addr=0x70 (assuming you're using the default I2c address...).

After reboot, you should see 5 I2c devices (the original one + 4 extra ones): do ls -als /dev/i2c-* on the command line and you should see the 4 extra I2c devices).  If you then use Pi4J classes to control the MCP23017 you only have to specify which I2c bus you're using (/dev/i2c-x) and the kernel will redirect the I2c commands for the MCP23017 connected to that particular I2c bus for you.

I've successfully applied this mechanism for a PCA9548 I2c muxer (8 channels iso 4)

Andrew Chandler

unread,
Jul 14, 2019, 5:21:53 PM7/14/19
to Pi4J
Oh that is awesome news Geert!
Reply all
Reply to author
Forward
0 new messages