in reply to Serial Port

Your problem description isn't very clear, but I think the issue is that you stop listening once you've received the 'ON' response. You print "message received" then the return out of testall_aspects, so there nothing left to listen for the 'off' message.

To deal with asynchronous messages from the arduino you could use a repeat callback to poll the serial port (see Tk::after and Tk::callbacks), something like :-

$widget->repeat(100,\&handler); ... sub handler { my $data = $port->lookfor; return unless $data; # do stuff ... }

Also, with this kind of problem it's worth drawing a sequence diagram to show how the messages get passed between the 2 systems.

Replies are listed 'Best First'.
Re^2: Serial Port
by PilotinControl (Pilgrim) on Aug 13, 2015 at 15:59 UTC

    Thanks RichardK,

    I did tried adding this code

    $sig4->repeat(100,\&handler); sub handler { my $data = $port->lookfor; return unless $data; print STDOUT "$Message Received: (" . $data . ")\n"; exit; # THIS WAS ADDED TO MAKE SURE THE CODE DOES WORK...IT DID WORK A +ND THE PROGRAM EXITED }
    and some how when each button is pushed for $sig1, $sig2 etc...the handler sub is invoked and thats strange because only $sig4 is assigned to the handler....and the second issue is that the code is still not polling the Arduino looking for "OF"...the button has to be pressed again....The Arduino is sending data after the 8 seconds as the TX light flashes...the perl code needs to continue in a loop some how to keep looking for the "OF" trigger.
    UPDATE: And to clear the buffer as the perl code is reading what the Arduino last sent

      That repeat callback is not tied to anything. The Tk event loop will call the handler every 100 ms. You will need to write code in the handler to deal with every message the arduino will send.

        RichardK a big thank you! It works! and the code is below:

        sub handler { my $timer = $mw->repeat(100,\&sighandler ); sub sighandler { my $data = $port->lookfor; return unless $data; # do stuff print STDOUT "$time Message Received: (" . $data . ")\n"; if ($data =~ /OF/) { $signalblock100->configure(-image => $mw->Photo(-file => 'picture.jpg' +)); } $port->lookclear; } }
        Had to use a sub with in a sub...unless there is a different way to do it..the code works as follow:
        1.) Button is Pushed = LEDs turn on for 8 seconds
        2.) LEDs go out and the word OF is sent from the Arduino and is Polled by the Perl Script and the button that is shaded is now unshaded and the RX/TX buffers have been purged. Thanks again!