i am trying to read a sting via the SPI port on the Raspberry Pi. This is my code to test the port:
raw = 0
string = ""
SPI = spidev.SpiDev()
SPI.open(0,0)
while True:
raw = SPI.xfer2([0])
string += str(chr(raw))
print string
print raw
time.sleep(0.2)
The result is "Hellinsert gibberish" so it fails after the fourth character. I try to send "Hello World!" The data I send is formatted as character in the list, for example "Hello" will look like [72, 101, 108, 108, 111]. How do I convert this to a string?
The answers were useful, as I did not know how to convert the data. The real problem however was with the device I was interfacing with. The answers were useful in finding the real problem, so many thanks! I am still fairly new to python so these things are kind of a hassle to get into.
You can either use bytearray
or str.join
with chr()
:
>>> lst = [72, 101, 108, 108, 111]
>>> str(bytearray(lst))
'Hello'
#or
>>> ''.join(chr(x) for x in lst)
'Hello'