Macs use the usual termios
TTY implementation that other POSIXes use, but support non-standard baud rates through the iossiospeed
ioctl (as of Mac OS X 10.4). To support non-standard baud rates on Mac, there are three main approaches:
iossiospeed
iossiospeed
for non-standard bauds, but termios
with standard baudsiossiospeed
always by default and fail-over to the termios approachThis library uses the first approach. Given that macOS as far back as 10.4 supports it (2005), there seem to be no downsides. Internally, baud rates within the termios
struct are kept at 9600 when that struct is read & written. This means that anytime the termios
struct is written back (using tcsetattr
a call to iossiospeed
follows it. Additionally, the termios
struct is not cached and instead retrieved on every settings adjustment. While this can increase the number of system calls when changing port settings, it removes the need to keep state consistent and instead the kernel's state can always be considered the canonical source.
iossiospeed
has no official documentation that can be found by searching https://developer.apple.com. However IOSerialTestLib.c can be found on Apple's open source code repository and has some example code for using this API.
Experimentation has shown that there are a few key features to using iossiospeed
:
iossiospeed
should be called after setting the termios
struct via tcsetattr
as that resets the baud rate and you cannot put custom baud rates in the termios
struct.iossiospeed
will modify the termios
struct in the kernel such that you can no longer round-trip the termios
struct. The following code will fail:struct termios t; tcgetattr(fd, &t); tcsetattr(fd, TCSANOW, &t)
termios
struct.