klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

accessing a fifo file

Wed Jul 31, 2019 12:38 am

Hello

I am making a fake radar.

I have setup a a fifo.

Code: Select all

mkfifo -m a=rw aprs.fifo
nano aprs.fifo
rtl_fm -p 7.826 -f 144.390M -s 22050|multimon-ng -t raw -a AFSK1200 -f alpha -A /dev/stdin > aprs.fifo
I would like to access the aprs.fifo with a quick look to see if there is new info on the file.

There are other process that I would like to check, like gps.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: accessing a fifo file

Wed Jul 31, 2019 8:56 am

klintkrossa wrote:
Wed Jul 31, 2019 12:38 am
I would like to access the aprs.fifo with a quick look to see if there is new info on the file.
Do you want to access it from a bash script, python script, c code etc etc?
From bash the "tail" command can be used to read the last few lines of a file and the number of lines read is configurable. Read "man tail"
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

blimpyway
Posts: 338
Joined: Mon Mar 19, 2018 1:18 pm

Re: accessing a fifo file

Wed Jul 31, 2019 7:46 pm

Code: Select all

$ mkfifo aprs.fifo
$ tail -f aprs.fifo &
$ echo blablabla > aprs.fifo
$ echo more data > aprs.fifo 

tail -f just keeps waiting for more bytes to be appended to a file and displays them as they come out of pipe. the "&" puts it in background.

hippy
Posts: 7459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: accessing a fifo file

Thu Aug 01, 2019 1:46 pm

klintkrossa wrote:
Wed Jul 31, 2019 12:38 am
I would like to access the aprs.fifo with a quick look to see if there is new info on the file.
This is what I've used, noting this is in the Python forum area -

Code: Select all

while True:
  with open("aprs.fifo", "r") as f:
    for s in f:
      s = s.strip()
      print(s)
klintkrossa wrote:
Wed Jul 31, 2019 12:38 am
There are other process that I would like to check, like gps.
Given that the above code appears to block, it is probably best to set up multiple threads for each FIFO you are reading.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: accessing a fifo file

Thu Aug 01, 2019 9:07 pm

scotty101 wrote:
Wed Jul 31, 2019 8:56 am
klintkrossa wrote:
Wed Jul 31, 2019 12:38 am
I would like to access the aprs.fifo with a quick look to see if there is new info on the file.
Do you want to access it from a bash script, python script, c code etc etc?
From bash the "tail" command can be used to read the last few lines of a file and the number of lines read is configurable. Read "man tail"
Thanks for the need to clarify.

I would like to access the program inpython3. when I do access the program with "with open() as" or "with popen() as" it stalls. and of coarse if it is accessed with "x = open()" it creates a mess in the file.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

hippy
Posts: 7459
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: accessing a fifo file

Fri Aug 02, 2019 12:59 pm

klintkrossa wrote:
Thu Aug 01, 2019 9:07 pm
I would like to access the program inpython3. when I do access the program with "with open() as" or "with popen() as" it stalls. and of coarse if it is accessed with "x = open()" it creates a mess in the file.
See my example program above. Yes, the "with open()" does seem to stall - or the "for s in f:" does - when there is nothing in the FIFO, but that shouldn't be a problem.

Write some simple reader and writer code to get to understand how FIFO's work. Then apply that to what you are wanting to actually do.

User avatar
Paeryn
Posts: 2952
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: accessing a fifo file

Fri Aug 02, 2019 6:13 pm

You can check to see if there is any data to be read from a fifo with an ioctl(FIONREAD), you need to use something like an array.array to store the result in.

Code: Select all

import array
import time
import fcntl
import termios

bytesLeft = array.array('l', [0])
f = open('test.fifo', 'r')
while True:
  fcntl.ioctl(f, termios.FIONREAD, bytesLeft, 1)
  if bytesLeft[0] == 0:
    print('fifo is empty')
  else:
      print('length of data in buffer =', bytesLeft[0])
      for s in f:
      s = s.strip()
      print(s)
  time.sleep(1)
She who travels light — forgot something.

Return to “Python”