title: Using IOKit to communicate with USB device
body: <p>I'm trying to make an application that communicates with a USB device about the same way I use the <code>screen</code> command on Terminal.</p>
  
  <p>To make my question easier to understand, This is what I normally do in Terminal : </p>
  
  <p>command :</p>
  
  <pre><code>ls /dev/tty.usb*
  </code></pre>
  
  <p>returns : </p>
  
  <pre><code>/dev/tty.usbmodem1411   /dev/tty.usbmodem1451
  </code></pre>
  
  <p>Next, I call : </p>
  
  <pre><code>screen /dev/tty.usbmodem1411
  </code></pre>
  
  <p>After this, I can send commands to the device (type in 'U' for example, get a response)</p>
  
  <p>I'm trying to do this from Xcode now.</p>
  
  <p>Using IOKit, I've managed to execute what is equivalent to the first command that returns the list of usb ports :</p>
  
  <pre><code>/dev/tty.usbmodem1411   /dev/tty.usbmodem1451
  </code></pre>
  
  <p>This is the code : </p>
  
  <pre><code>@IBAction func testPressed(sender: AnyObject) {
  
          var portIterator: io_iterator_t = 0
          let kernResult = findSerialDevices(kIOSerialBSDModemType, serialPortIterator: &amp;portIterator)
          if kernResult == KERN_SUCCESS {
              printSerialPaths(portIterator)
          }
  
      }
  
      func findSerialDevices(deviceType: String, inout serialPortIterator: io_iterator_t ) -&gt; kern_return_t {
          var result: kern_return_t = KERN_FAILURE
          var classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue).takeUnretainedValue()
          var classesToMatchDict = (classesToMatch as NSDictionary) as Dictionary&lt;String, AnyObject&gt;
          classesToMatchDict[kIOSerialBSDTypeKey] = deviceType
          let classesToMatchCFDictRef = (classesToMatchDict as NSDictionary) as CFDictionaryRef
          result = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatchCFDictRef, &amp;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;
      }
  }
  </code></pre>
  
  <p>Now, even after  reading Apple's IOKit manual, I can't move forward.
  How can I start sending commands using IOKit ?</p>