// // Serial.m // SerialGamepad / CaseLights // // For more informations refer to this document: // https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WWSerial_SerialDevs/SerialDevices.html // // Created by Thomas Buck on 14.12.15. // Copyright © 2015 xythobuz. All rights reserved. // #import #import #import #import #import #import #import #import #import "Serial.h" kern_return_t findSerialPorts(io_iterator_t *matches); kern_return_t getSerialPortPath(io_iterator_t serialPortIterator, char **deviceFilePath, CFIndex maxPathCount, CFIndex maxPathSize); @implementation Serial @synthesize fd, portName; - (NSInteger)openPort { // We need a port name if (portName == nil) { NSLog(@"Can't open serial port without name!\n"); return 1; } // Check if there was already a port opened if (fd != -1) { NSLog(@"Closing previously opened serial port \"%@\"!\n", portName); close(fd); } // Open port read-only, without controlling terminal, non-blocking fd = open([portName UTF8String], O_RDONLY | O_NOCTTY | O_NONBLOCK); if (fd == -1) { NSLog(@"Error opening serial port \"%@\": %s (%d)!\n", portName, strerror(errno), errno); return 1; } // Prevent additional opens except by root-owned processes if (ioctl(fd, TIOCEXCL) == -1) { NSLog(@"Error enabling exclusive access on \"%@\": %s (%d)!\n", portName, strerror(errno), errno); return 1; } fcntl(fd, F_SETFL, 0); // Enable blocking I/O // Read current settings struct termios options; tcgetattr(fd, &options); // Clear all settings options.c_lflag = 0; options.c_oflag = 0; options.c_iflag = 0; options.c_cflag = 0; options.c_cflag |= CS8; // 8 data bits options.c_cflag |= CREAD; // Enable receiver options.c_cflag |= CLOCAL; // Ignore modem status lines // Set Baudrate cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); options.c_cc[VMIN] = 0; // Return even with zero bytes... options.c_cc[VTIME] = 1; // ...but only after .1 seconds // Set new settings tcsetattr(fd, TCSANOW, &options); tcflush(fd, TCIOFLUSH); return 0; } - (NSInteger)hasData { struct pollfd fds; fds.fd = fd; fds.events = (POLLIN | POLLPRI); // Data may be read if (poll(&fds, 1, 0) > 0) { return 1; } else { return 0; } } + (NSArray *)listSerialPorts { // Get Iterator with all serial ports io_iterator_t serialPortIterator; kern_return_t kernResult = findSerialPorts(&serialPortIterator); // Create 2D array char **portList; portList = malloc(100 * sizeof(char *)); for (int i = 0; i < 100; i++) portList[i] = malloc(200 * sizeof(char)); // Copy device name into C-String array kernResult = getSerialPortPath(serialPortIterator, portList, 100, 200); IOObjectRelease(serialPortIterator); // Copy contents into NSString Array NSString *stringList[100]; NSUInteger realCount = 0; while (portList[realCount] != NULL) { stringList[realCount] = [NSString stringWithCString:portList[realCount] encoding:NSUTF8StringEncoding]; realCount++; } // Destroy 2D array for (int i = 0; i < 100; i++) free(portList[i]); free(portList); // And return them as NSArray return [[NSArray alloc] initWithObjects:stringList count:realCount]; } @end kern_return_t findSerialPorts(io_iterator_t *matches) { kern_return_t kernResult; mach_port_t masterPort; CFMutableDictionaryRef classesToMatch; kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort); if (KERN_SUCCESS != kernResult) { NSLog(@"IOMasterPort returned %d\n", kernResult); return kernResult; } // Serial devices are instances of class IOSerialBSDClient. classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (classesToMatch == NULL) { NSLog(@"IOServiceMatching returned a NULL dictionary.\n"); } else { CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDRS232Type)); // Each serial device object has a property with key // kIOSerialBSDTypeKey and a value that is one of // kIOSerialBSDAllTypes, kIOSerialBSDModemType, // or kIOSerialBSDRS232Type. You can change the // matching dictionary to find other types of serial // devices by changing the last parameter in the above call // to CFDictionarySetValue. } kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, matches); if (KERN_SUCCESS != kernResult) { NSLog(@"IOServiceGetMatchingServices returned %d\n", kernResult); return kernResult; } return kernResult; } kern_return_t getSerialPortPath(io_iterator_t serialPortIterator, char **deviceFilePath, CFIndex maxPathCount, CFIndex maxPathSize) { io_object_t modemService; kern_return_t kernResult = KERN_FAILURE; CFIndex i = 0; while ((modemService = IOIteratorNext(serialPortIterator)) && (i < (maxPathCount - 1))) { CFTypeRef deviceFilePathAsCFString; // Get the callout device's path (/dev/cu.xxxxx). // The callout device should almost always be // used. You would use the dialin device (/dev/tty.xxxxx) when // monitoring a serial port for // incoming calls, for example, a fax listener. deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(modemService, CFSTR(kIODialinDeviceKey), kCFAllocatorDefault, 0); if (deviceFilePathAsCFString) { Boolean result; deviceFilePath[i][0] = '\0'; // Convert the path from a CFString to a NULL-terminated C string // for use with the POSIX open() call. result = CFStringGetCString(deviceFilePathAsCFString, deviceFilePath[i], maxPathSize, kCFStringEncodingASCII); CFRelease(deviceFilePathAsCFString); if (result) { //NSLog(@"BSD path: %s\n", deviceFilePath[i]); i++; kernResult = KERN_SUCCESS; } } // Release the io_service_t now that we are done with it. (void) IOObjectRelease(modemService); } deviceFilePath[i] = NULL; return kernResult; }