Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Tk Realtime data aquisition

by jaschwartz (Novice)
on May 08, 2009 at 15:39 UTC ( [id://762855]=note: print w/replies, xml ) Need Help??


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

I started the big Tk script first, the monitor, and then started the smaller, the sender. The command line window I started it from shows incrementing numbers and its sent random number. But, nothing shows up in the monitor graph. I think some of the issue is that the monitor always shows "Not connected." I am not sure how to force it to connect? I am using a windows xp win32, is this the same as what you are using? After using a netstat -a, I see both the Listening on 7070 and the the established, but yet nothing shows up in the graph, I suspect this may be an issue in the the handle_connection subrutine. I uncommented your "print message line." But, nothing is printed. I suspect that handle_connection is not getting far enough to execute handle_connection. Your thoughts or advice?

Replies are listed 'Best First'.
Re^4: Tk Realtime data aquisition
by zentara (Archbishop) on May 08, 2009 at 17:05 UTC
    I am using a windows xp win32, is this the same as what you are using?

    Eh, sorry no. I use linux, it works. In all likelihood your problem is that, the Tk::fileevent (which monitors and reads the socket), is unreliable on Windows. I'm not going thru the code right now, but that is a very common problem on win32. The quick fix for you, is to remove the fileevent and use a timer ( every 10 or 20 ms ) to read the socket.

    Thats just an educated guess, and it should work. The fileevent problem was supposedly only on win32 pipes, not sockets. So I'm perplexed, you might post another node and ask win32 users if it works for them. You may have other issues I'm unaware of, like your firewall not allowing the connection, or other localhost issues. Possibly its a blocking problem, does it suddenly spurt out alot of data if you let it run for a few minutes? There are problems with getting non-blocking reads on win32 pipes...but this is a socket?

    Another way around it, is to switch to Gtk2.....you can use the same idea to write to a Gnome2::Canvas or Goo::Canvas. Gtk2 has a fix for the win32 fileevent problem, with it's comparable Glib::IO::Watch widget.

    Its all just a guess on my part, I avoid win32. What you should do, is find some Tk code, that works with sockets on win32, and test to see what the code differences are. Maybe you just need a /r/n for newlines, in the sender script, to tell it to flush, but Perl should handle that automagically on your platform.

    print $sock "$send \r\n ";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      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?
        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://762855]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-04-24 08:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found