2

I have been using the following link for reference... http://robsraspberrypi.blogspot.co.za/2016/01/raspberry-pi-adding-more-inputs-using.html

// setup SPI for communication
int fd = Spi.wiringPiSPISetup(1, 1000000);
if (fd <= -1) {
      throw new IOException("SPI port setup failed.");
}
byte packet[] = new byte[1];
// send data packet
int i Spi.wiringPiSPIDataRW(0, packet, 1);
  • Have you contacted the author? – joan Jul 25 '16 at 11:29
  • What is not working? What is the question you need answered ? – KennetRunner Jul 25 '16 at 11:56
  • The code just returns a 1 when calling the java code above and i dont know why. Does anyone know if this is the correct way of doing it – Brian Jul 25 '16 at 12:44
0

Okay, so it looks like there a number of issues with your code. I'm not sure it would even run. This line...

int fd = Spi.wiringPiSPISetup(1, 1000000);

...is setting up channel 1 for 1Mhz (which should be fine), but later you are trying to read/write on channel 0 (which has not yet been setup...)

int i Spi.wiringPiSPIDataRW(0, packet, 1);

I also think you need a = in there. int i = Spi.wiringPiSPIDataRW(0, packet, 1); This line...

byte packet[] = new byte[1];

...looks like it declares an array of 1 byte (?? I'm no java expert ??) but it is not initialized with any value ? That means you are sending whatever value happens to be in that byte at the time to the SPI bus and reading back it's output...

Finally - I don't think that you get the result in i (if that's what you were expecting ?), the docs for wiringPiSPI say:

  • int wiringPiSPIDataRW (int channel, unsigned char *data, int len);

This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.

So the data you are trying to read should end up in packet.

I would spend some more time looking at wiringPiSPI and some of the questions/comments here.

| improve this answer | |
  • One clarification: In Java this line byte packet[] = new byte[1]; will create an array of length 1 and initialize the contents to 0. That's different than some other languages, where you would get a garbage value in the array at initialization. Rest of this seems right. – Brick Nov 5 '17 at 13:28

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.