简体   繁体   English

使用IOKit与USB设备通信

[英]Using IOKit to communicate with USB device

I'm trying to make an application that communicates with a USB device about the same way I use the screen command on Terminal. 我正在尝试创建一个与USB设备通信的应用程序,就像我在终端上使用screen命令一样。

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) 在此之后,我可以向设备发送命令(例如输入'U',获得响应)

I'm trying to do this from Xcode now. 我正试图从Xcode做到这一点。

Using IOKit, I've managed to execute what is equivalent to the first command that returns the list of usb ports : 使用IOKit,我设法执行的东西等同于返回usb端口列表的第一个命令:

/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. 现在,即使阅读了Apple的IOKit手册,我也无法前进。 How can I start sending commands using IOKit ? 如何使用IOKit开始发送命令?

请参阅Apple Performing Serial I / O示例项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM