A simple 'pipe' opening and "Watch" routine

Post

Posted
Rating:
#1 (In Topic #476)
Guru
BruceSteers is in the usergroup ‘Guru’
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) Home - Bishop Wordsworth's School (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.
(attachment removed)
The code…

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public hFile As File  ' Declare pointer to the pipe file handle (needs to be global)
  4.  
  5. ' This routine is called when the window is opening.
  6. Public Sub Form_Open()
  7.  
  8. ' opening a pipe will lock the application until it gets some text so before
  9. ' opening it we 'echo' a blank line to it using the Shell command.
  10.  Shell "echo '' >/tmp/FIFO1"
  11.  
  12. ' Now we open the Pipe file for Reading asigning it to hFile and use the "Watch" arg
  13. ' using "Watch" makes it trigger the File_Read() event if it recieves a line of text.
  14.  hFile = Pipe "/tmp/FIFO1" For Read Watch
  15.  
  16. ' Pipe is opened and our "echo" command has unlocked it and allowed our application
  17. ' To get To this point so alert the user what's going on before opening the main window.
  18.  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")
  19.  
  20.  
  21.  
  22. ' This File_Read() event is trigered when a line of text is sent to the pipe file /tmp/FIFO1
  23. Public Sub File_Read()
  24.  Dim sMsgText As String
  25. ' Here we ReadLine from the hFile pointer While not at the Eof (End of file).
  26.  While Not Eof(hFile)
  27.   sMsgText = Trim(hFile.ReadLine())
  28. ' If the text is not a blank line then react to it...
  29.   If sMsgText <> "" Then TextArea1.Text = "\nReceived text from the pipe...\n\n'" & sMsgText & "'"
  30.  
  31.  
  32. ' On closing the application window this Form_Close() routine is called so
  33. ' here we can make sure the pipe file is closed and deleted.
  34. Public Sub Form_Close()
  35.  If hFile Then hFile.Close()
  36.  Kill "/tmp/FIFO1"
  37.  
  38.  
  39. ' the "Close" button triggers this event.
  40. Public Sub btnClose_Click()
  41.  Me.Close()
  42.  
  43.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
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…

Code (gambas)

  1.  Shell "echo '' >/tmp/fifo1"
  2.  hFile = Pipe "/tmp/fifo1" For Watch
  3.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
PartierSP is in the usergroup ‘Regular’
 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.
Online now: No Back to the top
1 guest and 0 members have just viewed this.