pi4j-v2 icon indicating copy to clipboard operation
pi4j-v2 copied to clipboard

Multiple SPI throws IOAlreadyExistsException

Open mvanassche opened this issue 3 years ago • 2 comments

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 avatar Sep 22 '22 09:09 mvanassche

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.

mvanassche avatar Sep 23 '22 09:09 mvanassche

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

FDelporte avatar Sep 23 '22 11:09 FDelporte