【问题标题】:Converting/Reading Values from SPI Encoder to Integers将值从 SPI 编码器转换/读取为整数
【发布时间】: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


    【解决方案1】:

    spi.xfer 总是返回一个列表。如果你知道你只得到一个字节,你可以通过索引检索它:

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

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2023-03-13
      • 2019-02-12
      • 2020-11-08
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多