Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Serial Port

by PilotinControl (Pilgrim)
on Aug 12, 2015 at 16:41 UTC ( #1138324=perlquestion: print w/replies, xml ) Need Help??

PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon Monks!

My question relates to the Win32-SerialPort module and an Arduino and here is my question and code below:

1.) Sending Data to and from the Arduino is not an issue.
2.) When data is received from the Arduino and is read by the Perl script the first action takes place which lights up the LEDs on the Arduino Board and shades a button on my Perl/Tk script.
3.) The problem is when the lights on the Arduino go off it sends a command via the serial port and the Perl Script is suppose to read that data and the button is no longer shaded.

However, this is not being done. I have confirmed the command gets sent by the Arduino and I can verify this by using the Arduinos serial monitor. I believe the problem lies when trying to read the data from when the lights go off and that is where I am stuck. Thanks in advance!

use strict; use warnings; use Tk; use Win32::SerialPort; require Tk::Balloon; my $port = Win32::SerialPort->new("COM7") or die "Open Port Failed. $! +\n"; $port->is_rs232; # SET UP THE SERIAL PORT 9600, 81N ON THE USB FTDI DRIVER $port->initialize(); $port->baudrate(9600); $port->databits(8); $port->parity("none"); $port->stopbits(1); $port->write_settings || undef $port; sleep(3); my $mw = MainWindow->new(); $mw->geometry('1020x550+180+600'); $mw->configure(-title => 'TEST'); my $balloon = $mw->Balloon(); my $buffer3 = $mw->Frame; $buffer3->pack(-side => 'left', -padx => 31, -anchor => 'nw'); my $frame3 = $mw->Frame; $frame3->pack(-side => 'left', -anchor => 'nw'); my $sig4 = $frame3->Button(-activebackground => 'red', -activeforegrou +nd => 'black', -text, 'TEST ALL ASPECTS', -anchor => 'w', -command => + sub{print STDOUT " Test All Aspects\n"; &testall_aspects}); $sig4->pack(-side => 'top', -fill => 'both', -anchor => 'w'); $sig4->configure(-background => "gray", -foreground => "black"); $balloon->attach($sig4, -balloonmsg => "Test All Aspects"); MainLoop; sub testall_aspects { no warnings 'uninitialized'; $signalblock100->configure(-image => $mw->Photo(-file => 'testall-yell +ow-signal.gif')); $sig1->configure(-background => "green", -foreground => "black"); my $testall = "t"; # THIS CODE GETS SENT TO THE ARDUINO $port->write($testall); $port->lookclear; while(1) { my $data = $port->lookfor(); if ($data) { print STDOUT "$time Signal Driver Message Received: (" . $data . ")\n" +; } if ($data =~ /ON/) { print STDOUT "Message Received: (" . $data . ")\n"; return; } if ($data =~ /OF/) { print STDOUT "Message Received: (" . $data . ")\n"; $signalblock100->configure(-image => $mw->Photo(-file => 'noaspect-yel +low-signal.gif')); } } }

Replies are listed 'Best First'.
Re: Serial Port
by RichardK (Parson) on Aug 13, 2015 at 10:03 UTC

    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.

      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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1138324]
Approved by shmem
Front-paged by Discipulus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2022-01-26 17:33 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (69 votes). Check out past polls.