2017-07-27 24 views
0

現在、SPIを使用するアブソリュートエンコーダからの値の読み取りを含むプロジェクトを行っています。それらの値を変更してモーターに送ります。私はエンコーダから値を読み取ることができましたが、私が変更して戻すことができない「リスト」変数として理解しているものとして出てきています。私はこれらを整数に変換する必要があります。私はSPIエンコーダから整数への値の変換/読み取り

int(temp[1]) 

をしようとすると、私はこのエラーを取得:"例外TypeError: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は常にリストを返します。 1バイト戻っているだけの場合は、インデックスで取得できます。

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

ありがとうございます!とてもシンプルだと思う.....笑今は知ってるよ! –

関連する問題