ORSSerialPortManager is a singleton class (one instance per application) that can be used to get a list of available serial ports. It will also handle closing open serial ports when the Mac goes to sleep, and reopening them automatically on wake. This prevents problems I’ve seen with serial port drivers that can hang if the port is left open when putting the machine to sleep. Note that using
ORSSerialPortManager is optional. It provides some nice functionality, but only
ORSSerialPort is necessary to simply send and received data.
To get the shared serial port manager:
ORSSerialPortManager *portManager = [ORSSerialPortManager sharedSerialPortManager];
To get a list of available ports:
ORSSerialPort posts notifications when a port is added to or removed from the system.
ORSSerialPortsWereConnectedNotification is posted when one or more ports are added to the system.
ORSSerialPortsWereDisconnectedNotification is posted when one ore more ports are removed from the system. The user info dictionary for each notification contains the list of ports added or removed. The keys to access these array are
ORSSerialPortManager is Key-Value Observing (KVO) compliant for its
availablePorts property. This means that you can observe
availablePorts to be notified when ports are added to or removed from the system. This also means that you can easily bind UI elements to the serial port manager’s
availablePorts property using Cocoa-bindings. This makes it easy to create a popup menu that displays available serial ports and updates automatically, for example.
ORSSerialPortManager’s close-on-sleep, reopen-on-wake functionality is automatic. The only thing necessary to enable it is to make sure that the singleton instance of
ORSSerialPortManager has been created by calling
sharedSerialPortManager at least once. Note that this behavior is only available in Cocoa apps, and is disabled when ORSSerialPort is used in a command-line only app.
@property (nonatomic, copy, readonly) NSArray *availablePorts
An array containing ORSSerialPort instances representing the serial ports available on the system. (read-only)
As explained above, this property is Key Value Observing compliant, and can be bound to for example an NSPopUpMenu to easily give the user a way to select an available port on the system.