2017-07-27 28 views
0

我正在執行一個項目,其中包括從使用SPI的絕對編碼器讀取值。將這些值修改併發送給電機。我已經能夠從編碼器中讀取數值,但是它們是按照我理解的「List」變量出現的,而我無法修改和發送變量。我需要將這些轉換爲整數。當我嘗試將SPI編碼器的值轉換爲整數

int(temp[1]) 

我得到這個錯誤:「類型錯誤:int()函數的參數必須是字符串或數字,而不是 '名單'」 這裏我的代碼:

#Import Librarys 
import RPi.GPIO as GPIO 
import time 
import spidev 

#Setup GPIO 
GPIO.setmode(GPIO.BOARD) 
GPIO.setwarnings(False) 
GPIO.setup(24,GPIO.OUT) 

#Declare variables and librarys 
temp = [1,2] 
spi = spidev.SpiDev() 

#Recieving Values from Absolute Encoder 

while True: 

spi.open(0,0)      #Opens the SPI Slave State Port for communication 

spi.xfer([0x10])     #Transfers the read position command [0x10] 
while spi.xfer([0x10])!=[0x10]:  #Waits for the response 
    spi.xfer([0x00])    #Sends a blank command while waiting 
    time.sleep(.1) 

temp[0] = spi.xfer([0x00])   #Pulls first Byte 
temp[1] = spi.xfer([0x00])   #Pulls second Byte 



print(temp[0]) 
print(temp[1]) 

這裏是我的輸出示例:

[10] 
[125] 
[10] 
[125] 
[10] 
[125] 
[10] 
[125] 
[10] 
[125] 
[10] 
[125] 
[10] 
[125] 

回答

0

spi.xfer總是返回一個列表。如果你知道你只得到一個字節後面你可以通過索引檢索:

temp[0] = spi.xfer([0x00])[0] 
temp[1] = spi.xfer([0x00])[0] 
+0

非常感謝!似乎很簡單.....哈哈,但現在我知道了! –