本文介绍了尝试在MacOS中打开串行端口时,open(2)函数挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,当我尝试打开串行端口时,打开功能永远不会返回.并非一直如此,如果我将USB拔出到串行适配器并将其重新插入,问题会消失一会儿.我的代码如下:

I've run into a problem where the open function never returns when I try to open a serial port. It doesn't happen all the time, and the problem disappears for a while if I unplug my USB to serial adapter and plug it back in. My code looks like this:

fileDescriptor = open(bsdPath, O_RDWR | O_NOCTTY);

其中bsdPath是/dev/cu.KeySerial1.我尝试将O_NONBLOCK选项添加到open命令中,但是它仍然挂起.

where bsdPath is /dev/cu.KeySerial1 . I've tried adding the O_NONBLOCK option to the open command, but it still hangs.

我当然想了解为什么会这样.我的信念是,指定O_NONBLOCK的任何问题打开后都将返回,即使它无法打开端口也是如此.如果无法打开端口,则fileDescriptor应该为-1并设置errno(在打开调用之后我会立即检查).当然,这不会发生.我的假设不正确吗?遇到错误时,即使指定了O_NONBLOCK,open()也永远不会返回有什么已知的原因吗?

Of course I'd like to understand why this is happening. My belief is that whatever the problem, with O_NONBLOCK specified, open should return no matter what even if it was unable to open the port. If it's unable to open the port, fileDescriptor should be -1 and errno should be set (I check for this immediately after the call to open). Of course, this isn't happening. Is my assumption incorrect? Is there some known reason for open() to never return even with O_NONBLOCK specified when an error is encountered?

使用最新版本的Prolific PL-2303驱动程序和10.7.2上基于PL-2303的USB到串行适配器的USB,我今天能够再次重现此问题.一些注意事项:

Using the latest version of the Prolific PL-2303 driver with a PL-2303 based USB to serial adapter on 10.7.2, I've been able to reproduce this problem again today. A few notes:

  • 当挂在open()调用中时,使用command-不能中断该过程. (控制C).
  • 运行ps -avx显示进程的U的进程状态代码.我不确定这段代码是什么意思.它没有出现在Googling找到的ps的手册页中.我的机器上ps的手册页中没有进程状态代码的列表.也许特定于ps的Mac(10.4+?)版本?
  • 我注意到在此问题首次出现之前的运行中,我调用ioctl()将端口上的选项重置为它们的状态,然后才更改它们以供在程序中使用.我不得不杀死该程序(通过Xcode的调试器).之后,在该程序的下一次启动时,open()挂起了...
  • When hung in the open() call, the process is not interruptible using command-. (control-C).
  • Running ps -avx shows a process state code of U for the process. I'm not sure what this code means. It doesn't appear in man pages for ps found by Googling. There is no listing of process state codes in the man page for ps on my machine. Perhaps it's specific to the Mac (10.4+?) version of ps?
  • I noted that on the run immediately before the first appearance of this problem, my call to ioctl() to reset the options on the port back to their state before I changed them for use in my program hung. I had to kill the program (via Xcode's debugger). Immediately after this, on the next launch of the program, open() hung...

推荐答案

问题很可能出在设备驱动程序中.您对O_NONBLOCK的行为是正确的,但是要由驱动程序来正确实现.了解正在使用哪个版本的OS X和哪个USB到串行设备将很有帮助.

The problem is likely in the device driver. You are correct about how O_NONBLOCK should behave, but it is up to the driver to implement that correctly. It would be helpful to know which version OS X and which USB to Serial device is being used.

标准步骤是确保将设备直接插入CPU USB端口(而不是集线器),检查电缆并检查更新的驱动程序.

Standard procedures would be to make sure the device is plugged directly into the CPU USB ports (not a hub), check cables, and check for updated drivers.

此外,当open()处于阻塞状态时,进程是否可以被control-c中断?如果您在阻止过程中使用"ps -aux"查看该过程,那么"STAT"字段表示什么?

Also, when open() is blocking, is the process interruptible by control-c?If you look at the process with "ps -aux" while its blocked, what does the "STAT" field say?

这篇关于尝试在MacOS中打开串行端口时,open(2)函数挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 01:23