Matlab从FIFO读取与FOPEN超时 [英] Matlab read from fifo with fopen timeout

查看:180
本文介绍了Matlab从FIFO读取与FOPEN超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用命名管道(fifo)在python和MATLAB之间进行通信.从管道读取的MATLAB代码可正常使用,但如果未向fifo写入任何内容,则该代码将挂起.我希望当没有可用数据时,它会适当地超时.

I'm working with named pipes (fifo) to communicate between python and MATLAB. The MATLAB code to read from the pipe is functional but it hangs if nothing has been written to the fifo. I would prefer it would gracefully timeout when no data is available.

如果管道存在(以bash格式):

If the pipe exists (in bash):

$ mkfifo pipe_read

但没有数据打开MATLAB命令:

but has no data the MATLAB open command:

>> fid = fopen('pipe_read', 'r'); 

挂起,直到有可用数据为止:

hangs until data is available:

$ echo "test data" >> pipe_read

如果没有可用数据,我希望fopen返回一个指示错误(即类似于-1的文件,而不是文件不存在时),而不是永远阻塞.

Rather than blocking forever I would like fopen to return a file-id that indicates an error (i.e. similar to -1 as it does when the file does not exist) if there is no data available.

是否存在类似于命令中可用的异步读取和写入串行仪器的解决方案: http://www.mathworks.com/help/matlab/ref/readasync.html 吗?

Could there be a solution similar to the asynchronous reads available in the commands for writing and reading to serial instruments: http://www.mathworks.com/help/matlab/ref/readasync.html ?

或者fopen是否可以嵌入到启用超时的matlab计时器对象中?

Or possibly fopen could be embedded into a matlab timer object that enables a timeout?

之前曾有人问过,但没有答案: 从命名管道(fifo)读取的Matlab

This has been asked before but without an answer: Matlab read from named pipe (fifo)

推荐答案

我很确定问题实际上与Matlab的fopen无关,而与底层的open系统调用有关.通常,仅当同时存在读取器和写入器时才使用管道或FIFO才有意义,因此,默认情况下,open(2)将阻塞直到FIFO的另一端也已打开.

I'm pretty sure the issue is not actually with Matlab's fopen, but the underlying open system call. Generally, the use of a pipe or FIFO only makes sense when there exists both a reader and a writer, and so, by default, open(2) will block until the other end of the FIFO has been opened as well.

我认为将fopen调用嵌入到任何其他Matlab对象中是行不通的.据我所知,唯一可以解决此问题的方法是编写自己的fopen版本,作为专门的Mex函数.在这种情况下,您可以使用O_NONBLOCK标志or调用带有任何所需读/写标志的open(2).但是深入研究man 2 openERRORS部分下,您可以看到,如果设置了< O_NONBLOCKO_WRONLY,文件是FIFO,并且没有进程,则返回ENXIO打开以供阅读".这意味着您需要确保在 Matlab尝试打开进行写入之前(反之亦然),Python必须先打开FIFO以进行读取.

I don't think it will work to embed the fopen call in any other Matlab object. As far as I'm aware, the only way to circumvent this is to write your own version of fopen, as a specialized Mex function. In this case, you can make a call to open(2) with the O_NONBLOCK flag or'd with whatever read/write flag you'd like. But digging around in man 2 open, under the ERRORS section, you can see that ENXIO is returned if "O_NONBLOCK and O_WRONLY are set, the file is a FIFO, and no process has it open for reading". That means you need to make sure that Python has opened the FIFO for reading before Matlab tries to open for writing (or vice versa).

最后一点,请记住,Matlab的fopen返回文件描述符的句柄.您的Mex函数可能应该对此进行镜像,因此您可以毫无问题地将其传递给fread/fscanf/etc.

As a final point, keep in mind that Matlab's fopen returns a handle to a file descriptor. Your Mex function should probably mirror that, so you can pass it around to fread/fscanf/etc without issues.

这篇关于Matlab从FIFO读取与FOPEN超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆