A simple 'pipe' opening and "Watch" routine
Posted
#1
(In Topic #476)
Guru

The ReadMe…
(attachment removed)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.
The code…
Code (gambas)
- ' Gambas class file
- ' This routine is called when the window is opening.
- ' 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.
- ' 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")
- ' This File_Read() event is trigered when a line of text is sent to the pipe file /tmp/FIFO1
- ' Here we ReadLine from the hFile pointer While not at the Eof (End of file).
- ' If the text is not a blank line then react to it...
- ' On closing the application window this Form_Close() routine is called so
- ' here we can make sure the pipe file is closed and deleted.
- Kill "/tmp/FIFO1"
- ' the "Close" button triggers this event.
Posted
Guru

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…
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.
Posted
Regular

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.
1 guest and 0 members have just viewed this.


