0

我正在构建一个机械臂并使用 iOS 应用程序控制机械臂。我无法将位置发送到 arduino 蓝牙 4.0 防护罩。

我正在使用滑块来控制手臂的位置。

有两个错误。

  1. “不能使用 '(UInt8)' 类型的参数列表调用 'writePosition'”
  2. “不能使用 '(UInt64)' 类型的参数列表调用 'sendPosition'”

    func sendPosition(position: UInt8)         
    if !self.allowTX {
        return
    }
    
    // Validate value
    if UInt64(position) == lastPosition {
        return
    }
    else if ((position < 0) || (position > 180)) {
        return
    }
    
    // Send position to BLE Shield (if service exists and is connected)
    if let bleService = btDiscoverySharedInstance.bleService {
        bleService.writePosition(position) ***1)ERROR OCCURS ON THIS LINE***
        lastPosition = position
    
        // Start delay timer
        self.allowTX = false
        if timerTXDelay == nil {
            timerTXDelay = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("timerTXDelayElapsed"), userInfo: nil, repeats: false)
        }
      }
    }
    
    func timerTXDelayElapsed() {
    self.allowTX = true
    self.stopTimerTXDelay()
    
    // Send current slider position
    self.sendPosition(UInt64(self.currentClawValue.value)) **2)ERROR OCCURS ON THIS LINE**
    

    }

这是我的“writePosition”函数。

func writePosition(position: Int8) {
    // See if characteristic has been discovered before writing to it
    if self.positionCharacteristic == nil {
        return
    }

    // Need a mutable var to pass to writeValue function
    var positionValue = position
    let data = NSData(bytes: &positionValue, length: sizeof(UInt8))
    self.peripheral?.writeValue(data, forCharacteristic: self.positionCharacteristic, type: CBCharacteristicWriteType.WithResponse)
}

我不知道我是遗漏了什么还是完全遗漏了什么。我尝试过 UInt8 和 UInt64 之间的简单转换,但没有奏效。

4

2 回答 2

2

也许我遗漏了一些东西,但错误表明您正在使用类型为“(UInt8)”的参数列表调用“writePosition”

但是,writePosition 的参数列表指定了一个 Int8。要么将 writePosition 的参数类型更改为 UInt8,要么将调用参数更改(或强制转换)为 Int8。

同样,对于 sendPosition,它需要一个 UInt8,但您要向它发送一个 UInt64。

Swift 比较麻烦,因为它抱怨隐式类型转换。

您应该使用最适合您的数据的整数大小,或者 API 要求您使用的整数大小。

于 2015-02-23T23:19:35.420 回答
2

您的问题是您使用的不同 int 类型。

首先让我们检查writePosition方法。您使用Int8as 参数。因此,您需要确保您还使用Int8as 参数调用该方法。为了确保您使用的是,Int8您可以强制转换它:

bleService.writePosition(Int8(position))

正如您在此处看到的,您需要将positionto 转换为Int8.

现在检查你的sendPosition方法。你有一个类似的问题。你想要一个UInt8as 参数,但你用UInt64参数调用它。这是你做不到的。您需要使用相同的整数类型:

self.sendPosition(UInt8(self.currentClawValue.value))

这里是一样的。使用UInt8而不是UInt64使铸造工作。

于 2015-02-23T23:20:38.083 回答