I'm trying to make an application that communicates with a USB device about the same way I use the screen
command on Terminal.
To make my question easier to understand, This is what I normally do in Terminal :
command :
ls /dev/tty.usb*
returns :
/dev/tty.usbmodem1411 /dev/tty.usbmodem1451
Next, I call :
screen /dev/tty.usbmodem1411
After this, I can send commands to the device (type in 'U' for example, get a response)
I'm trying to do this from Xcode now.
Using IOKit, I've managed to execute what is equivalent to the first command that returns the list of usb ports :
/dev/tty.usbmodem1411 /dev/tty.usbmodem1451
This is the code :
@IBAction func testPressed(sender: AnyObject) {
var portIterator: io_iterator_t = 0
let kernResult = findSerialDevices(kIOSerialBSDModemType, serialPortIterator: &portIterator)
if kernResult == KERN_SUCCESS {
printSerialPaths(portIterator)
}
}
func findSerialDevices(deviceType: String, inout serialPortIterator: io_iterator_t ) -> kern_return_t {
var result: kern_return_t = KERN_FAILURE
var classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue).takeUnretainedValue()
var classesToMatchDict = (classesToMatch as NSDictionary) as Dictionary<String, AnyObject>
classesToMatchDict[kIOSerialBSDTypeKey] = deviceType
let classesToMatchCFDictRef = (classesToMatchDict as NSDictionary) as CFDictionaryRef
result = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatchCFDictRef, &serialPortIterator);
return result
}
func printSerialPaths(portIterator: io_iterator_t) {
var serialService: io_object_t
do {
serialService = IOIteratorNext(portIterator)
if (serialService != 0) {
let key: CFString! = "IOCalloutDevice"
let bsdPathAsCFtring: AnyObject? = IORegistryEntryCreateCFProperty(serialService, key, kCFAllocatorDefault, 0).takeUnretainedValue()
var bsdPath = bsdPathAsCFtring as String?
if let path = bsdPath {
println(path)
}
}
} while serialService != 0;
}
}
Now, even after reading Apple's IOKit manual, I can't move forward. How can I start sending commands using IOKit ?
See Apple Performing Serial I/O sample project.