0

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) { } } 
1
  • I think you need to create a Serial object first to call the method? Commented Aug 13, 2014 at 23:44

2 Answers 2

1

refreshSerialList takes one parameter, a string. So first you'd have to create an instance of Serial:

var serial = Serial() 

then call the method on the instance:

serial.refreshSerialList("some string") 
Sign up to request clarification or add additional context in comments.

Comments

0

while (serialPort == IOIteratorNext(serialPortIterator))

tests the value of serialPort against each value of IOIteratorNext(serialPortIterator). But serialPort never gets assigned a value so this will not work. What's required is:

while (serialPort = IOIteratorNext(serialPortIterator)) 

which both assigns a value to serialPort and tests its value.

In Swift, an assignment in an if clause is not allowed. In Swift, what's required is:

repeat { serialPort = IOIteratorNext(serialPortIterator) if (serialPort != 0) { <statement> <statement> } } while serialPort != 0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.