cocoa – 无法使用GCD调度源从串行端口文件描述符读取

前端之家收集整理的这篇文章主要介绍了cocoa – 无法使用GCD调度源从串行端口文件描述符读取前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从串口读取时,我无法使用Grand Central Dispatch Source事件.

我将dispatch_source_create与DISPATCH_SOURCE_TYPE_READ一起使用,以便当从与串行端口关联的fileDescriptor中读取数据时,操作系统将运行我的代码块.这是我的代码

- (void) receiveThread
{
    globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    readSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,[self fileDescriptor],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);
}

当程序运行时,第一次将串行数据发送到端口时调用该块.然后我在控制台中收到一条消息

[Switching to process 11969 thread 0x6603]

当更多字符被发送到串行端口时,不会调用代码块.我仍然可以从串口发送字符,我可以确认正在发送字符,但块不会再次运行.

从Web上的文档和示例我希望只要串行缓冲区中有字符就可以重复调用块.

解决方法

@KazukiSakamoto指出文件描述符应该设置O_NONBLOCK是正确的.我发现了其他一些可能让你感到困扰的问题:当你应该使用缓冲区时,你正在使用& buffer.此外,您有一个512字节的缓冲区,然后您读取最多512个字节并将下一个设置为0(用于空终止).如果您实际读取了512个字节,则会导致缓冲区溢出.此外,看起来readSource是一个iVar,你在块中引用self.这可能会创建一个保留周期,应该避免.

无论如何,我编写了最简单的小应用程序,然后缩短了我的串口的引脚2和3,所以写出的任何内容都会被回显,然后在我的应用程序中连接一个按钮来发送一些数据.像魅力一样工作!这是代码

@implementation SOAppDelegate
{
    int fd;
    dispatch_source_t readSrc;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    fd = open("/dev/mySerialPort",O_RDWR | O_NONBLOCK);
    __block dispatch_source_t blockReadSrc = readSrc = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,fd,dispatch_get_main_queue());

    dispatch_source_set_event_handler(readSrc,^{
        NSLog(@"expected: %lu\n",dispatch_source_get_data(blockReadSrc));
        ssize_t numBytes;
        do {
            char buffer[513];
            numBytes = read((int)dispatch_source_get_handle(blockReadSrc),512);
            buffer[numBytes] = '\x000'; //make sure that the string is terminated.
            NSLog(@"numBytes: %ld\n",numBytes);
            if (numBytes != -1)
            {
                NSLog(@"String:%@\n",[NSString stringWithUTF8String: buffer]);
            }
        } while (numBytes > 0);
    });

    dispatch_resume(readSrc);
}

- (IBAction)sendData: (id)sender
{
    write(fd,"foobar",6);
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    if (fd > 0) close(fd); fd = 0;
    if (readSrc) dispatch_release(readSrc); readSrc = nil;
    return NSTerminateNow;
}

@end

猜你在找的cocoa相关文章