OBD2Kit Swift 示例无法进行类型转换,一直为 nil
OBD2Kit Swift example can't typecast, keep getting nil
由于没有使用 OBD2Kit 的示例代码,Swift 我将其分叉到 https://github.com/YannisDC/OBD2Kit 中并将其用作 pod。
我翻译了一些 OBJ-C 示例代码,但似乎无法将 FLWiFiScanTool 向下转换为 ELM327 类型。为什么我总是得到零?
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
@IBOutlet weak var hostIpAddress: UITextField!
var scanTool: ELM327!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var scanToolLabel: UILabel!
@IBOutlet weak var rpmLabel: UILabel!
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.hostIpAddress.text = "192.168.0.10"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: self.hostIpAddress.text!, andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
// let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as! AnyObject,
// OBD2Sensor.SensorVehicleSpeed as! AnyObject,
// OBD2Sensor.SensorOxygenSensorsPresent as! AnyObject ]
let sensors: [AnyObject] = [ 0x0C,
0x0D,
0x13 ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool, sensor: FLECUSensor) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(false)) \(sensor.imperialUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}
编辑 1:
我现在已经可以扫描工具了,因为 ELM327 是一个 FLWiFiScanTool,而不是相反。我可以获得工具名称,因此它正在连接,但似乎无法获得传感器输出。
我设法弄明白了。预期的传感器阵列应该是 NSNumbers 之一,所以我将传感器 UInt 投射到 NSNumbers。
而且我还错过了 scanTool 函数中的 didUpdateSensor 部分。
确保使用公制系统,这在某种程度上对我来说更可靠。 (我的车也使用公制,也许这就是原因。)
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
var scanTool: ELM327!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var scanToolLabel: UILabel!
@IBOutlet weak var rpmLabel: UILabel!
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: "192.168.0.10", andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as NSNumber, OBD2Sensor.SensorVehicleSpeed as NSNumber, OBD2Sensor.SensorOxygenSensorsPresent as NSNumber ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool!, didUpdateSensor sensor: FLECUSensor!) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(true)) \(sensor.metricUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}
由于没有使用 OBD2Kit 的示例代码,Swift 我将其分叉到 https://github.com/YannisDC/OBD2Kit 中并将其用作 pod。
我翻译了一些 OBJ-C 示例代码,但似乎无法将 FLWiFiScanTool 向下转换为 ELM327 类型。为什么我总是得到零?
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
@IBOutlet weak var hostIpAddress: UITextField!
var scanTool: ELM327!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var scanToolLabel: UILabel!
@IBOutlet weak var rpmLabel: UILabel!
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.hostIpAddress.text = "192.168.0.10"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: self.hostIpAddress.text!, andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
// let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as! AnyObject,
// OBD2Sensor.SensorVehicleSpeed as! AnyObject,
// OBD2Sensor.SensorOxygenSensorsPresent as! AnyObject ]
let sensors: [AnyObject] = [ 0x0C,
0x0D,
0x13 ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool, sensor: FLECUSensor) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(false)) \(sensor.imperialUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}
编辑 1:
我现在已经可以扫描工具了,因为 ELM327 是一个 FLWiFiScanTool,而不是相反。我可以获得工具名称,因此它正在连接,但似乎无法获得传感器输出。
我设法弄明白了。预期的传感器阵列应该是 NSNumbers 之一,所以我将传感器 UInt 投射到 NSNumbers。 而且我还错过了 scanTool 函数中的 didUpdateSensor 部分。
确保使用公制系统,这在某种程度上对我来说更可靠。 (我的车也使用公制,也许这就是原因。)
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
var scanTool: ELM327!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var scanToolLabel: UILabel!
@IBOutlet weak var rpmLabel: UILabel!
@IBOutlet weak var speedLabel: UILabel!
@IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: "192.168.0.10", andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as NSNumber, OBD2Sensor.SensorVehicleSpeed as NSNumber, OBD2Sensor.SensorOxygenSensorsPresent as NSNumber ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool!, didUpdateSensor sensor: FLECUSensor!) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(true)) \(sensor.metricUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}