in reply to Re^4: Tk Realtime data aquisition
in thread Tk Realtime data aquisition

So, I tried a few things the first was to see if the data was coming across the socket. Here's my simple script:
#!/usr/bin/perl use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '7070', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(<$new_sock>) { print $_; } close($sock); __END__
And it works, data comes across the socket, and shows up in the command window. I do think the problem is in the fileevent. But, I can do the read like I did in my small script, but how do I pass what is read to TK and then use it within its subroutines to build the graph?

Replies are listed 'Best First'.
Re^6: Tk Realtime data aquisition
by zentara (Cardinal) on May 09, 2009 at 11:00 UTC
    Hi, a couple of quick ways....clunky hacks for win32.
    while(<$new_sock>) { my $input = chomp $_; #print $_; $canvas->update_graph( $input ) # pseudocode to demo point :-) $mw->update; # force the event loop to auto update the window }

    Ideally, you don't want to use a $mw->update in a while() loop. A timer is better, but may not be as fast.....although it will allow you to run other subs in the eventloop simultaneously with the socket connection.... like watching for a value to trip an alarm.

    my $timer = $mw->repeat(10, \&update_graph);
    thats better. Then in the update_graph sub, read your socket, and update the canvas. You can start a second timer to watch for values, as they come in( store the last 10 values in a buffer)). It's all an event-loop system underneath......very powerful and useful once you get the idea. :-) Gtk2's event-loop is even more powerful than Tk's.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku