Sending a number over SPI

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
HolyGrail
Posts: 7
Joined: Fri Jul 08, 2022 3:45 pm

Sending a number over SPI

Post by HolyGrail » Thu Jul 28, 2022 1:00 pm

I have set up an SPI link to send data from my micro:bit. The method write.spi(buffer) works fine when I am sending characters because the ASCII values are being transmitted. However, I want to be able to send numbers (up to the value of about 25). If I want to send a number between 0 and 9, that is easy as I can deduct 48 at the receiving end to convert the ASCII back into decimal. The problem arises when I want to send 10 and above as each digit is transmitted separately (eg first "1" then "0"). Is there a way that I can get spi.write to send a decimal number instead of ASCII characters, please?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Sending a number over SPI

Post by Roberthh » Thu Jul 28, 2022 1:36 pm

SPI.write requires a buffer for the data. So you have to create and fill it with your number. Doing that step by step tha may look like

buffer = bytearry(1)
buffer[0] = number
spi.write(buffer)

or you use struct, which is more flexible.

import struct
spi.write (struct.pack("B", number))

See the documentation of struct for format codes and syntax examples.

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Sending a number over SPI

Post by karfas » Thu Jul 28, 2022 1:36 pm

Send and receive an array of bytes instead of a string.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

HolyGrail
Posts: 7
Joined: Fri Jul 08, 2022 3:45 pm

Re: Sending a number over SPI

Post by HolyGrail » Thu Jul 28, 2022 5:58 pm

Many thanks to both of you for replying! :) I am new to Python so I am struggling with this. I want to take your advice and use an array of bytes to overcome the problem of sending integers in a string.

Let's say I want to send the integers 2 and 12 consecutively.

For an 'array' in MicroPython, I think I have to create a list or tuple. I will use a list as that is mutable. I have tried this:

bytearray = [2, 12]
buffer = bytearray
spi.write(buffer)

Mu does not complain but the Micro:bit reports a type error for the last line. Your help will be appreciated, thanks.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Sending a number over SPI

Post by Roberthh » Thu Jul 28, 2022 6:07 pm

bytearray = [2, 12] is a list, not a bytearray. Just give a variable the name bytearray does not make it a bytearray type of object.

buffer = bytearray([2, 12])

creates a two-element bytearray from a list, the same as:

buffer = struct.pack("BB", 2, 12)

creates a two element bytes object. The difference between the data types bytes and bytearray is, that bytes is immutable. But you can use both data types for spi.write(), since both of them have a so called buffer protocol.

HolyGrail
Posts: 7
Joined: Fri Jul 08, 2022 3:45 pm

Re: Sending a number over SPI

Post by HolyGrail » Thu Jul 28, 2022 7:22 pm

Thank you again for your help. I had never heard of bytearray before. I have just Googled it and now have a better understanding. I struggled to learn C but I find Python harder as it is a much larger language. Anyway, you have solved my problem. Best wishes.

Post Reply