Pi4J / pi4j-v2

Pi4J Version 2.0
Apache License 2.0
273 stars 57 forks source link

Multiple SPI throws IOAlreadyExistsException #244

Open mvanassche opened 2 years ago

mvanassche commented 2 years ago

When creating multiple spi, an exception IOAlreadyExistsException is thrown.

The config id is not transferred to the instance object.

Unlike other IO, SpiBase inherits from IOBase, which does not transfer the id/name to the instance (GpioBase and I2CBase do transfer).

I suppose there is simply

        this.name = config.name();
        this.id = config.id();
        this.description = config.description();

missing in the constructor of SpiBase?

mvanassche commented 2 years ago
diff --git a/pi4j-core/src/main/java/com/pi4j/io/spi/SpiBase.java b/pi4j-core/src/main/java/com/pi4j/io/spi/SpiBase.java
index 80f807e..751bc80 100644
--- a/pi4j-core/src/main/java/com/pi4j/io/spi/SpiBase.java
+++ b/pi4j-core/src/main/java/com/pi4j/io/spi/SpiBase.java
@@ -48,6 +48,9 @@ public abstract class SpiBase extends IOBase<Spi, SpiConfig, SpiProvider> implem
      */
     public SpiBase(SpiProvider provider, SpiConfig config) {
         super(provider, config);
+        this.name = config.name();
+        this.id = config.id();
+        this.description = config.description();
     }

     /** {@inheritDoc} */

I tested with that diff with my example with two SPI devices on the same bus, different channel, and so far, it works.

FDelporte commented 2 years ago

Thanks @mvanassche for reporting and proposing a fix!!! I created a PR for this https://github.com/Pi4J/pi4j-v2/pull/245.