0

I'm trying to connect raspberry with another pcb via spi. It is connected via spi1 port and I can't change it. So I have a python script that works well, so I can send and receive data. The python script:

import RPi.GPIO
import RPi.GPIO as GPIO

import spidev
import time
import os


spi = spidev.SpiDev()

spi.open(1, 2)
spi.max_speed_hz =1000000
spi.mode = 0b00


try:
    while True:
        tx_list = [0x10]+ [228, 123, 145, 234, 221, 42, 212, 226] +[0]
        print(tx_list)
        rx_list=spi.xfer(tx_list)
        
        str_read = "".join([chr(_) for _ in rx_list])
        print(str_read)
        time.sleep(1)
except KeyboardInterrupt:
    print('exiting')
finally:
    spi.close()

But I need to work with spi using java, so I decided to use pi4j library. Then I wrote a simple program, that should do the same as the python script:

import com.pi4j.io.spi.SpiChannel;
import com.pi4j.io.spi.SpiDevice;
import com.pi4j.io.spi.SpiFactory;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;

public class SPI
{

    public static void StartSPI()
    {
        try {
            SpiDevice spi = SpiFactory.getInstance(
                    SpiChannel.CS1,
                    SpiDevice.DEFAULT_SPI_SPEED,
                    SpiDevice.DEFAULT_SPI_MODE);

            while(true)
            {
                byte[] rx = spi.write((byte)16, (byte)228, (byte)123, (byte)145, (byte)234, (byte)221,
                        (byte)42, (byte)212, (byte)226, (byte)0);
                System.out.println(Arrays.toString(rx));
                Thread.sleep(1000);
            }
        }
        catch (Exception e)
        {
            // log
        }
    }
}

But it doesn't work. It returns only empty arrays like this:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

What could be the reason of this? Thanks!

Edit 1: So I found jdk.dio library, but it is too difficult for me to install it to rpi. So the conclusion is: java is poor.

0

Setting dtparam & dtoverlay in a program is futile.

These are instructions to Device Tree to configure the operating system at boot time.

Edit your config.txt.

https://www.raspberrypi.org/documentation/computers/configuration.html#part3.1

1
  • thank you for your answer. I edited config.txt and removed command line calls from my code (also I will edit my question now). But it didn't help :( Java program still doesn't work Sep 28 '21 at 7:47
0

In the Python code you are using SPI bus 1 and chip select 2.

In the Java you are using the default SPI bus (0) and chip select 1.

You need to use the bus and chip select appropriate for your wiring.

2
0

Which version of Pi4J are you using?

Examples with SPI can be found here:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.