Simple RGB LED controller for Mac OS X
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Serial.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Serial.m
  3. // SerialGamepad / CaseLights
  4. //
  5. // For more informations refer to this document:
  6. // https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WWSerial_SerialDevs/SerialDevices.html
  7. //
  8. // Created by Thomas Buck on 14.12.15.
  9. // Copyright © 2015 xythobuz. All rights reserved.
  10. //
  11. #import <Cocoa/Cocoa.h>
  12. #import <IOKit/IOKitLib.h>
  13. #import <IOKit/serial/IOSerialKeys.h>
  14. #import "Serial.h"
  15. kern_return_t findSerialPorts(io_iterator_t *matches);
  16. kern_return_t getSerialPortPath(io_iterator_t serialPortIterator, char **deviceFilePath, CFIndex maxPathCount, CFIndex maxPathSize);
  17. @implementation Serial
  18. + (NSArray *)listSerialPorts {
  19. // Get Iterator with all serial ports
  20. io_iterator_t serialPortIterator;
  21. kern_return_t kernResult = findSerialPorts(&serialPortIterator);
  22. // Create 2D array
  23. char **portList;
  24. portList = malloc(100 * sizeof(char *));
  25. for (int i = 0; i < 100; i++) portList[i] = malloc(200 * sizeof(char));
  26. // Copy device name into C-String array
  27. kernResult = getSerialPortPath(serialPortIterator, portList, 100, 200);
  28. IOObjectRelease(serialPortIterator);
  29. // Copy contents into NSString Array
  30. NSString *stringList[100];
  31. NSUInteger realCount = 0;
  32. while (portList[realCount] != NULL) {
  33. stringList[realCount] = [NSString stringWithCString:portList[realCount] encoding:NSUTF8StringEncoding];
  34. realCount++;
  35. }
  36. // Destroy 2D array
  37. for (int i = 0; i < 100; i++) free(portList[i]);
  38. free(portList);
  39. // And return them as NSArray
  40. return [[NSArray alloc] initWithObjects:stringList count:realCount];
  41. }
  42. @end
  43. kern_return_t findSerialPorts(io_iterator_t *matches) {
  44. kern_return_t kernResult;
  45. mach_port_t masterPort;
  46. CFMutableDictionaryRef classesToMatch;
  47. kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
  48. if (KERN_SUCCESS != kernResult) {
  49. NSLog(@"IOMasterPort returned %d\n", kernResult);
  50. return kernResult;
  51. }
  52. // Serial devices are instances of class IOSerialBSDClient.
  53. classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
  54. if (classesToMatch == NULL) {
  55. NSLog(@"IOServiceMatching returned a NULL dictionary.\n");
  56. } else {
  57. CFDictionarySetValue(classesToMatch,
  58. CFSTR(kIOSerialBSDTypeKey),
  59. CFSTR(kIOSerialBSDRS232Type));
  60. // Each serial device object has a property with key
  61. // kIOSerialBSDTypeKey and a value that is one of
  62. // kIOSerialBSDAllTypes, kIOSerialBSDModemType,
  63. // or kIOSerialBSDRS232Type. You can change the
  64. // matching dictionary to find other types of serial
  65. // devices by changing the last parameter in the above call
  66. // to CFDictionarySetValue.
  67. }
  68. kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, matches);
  69. if (KERN_SUCCESS != kernResult) {
  70. NSLog(@"IOServiceGetMatchingServices returned %d\n", kernResult);
  71. return kernResult;
  72. }
  73. return kernResult;
  74. }
  75. kern_return_t getSerialPortPath(io_iterator_t serialPortIterator, char **deviceFilePath, CFIndex maxPathCount, CFIndex maxPathSize) {
  76. io_object_t modemService;
  77. kern_return_t kernResult = KERN_FAILURE;
  78. CFIndex i = 0;
  79. while ((modemService = IOIteratorNext(serialPortIterator)) && (i < (maxPathCount - 1))) {
  80. CFTypeRef deviceFilePathAsCFString;
  81. // Get the callout device's path (/dev/cu.xxxxx).
  82. // The callout device should almost always be
  83. // used. You would use the dialin device (/dev/tty.xxxxx) when
  84. // monitoring a serial port for
  85. // incoming calls, for example, a fax listener.
  86. deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(modemService,
  87. CFSTR(kIODialinDeviceKey),
  88. kCFAllocatorDefault,
  89. 0);
  90. if (deviceFilePathAsCFString) {
  91. Boolean result;
  92. deviceFilePath[i][0] = '\0';
  93. // Convert the path from a CFString to a NULL-terminated C string
  94. // for use with the POSIX open() call.
  95. result = CFStringGetCString(deviceFilePathAsCFString,
  96. deviceFilePath[i],
  97. maxPathSize,
  98. kCFStringEncodingASCII);
  99. CFRelease(deviceFilePathAsCFString);
  100. if (result) {
  101. //NSLog(@"BSD path: %s\n", deviceFilePath[i]);
  102. i++;
  103. kernResult = KERN_SUCCESS;
  104. }
  105. }
  106. // Release the io_service_t now that we are done with it.
  107. (void) IOObjectRelease(modemService);
  108. }
  109. deviceFilePath[i] = NULL;
  110. return kernResult;
  111. }