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

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

概述从串口读取时,我无法使用Grand Central Dispatch Source事件. 我将dispatch_source_create与DISPATCH_SOURCE_TYPE_READ一起使用,以便当从与串行端口关联的fileDescriptor中读取数据时, *** 作系统将运行我的代码块.这是我的代码 - (void) receiveThread{ globalQueue = disp 从串口读取时,我无法使用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 – 无法使用GCD调度源从串行端口文件描述符读取全部内容,希望文章能够帮你解决cocoa – 无法使用GCD调度源从串行端口文件描述符读取所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001143.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存