Skip to content

Commit

Permalink
Added allowsNonStandardBaudRates setting. Defaults to NO. See discuss…
Browse files Browse the repository at this point in the history
…ion on pull request #49.
  • Loading branch information
armadsen committed Apr 5, 2015
1 parent eae268b commit 5a270cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
20 changes: 19 additions & 1 deletion Source/ORSSerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ typedef NS_ENUM(NSUInteger, ORSSerialPortParity) {
/**
* The baud rate for the port.
*
* This value should be one of the values defined in termios.h:
* Unless supportsNonStandardBaudRates is YES,
* this value should be one of the values defined in termios.h:
*
* - 0
* - 50
Expand Down Expand Up @@ -356,6 +357,23 @@ typedef NS_ENUM(NSUInteger, ORSSerialPortParity) {
*/
@property (nonatomic, copy) NSNumber *baudRate;

/**
* Whether or not the port allows setting non-standard baud rates.
* Set this property to YES to allow setting non-standard baud rates
* for the port. The default is NO.
*
* @note Support for non-standard baud rates
* depends on the serial hardware and driver being used. Even
* for hardware/drivers that support non-standard baud rates,
* it may be that not all baud rates are supported.
* ORSSerialPort may *not* report an error when setting
* a non-standard baud rate, nor will the baudRate getter return
* the actual baud rate when non-standard baud rates are
* used. This option should only be used when necessary,
* and should be used with caution.
*/
@property (nonatomic) BOOL allowsNonStandardBaudRates;

/**
* The number of stop bits. Values other than 1 or 2 are invalid.
*/
Expand Down
21 changes: 12 additions & 9 deletions Source/ORSSerialPort.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ - (instancetype)initWithDevice:(io_object_t)device;
self.requestsQueue = [NSMutableArray array];
self.selectSemaphore = dispatch_semaphore_create(1);
self.baudRate = @B19200;
self.allowsNonStandardBaudRates = NO;
self.numberOfStopBits = 1;
self.parity = ORSSerialPortParityNone;
self.shouldEchoReceivedData = NO;
Expand Down Expand Up @@ -600,15 +601,17 @@ - (void)setPortOptions;
cfsetspeed(&options, [[self baudRate] unsignedLongValue]);

int result = tcsetattr(self.fileDescriptor, TCSANOW, &options);
if (result != 0) {
// Try to set baud rate via ioctl if normal port settings fail
int new_baud = [[self baudRate] intValue];
result = ioctl(self.fileDescriptor, IOSSIOSPEED, &new_baud, 1);
if (result == -1) {
// Notify delegate of port error stored in errno
[self notifyDelegateOfPosixError];
}
}
if (result != 0) {
if (self.allowsNonStandardBaudRates) {
// Try to set baud rate via ioctl if normal port settings fail
int new_baud = [[self baudRate] intValue];
result = ioctl(self.fileDescriptor, IOSSIOSPEED, &new_baud, 1);
}
if (result != 0) {
// Notify delegate of port error stored in errno
[self notifyDelegateOfPosixError];
}
}
}

+ (io_object_t)deviceFromBSDPath:(NSString *)bsdPath;
Expand Down

0 comments on commit 5a270cf

Please sign in to comment.