A simple 'pipe' opening and "Watch" routine

Post your Gambas programming questions here.
Post Reply
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

A simple 'pipe' opening and "Watch" routine

Post by BruceSteers »

I've uploaded this as an example to the gambas wiki.

The ReadMe...
Very simple pipe example.
This shows how to open a pipe file and monitor "Watch" it for incoming text.
(Without comments it's just 20 lines of code)
Written by Bruce Steers, Fully commented to explain the workings.
Once the pipe is opened the app continues to run and will react to any text sent to the pipe.

Pipes can have many uses especially in being able to control/communicate to your application form an outside source.

See My program "GForm" (an interactive shell GUI builder) http://gform.bws.org.uk (advanced)
to see how 2 pipes can be used for 2 way communication between a
shell script and a gambas application.

More info in the source code.
Simple pipe example.zip
The code...
' Gambas class file

Public hFile As File  ' Declare pointer to the pipe file handle (needs to be global)

' This routine is called when the window is opening.
Public Sub Form_Open()

' opening a pipe will lock the application until it gets some text so before
' opening it we 'echo' a blank line to it using the Shell command.
 Shell "echo '' >/tmp/FIFO1"

' Now we open the Pipe file for Reading asigning it to hFile and use the "Watch" arg
' using "Watch" makes it trigger the File_Read() event if it recieves a line of text.
 hFile = Pipe "/tmp/FIFO1" For Read Watch

' Pipe is opened and our "echo" command has unlocked it and allowed our application 
' To get To this point so alert the user what's going on before opening the main window.
 Message("Pipe is opened and this application is now\nfree to continue running while listening for\ntext from the pipe in the background.", "Show Me")
End



' This File_Read() event is trigered when a line of text is sent to the pipe file /tmp/FIFO1
Public Sub File_Read()
 Dim sMsgText As String
' Here we ReadLine from the hFile pointer While not at the Eof (End of file).
 While Not Eof(hFile)
  sMsgText = Trim(hFile.ReadLine())
' If the text is not a blank line then react to it...
  If sMsgText <> "" Then TextArea1.Text = "\nReceived text from the pipe...\n\n'" & sMsgText & "'"
 Wend
End


' On closing the application window this Form_Close() routine is called so 
' here we can make sure the pipe file is closed and deleted.
Public Sub Form_Close()
 If hFile Then hFile.Close()
 Kill "/tmp/FIFO1"
End


' the "Close" button triggers this event.
Public Sub btnClose_Click()
 Me.Close()
End

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: A simple 'pipe' opening and "Watch" routine

Post by BruceSteers »

I just added the following important info about pipes in the wiki..

No more locking up the app untill it gets some text when opening since 3.15 :)


Useful Tip:
In versions of Gambas below V 3.15 opening the pipe even for Watch would use the Linux default behaviour of locking your program until the pipe received some text.
A simple fix for this was to 'echo' a blank line to the pipe using the Shell command just before opening like this...
 Shell "echo '' >/tmp/fifo1"
 hFile = Pipe "/tmp/fifo1" For Watch
Then opening the pipe would instantly receive some text and your program would continue to run.

As from Gambas V3.15+ Opening a pipe no longer locks the program waiting for text to be received as this behaviour was not considered useful.

So using the echo command is no longer needed but for backward compatibility (If your app is intended for others) it is advisable to still use it as your program will work okay on Gambas V3.15 without it but will lock on a lesser version runtime.
If at first you don't succeed , try doing something differently.
BruceS
PartierSP
Posts: 57
Joined: Tuesday 30th November 2021 12:14am
Location: Canada
Contact:

Re: A simple 'pipe' opening and "Watch" routine

Post by PartierSP »

Hey Bruce, any chance of this working on a device file like /dev/input/js0? I tried editing the Pipe line to point to the joystick, and had to rem the Shell echo line as the file is read only. But I'm getting the program lock-up issue you described. Even moving the joystick doesn't unlock the program.

I'm trying to read the raw data from a joystick just to see if I can make heads or tails out of it. I found some C++ code that deciphers js0 so I wanted to play with it a bit Gambas. The device file is world readable, so it shouldn't be crashing on me for that.
Post Reply