I'm having problems using Grand Central Dispatch Source events when reading from serial ports.
I use dispatch_source_create with DISPATCH_SOURCE_TYPE_READ so that the OS runs my code block when there is data to read from the fileDescriptor file associated with the serial port. Here is my code
- (void) receiveThread { globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, [self fileDescriptor], 0, globalQueue); dispatch_source_set_event_handler(readSource, ^{ char buffer[512]; NSString *bar; ssize_t numBytes; int expected; expected = dispatch_source_get_data(readSource); printf("expected:%d\n",expected); do { numBytes = read([self fileDescriptor], buffer, 512); buffer[numBytes] = '\x000'; //make sure that the string is terminated. printf("bytes:%ld\n",numBytes); if (numBytes != -1) { bar = [NSString stringWithCString:&buffer]; //printf("bytes:%ld\n",numBytes); NSLog(@"String:%@\n",bar); [[NSNotificationCenter defaultCenter] postNotificationName:EISerialTextDidArrive object:bar]; } } while (numBytes > 0); }); dispatch_resume(readSource); }
When the program starts, the block is called first when serial data is sent to the port. Then I get a message in the console
[Switching to process 11969 thread 0x6603]
When more characters are sent to the serial port, the code block is not called. I can still send characters from the serial port, and I can confirm that the characters are sent, but the block does not start a second time.
From the documentation and examples on the Internet, I expect the block to be called multiple times while there are characters in the serial buffer.
source share