Perl Code
# ==================================================================
# Perl script listens for the Arduino messages and displays them
#
# Windows 7, ActivePerl (32 bit)
# install win32::SerialPort module
#
# Based on: http://www.windmeadow.com/node/38
# ==================================================================
use strict;
use warnings;
require Win32::SerialPort;
# setup the serial port on the USB FTDI driver
my $portname = "COM4";
my $port = Win32::SerialPort->new($portname)
or die "Can't open serial port (" . $portname . ")\n$^E\n";
$port->initialize();
$port->databits(8);
$port->baudrate(9600);
$port->parity("none");
$port->stopbits(1);
$port->debug(0);
# print "Serial port baudrate: " . $port->baudrate() . "\n";
# define line termination for $port->lookfor()
# Note: Arduino serial.println terminates each line/string sent with a "\r\n"
$port->are_match("\r\n");
my $data;
while(1)
{
# poll looking for data
$data = $port->lookfor();
# -------------------------------------------------------------------
# the default EOL for $port->lookfor() is "\n". it is removed by the
# method. to remove the remaining "\r" use $data =~ s/\r$//;
# -------------------------------------------------------------------
# if we get data, display it
if ($data)
{
print "String length: " . length($data) . "\n";
print "String Received: (" . $data . ")\n";
}
# sleep for a second (look up usleep for a shorter period)
sleep(1);
}
Arduino code
// ======================================================
// Send "Hello World" message from the Arduino
// to the computer
// ======================================================
const int led = 13; // led pin
void setup()
{
pinMode(led,OUTPUT); // pic OUTPUT
Serial.begin(9600); // set port baud rate
}
void loop()
{
Serial.println("Hello World"); // send message
flash(500); // flash led (milliseconds)
delay(4000); // pause (milliseconds)
}
void flash(int duration)
{
digitalWrite(led,HIGH);
delay(duration);
digitalWrite(led,LOW);
delay(duration);
}