0

I am trying to convert data from my Arduino bluetooth device to a Double. So I write this function :

 func convert(from characteristic: CBCharacteristic) -> Double {

    guard let arduinoData = characteristic.value else { return -1 }
    let byteArray = [UInt8](arduinoData)

//  print(characteristicData as NSData)

    return Double(byteArray[0])
}

I receive character ascii from my arduino. So this code works for numbers from 0 to 9. But I want to receive value like 300.207

I would like to know if it was possible to read 7 octets in succession to place them in a single double ?

I try with the code bellow to read the first octet and the seconde but I have this error : Thread 1 : Fatal error: Index out of range

func convert(from characteristic: CBCharacteristic) -> Double {
        guard let arduinoData = characteristic.value else { return -1 }
        let byteArray = [UInt8](arduinoData)

        return Double(byteArray[0]) + Double(byteArray[1])
    }

Thanks ( I am a beginner )

H.U
  • 3
  • 2

0 Answers0