6

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 ?

daniula
  • 6,898
  • 4
  • 32
  • 49
kmn
  • 2,615
  • 4
  • 18
  • 28

1 Answers1

-3

See Apple Performing Serial I/O sample project.

geowar
  • 4,397
  • 1
  • 28
  • 24
  • Thank you ! I'll look into it and get back to you – kmn Feb 05 '15 at 07:15
  • 2
    Hi, Thanks for the link. I'm looking for a way to do this in swift though. This is in C. I appreciate your help. – kmn Feb 05 '15 at 09:23
  • @kmn did you get this? I'm looking for the same thing! – Parth Sane Nov 11 '15 at 17:36
  • 1
    Contact Developer Technical Support (DTS) at Apple or file a bug report (enhancement request) and request a swift version of the sample. – geowar Nov 11 '15 at 18:08
  • @Boggartfly, I couldn't fix the problem, but used ORSSerialPort (https://github.com/armadsen/ORSSerialPort), that worked very well – kmn Nov 13 '15 at 07:39
  • I got around this problem entirely by using libusb 0.x version! I wanted to communicate with a USB device and this served my purpose! – Parth Sane Nov 13 '15 at 07:55