I'm just getting started on an app that should eventually run a DIY CNC machine via an Arduino UNO. I've found a few resources for serial communication written in Objective-C so I figured I'd try to make it work. I'm trying to implement a function that will search for available serial ports on the computer and present them in a drop-down menu.
Xcode isn't complaining about any of the Objective-C components so I'm going to assume that's not the problem right now. However, Xcode gives me the error: "Use of unresolved identifier 'refreshSerialList'". This is the manner suggested by Apple to call an Objective-C method in Swift: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Serial.h
#import <Cocoa/Cocoa.h> // import IOKit headers #include <IOKit/IOKitLib.h> #include <IOKit/serial/IOSerialKeys.h> #include <IOKit/IOBSD.h> #include <IOKit/serial/ioss.h> #include <sys/ioctl.h> @interface Serial : NSObject { IBOutlet NSPopUpButton *serialListPullDown; } - (void) refreshSerialList: (NSString *) selectedText; @end Serial.m
#import "Serial.h" @implementation Serial - (void) refreshSerialList: (NSString *) selectedText { io_object_t serialPort; io_iterator_t serialPortIterator; // remove everything from the pull down list [serialListPullDown removeAllItems]; // ask for all the serial ports IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching(kIOSerialBSDServiceValue), &serialPortIterator); // loop through all the serial ports and add them to the array while (serialPort == IOIteratorNext(serialPortIterator)) { [serialListPullDown addItemWithTitle: (__bridge NSString*)IORegistryEntryCreateCFProperty(serialPort, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0)]; IOObjectRelease(serialPort); } // add the selected text to the top [serialListPullDown insertItemWithTitle:selectedText atIndex:0]; [serialListPullDown selectItemAtIndex:0]; IOObjectRelease(serialPortIterator); } @end Bridging Header.h
#import "Serial.h" AppDelegate.Swift
import Cocoa class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet var window: NSWindow! @IBOutlet var CommandInput : NSView! @IBOutlet var serialListPullDown : NSPopUpButton! func applicationDidFinishLaunching(aNotification: NSNotification?) { // Insert code here to initialize your application let refresh = refreshSerialList() } func applicationWillTerminate(aNotification: NSNotification?) { // Insert code here to tear down your application } @IBAction func CommandSend(sender : AnyObject) { } }