【发布时间】:2017-07-27 01:12:01
【问题描述】:
我目前正在做一个项目,其中包括从使用 SPI 的绝对编码器读取值。获取这些值来修改它们并将它们发送到电机。我已经能够从编码器中读取值,但是它们以我理解的“列表”变量的形式出现,我无法修改并发回。我需要将这些转换为整数。当我尝试
int(temp[1])
我收到此错误:“TypeError: int() 参数必须是字符串或数字,而不是 'list'” 这是我的代码:
#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]
【问题讨论】:
标签: python raspberry-pi spi