简体   繁体   中英

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). 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. The use would be such as of /dev/com# . I could then type into command-line:

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

which would send the "*IDN?" string to device 12 on board 1. So far it is a peace of cake...

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.
    -or-
  2. Query the device only when the client program attempts to read from the /dev/gpib#-* pipe. 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 . 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? 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. I also stumbled upon this , where the guy explains how to do something similar, yet sooo different (code 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?

Thanx for answers.


PS: It is half past seven in the morning here (yep another sleepless night), so please excuse my broken English...
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. I did read the doc's and src's for linux-gpib, but found nothing helpful. All the linux-gpib library provides are bindings to C, Python, etc.
PPS: Are there maybe other alternatives to using pipes?

If you just need a nice terminal for your gpib device, you can use python (or even better ipython).

linux-gpib comes with python wrappers (for the code look here ). so in your shell open python by typing python In the python interpreter you can easily communicate with the device like this

>>>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

>>>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. This gives you tab-completion and much more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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