I am trying to use file-based SPI communication from my Raspberry Pi to an Arduino (328p based). The weird thing is that when I write it out one byte at a time and flush the buffer between each write it works, but I cannot just send it a large buffer all at once. What's really weird about it is that when I do, some of the data goes through until there is a 0 in the byte array being sent and then it just ignores everything after that. So I don't think it's a speed issue. See python code below with both what works and what doesn't. Arduino side has SPI handling code that I know to work perfectly between it and another Arduino, so I'll exclude that for brevity.

dev = '/dev/spidev0.0'
spi = open(dev, "wb")

buffer = bytearray()

#fill the buffer here

#this works
for i in range(len(buffer)):
        spi.write(struct.pack("B", buffer[i]))

#this does NOT work

Any thoughts as to why, when writing multiple bytes at once, it just gives up once it sees a 0?

  • 1
    Your description suggests that the variation of the spi.write() method is interpreting the 0 as a terminating null. Either that, or there is an error in your arduino code exposed or concealed by certain timings between bytes. Mar 3, 2015 at 12:28

3 Answers 3


First I assume that You don't use spidev library because want to play with direct file access to learn.

Have you tried if a byte of 0xFF (all 1'ns) will break communication too?

Hoe is the mode of SPI on both ends, I am referring to CPOL and CPHA

I have not tried sending an ioctl command in python but it is possible se fcntl

You can maybe use this example in c, first to test your communication with different settings of mode, then as inspiration on how to set mode using ioctl in python.


I assume; just using the open function creates a standard file object. In a standard file "0" is the "EOF" flag, so the write function closes the file. I was looking into writing directly to the SPI device file as well. It looks like the best solution would be to import os and use os.open()I haven't tried it yet, but in theory it should solve the issue; you had.

  • I'm pretty sure io.write() can handle zero bytes just fine, but I'd be interested in the feedback of the asker. Jun 26, 2019 at 11:28
  • I think; this may be outdated functionality as I was testing this the write() method does not iterate at all. I'm using version 3.6.5.
    – slr1337
    Jul 9, 2019 at 17:05

Check the return code of spi.write(buffer) - it should be equal to len(buffer), unless you have overlooked something.

Note that if you're trying to send more than 4 kilobytes at once, spidev will be unable to handle that data in a single transfer:

  • There's a limit on the number of bytes each I/O request can transfer to the SPI device. It defaults to one page, but that can be changed using a module parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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