Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try the ioctl call - you can specify an arbitrary baud rate. That is,</p> <p><em>ioctl(serialFileDescriptor, IOSSIOSPEED, &amp;baudRate);</em></p> <p><strong>To open the serial port:</strong></p> <pre><code>// Open the serial like POSIX C serialFileDescriptor = open( "/dev/tty.usbserial-A6008cD3", O_RDWR | O_NOCTTY | O_NONBLOCK ); // Block non-root users from using this port ioctl(serialFileDescriptor, TIOCEXCL); // Clear the O_NONBLOCK flag, so that read() will // block and wait for data. fcntl(serialFileDescriptor, F_SETFL, 0); // Grab the options for the serial port tcgetattr(serialFileDescriptor, &amp;options); // Setting raw-mode allows the use of tcsetattr() and ioctl() cfmakeraw(&amp;options); // Specify any arbitrary baud rate ioctl(serialFileDescriptor, IOSSIOSPEED, &amp;baudRate); </code></pre> <p><strong>To read from the serial port:</strong></p> <pre><code>// This selector will be called as another thread - (void)incomingTextUpdateThread: (NSThread *) parentThread { char byte_buffer[100]; // Buffer for holding incoming data int numBytes=1; // Number of bytes read during read // Create a pool so we can use regular Cocoa stuff. // Child threads can't re-use the parent's autorelease pool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // This will loop until the serial port closes while(numBytes&gt;0) { // read() blocks until data is read or the port is closed numBytes = read(serialFileDescriptor, byte_buffer, 100); // You would want to do something useful here NSLog([NSString stringWithCString:byte_buffer length:numBytes]); } } </code></pre> <p><strong>To write to the serial port:</strong></p> <pre><code>uint8_t val = 'A'; write(serialFileDescriptor, val, 1); </code></pre> <p><strong>To list availble serial ports:</strong></p> <pre><code>io_object_t serialPort; io_iterator_t serialPortIterator; // Ask for all the serial ports IOServiceGetMatchingServices( kIOMasterPortDefault, IOServiceMatching(kIOSerialBSDServiceValue), &amp;serialPortIterator); // Loop through all the serial ports while (serialPort = IOIteratorNext(serialPortIterator)) { // You want to do something useful here NSLog( (NSString*)IORegistryEntryCreateCFProperty( serialPort, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0)); IOObjectRelease(serialPort); } IOObjectRelease(serialPortIterator); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload