简体   繁体   English

在Linux中检测从管道/ FIFO的另一端读取的尝试

[英]Detect an attempt to read from opposite end of pipe/fifo in Linux

I am thinking of implementing a sort of daemon/service in C/C++ for linux, that would communicate with a specific gpib device through shell (using linux-gpib library). 我正在考虑在C / C ++中为Linux实现某种守护程序/服务,该守护程序/服务将通过外壳(使用linux-gpib库)与特定的gpib设备进行通信。 The idea is that the daemon would scan for all existing devices and would create a file/pipe /dev/gpib#-* (where * would be their address on specified gpib bus) for each device. 这个想法是守护程序将扫描所有现有设备,并为每个设备创建一个文件/管道/dev/gpib#-* (其中*是它们在指定gpib总线上的地址)。 The use would be such as of /dev/com# . 使用方式如/dev/com# I could then type into command-line: 然后,我可以输入命令行:

echo "*IDN?" > /dev/gpib1-12

which would send the "*IDN?" 哪个会发送“ * IDN”? string to device 12 on board 1. So far it is a peace of cake... 串到设备1上的设备12上。

The problem starts, when I want to retrieve data from the device. 当我想从设备检索数据时,问题就开始了。 I want it to work analogically, so that 我希望它以类推的方式工作,以便

cat /dev/gpib1-12

would write out what has the device to say... But I can not know which command, I have sent to the device, would make the device to return a string (value) and which wouldn't. 会写出设备要说的内容...但是我不知道我发送给设备的哪个命令会使设备返回字符串(值),而不会。 So my options are: 所以我的选择是:

  1. Repeatedly check ( while-loop ) if the device has anything to reply and send it to the corresponding pipe afterwards. 反复检查( while循环 )设备是否有任何要回复的内容,然后将其发送到相应的管道。
    -or- -要么-
  2. Query the device only when the client program attempts to read from the /dev/gpib#-* pipe. 仅当客户端程序尝试从/dev/gpib#-*管道读取时,才查询设备。 This would have to be served through ' signals ' and ' waits '. 这必须通过“ 信号 ”和“ 等待 ”来实现。

For obvious reasons (performance and/or latency handicap) I do not want to implement solution 1 . 由于明显的原因(性能和/或延迟障碍),我不想实施解决方案1 I do not know how to do the the other thing though... I feel, that it must be possible to implement on the ol'mighty linux, but how? 我不知道该怎么做……我觉得,必须在全能的linux上实现,但是如何呢? I did read this and I think that some spin of the function select() is the right way forward, but I can not figure out how to use it for my problem. 我确实读过这篇文章,并且我认为函数select()某些旋转是正确的前进方法,但是我无法弄清楚如何将其用于我的问题。 I also stumbled upon this , where the guy explains how to do something similar, yet sooo different (code mosfet.c ). 我也偶然发现了这个 ,在那家伙解释了如何做类似但又不同的事情(代码mosfet.c )。

The question is: how can I immediately detect and react upon an attempt to read from the other side of pipe/FIFO/file via signaling, waiting or interrupts? 问题是:如何通过信号,等待或中断立即检测到从管道/ FIFO /文件的另一侧读取的尝试并做出反应?

Thanx for answers. 谢谢答案。


PS: It is half past seven in the morning here (yep another sleepless night), so please excuse my broken English... PS:今天早上七点半(又是不眠之夜),所以请原谅我的英语不好...
PPS: Oh yes, and if anyone would already know of such gpib daemon for linux, or if the think I am asking (accessing individual devices through file I/O) would be possible via the linux-gpib library, please let me know. PPS:哦,是的,如果有人已经知道用于Linux的gpib守护程序,或者是否可以通过linux-gpib库实现我所要求的(通过文件I / O访问单个设备),请告诉我。 I did read the doc's and src's for linux-gpib, but found nothing helpful. 我确实阅读了linux-gpib的文档和src,但没有发现任何帮助。 All the linux-gpib library provides are bindings to C, Python, etc. linux-gpib库提供的所有内容都是对C,Python等的绑定。
PPS: Are there maybe other alternatives to using pipes? PPS:使用管道可能还有其他替代方法吗?

If you just need a nice terminal for your gpib device, you can use python (or even better ipython). 如果您只需要gpib设备的漂亮终端,则可以使用python(甚至更好的ipython)。

linux-gpib comes with python wrappers (for the code look here ). linux-gpib随附python包装器(有关代码,请参见此处 )。 so in your shell open python by typing python In the python interpreter you can easily communicate with the device like this 因此,在您的shell中通过键入python打开python。在python解释器中,您可以像这样轻松地与设备通信

>>>import Gpib
>>>device = Gpib.Gpib(pad=2)

This opens a connection to the gpib device with the primary address 2. To communicate with it simply do 这将打开与主要地址2的gpib设备的连接。要与之通信,只需执行以下操作

>>>device.write('*IDN?')
>>>device.read()
'HEWLETT-PACKARD,33120A,0,8.0-5.0-1.0'

To simplify it even further, use ipython instead of plain python. 为了进一步简化,请使用ipython代替纯python。 This gives you tab-completion and much more. 这使您可以完成制表符补全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM