PHP-GTK2 Cookbook Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Re:infinite loop (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:infinite loop


#5211
tMS1 (User)
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
infinite loop 2 Years, 3 Months ago Karma: 0  
Hello all,

I'm working on a project to read information on a serial port. I used PHP-GTK to get a nice UI.
Everything is working fine until i need to read on that port. Problem is i think i need to use an infinite loop to keep scanning data incoming from COM3. Data received should be displayed in a GtkTreeView, appending rows to the model.
I made a button with a callback function to start reading. That's where i got the problem cause when i start reading, the gtk main loop hangs which is finally logical.

After long research on what was the problem, i found that thread on the forum, where someone had the same kind of problem.
The solution suggested was to use proc_open to fork to a new process to read the input.

Here is where i'm stuck at the moment. Using proc_open still makes the gtk main loop hanging on.

Here's the code i wrote :
main.php
Code:
<?php
        error_reporting(E_ALL);
	if (!class_exists("gtk"))
	{
		dl("php_gtk2.dll");
	}
	
	$window = new GtkWindow();
	$button = new GtkButton("Start!");
	$vBox   = new GtkVBox();
	$frame  = new GtkFrame("Gencods Lus");
	$scrollWin = new GtkScrolledWindow();
	$model = new GtkListStore(Gobject::TYPE_STRING);
	$list 	= new GtkTreeView($model);
	$column = new GtkTreeViewColumn('Gencod');
	$cell_renderer = new GtkCellRendererText();
	
	$window->connect("destroy", "destroy");
	
	$window->set_border_width(10);
	$window->set_default_size(400, 500);
	$window->set_position(1);
	
	$button->connect("clicked", "start");
	
	$list->append_column($column);
	$cell_renderer->set_property('width', 280);
	$cell_renderer->set_property('editable', false);
	$column->pack_start($cell_renderer, true);
	$column->set_attributes($cell_renderer, 'text',   0);
	$root = $model->insert(0);
	$model->set($root, 0, 'TEST');
	$model->append(array('TEST2'));
	$model->prepend(array('TEST3'));
	
	$scrollWin->add($list);
	$frame->add($scrollWin);
	
	$vBox->pack_start($frame, true);
	$vBox->pack_start(new GtkHSeparator(), false, false, 3);
	$vBox->pack_start($button, false);
	
	
	$window->add($vBox);
	$window->show_all();
	Gtk::main();
	
	
	// Callback to start reading port
	function start($button)
	{
		global $model;
		$out = '';
		
		print "Here we go !!\n";
		
		$ret = _exec('php Scripts/read_port.php', $out);
		$str = $out[0];
		//echo $out[1];
		echo $str = trim($str);
		$model->append(array($str));
		
	}
	
	function destroy() 
	{
		Gtk::main_quit();
	}
	
	function _exec($cmd, &$out = null)
	{
		$desc = array(
			1 => array("pipe", "w"),
			2 => array("pipe", "w")
		);

		$proc = proc_open($cmd, $desc, $pipes);

		$ret = stream_get_contents($pipes[1]);
		$err = stream_get_contents($pipes[2]);

		fclose($pipes[1]);
		fclose($pipes[2]);

		$retVal = proc_close($proc);

		if (func_num_args() == 2) $out = array($ret, $err);
		return $retVal;
	}

?>
And the read_port.php :
Code:
	error_reporting(E_ALL);
	
	// for($i=0; $i <= 100;$i++) // test
		// echo $i."\n";

	while($str = fgets(STDIN))
		echo $str;
	
	ser_open( "COM3", 9600, 8, "None", "1", "None" );
	echo '> ';
	while(1)
	{
		$read = ser_read();
		echo $read;
	}
If anyone could give me some help on that. I don't really know how it works and how i can get the UI staying active as i read infos on the serial handle. Hope i was clear enough. Thanks by advance
 
  The administrator has disabled public write access.

#5215
kksou (Admin)
Admin
Posts: 1715
graph
User Online Now Click here to see the profile of this user
Re:infinite loop 2 Years, 3 Months ago Karma: 27  
Hi,

You can try using Gtk::idle_add() or Gtk::timeout_add() and do the reading during the idle time or timeout event.

Please refer to the following sample codes:

Sample Code 67: How to display progress bar while processing long task - Part 1?

Sample Code 68: How to display progress bar while processing long task - Part 2 using_idle_add?

Once you understand the above sample codes, instead of display progress bar, you do your reading of data from COM3. Of course, it would be great to display some kind of Reading-In-Progress to the user, e.g. to inform the user how much data has been read so far.

You can even do the reading and display the progress using GtkTreeview (of course also using Gtk::idle_add() or Gtk::timeout_add()) e.g.

Sample Code 365: How to display dynamic progress bar in GtkTreeView - Part 1?

Sample Code 366: How to display dynamic progress bar in GtkTreeView - Part 2?

Hope this helps.

Regards,
/kksou
 
  The administrator has disabled public write access.

#5217
tMS1 (User)
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Re:infinite loop 2 Years, 3 Months ago Karma: 0  
Hey kksou,
Thanks you for answering!
That's indeed what i did, found yesterday this way of using Gtk::timeout_add(1000, 'read_port', $textBuffer);
I'm glad to find it my own way (but was so lonnnng to find) and also that you confirm it's a good method.

Thank you, your forum is great and i'll keep browsing it for php-gtk for sure. Your tutorials are really good job too!

Cheers

tMS1
 
 
Last Edit: 2012/03/29 15:30 By tMS1.
  The administrator has disabled public write access.
#5218
kksou (Admin)
Admin
Posts: 1715
graph
User Online Now Click here to see the profile of this user
Re:infinite loop 2 Years, 3 Months ago Karma: 27  
Hi,

Although Gtk::timeout_add() will work, would encourage you to try using Gtk::idle_add(). You will find that your application will be more responsive, as you get control whenever the system is idling (which is the case most of the time).

Give it a try. You will find that Gtk::idle_add() is more powerful and provides better user interaction to work with for such situation.

Regards,
/kksou
 
  The administrator has disabled public write access.
#5219
tMS1 (User)
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Re:infinite loop 2 Years, 3 Months ago Karma: 0  
Ok !

I'm gonna try this. I just hope i wont loose data between the calls to the function, i don't really know how my serial class buffer work, thanks again ! :)

tMS1
 
  The administrator has disabled public write access.
#5870
chennaiiq (User)
Fresh Boarder
Posts: 3
graphgraph
User Offline Click here to see the profile of this user
Re:infinite loop 1 Year, 3 Months ago Karma: 0  
We are using now MSComm1_onComm event in vb6.

How can I do this in PHP-GTK2.

Please give me detailed reply.

Thank you very much
 
  The administrator has disabled public write access.



Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved