Arduino and Perl

Overview

This article will show you what you need to know for communicating with the Arduino in Perl.

This article assumes that you know how to handle serial on the Arduino side. All code here is Perl.

Requirements

In order to communicate between Perl and the Arduino you're going to need Perl's Device::SerialPort module.

Serial in Perl

  1. # Set up the serial port
  2. use Device::SerialPort;
  3. my $port = Device::SerialPort->new("/dev/tty.usbserial");
  4.  
  5. # 19200, 81N on the USB ftdi driver
  6. $port->baudrate(19200); # you may change this value
  7. $port->databits(8); # but not this and the two following
  8. $port->parity("none");
  9. $port->stopbits(1);
  10.  
  11. # now catch gremlins at start
  12. my $tEnd = time()+2; # 2 seconds in future
  13. while (time()< $tEnd) { # end latest after 2 seconds
  14.   my $c = $port->lookfor(); # char or nothing
  15.   next if $c eq ""; # restart if noting
  16.   # print $c; # uncomment if you want to see the gremlin
  17.   last;
  18. }
  19. while (1) { # and all the rest of the gremlins as they come in one piece
  20.   my $c = $port->lookfor(); # get the next one
  21.   last if $c eq ""; # or we're done
  22.   # print $c; # uncomment if you want to see the gremlin
  23. }

Transmitting from Perl

  1. $port->write("Whatever you feel like sending");

Note that Perl may treat such a string as UTF-8 (e.g. whenever you include umlaut chars or any above ordinal 127). In those cases your Arduino will receive double the amount of chars you might have expected! Consider to use the bytes pragma in your program code.

Further you should limit the length of the output buffer to chunks of 4096 as there seems to be a limitation within the SerialPort module.

Receiving from Arduino

  1. while (1) {
  2.     # Poll to see if any data is coming in
  3.     my $char = $port->lookfor();
  4.  
  5.     # If we get data, then print it
  6.     # Send a number to the arduino
  7.     if ($char) {
  8.         print "Received character: $char \n";
  9.     }
  10.     # Uncomment the following lines, for slower reading,
  11.     # but lower CPU usage, and to avoid
  12.     # buffer overflow due to sleep function.
  13.  
  14.     # $port->lookclear;
  15.     # sleep (1);
  16. }

Caveats

Found using a Mac (Lion)

Some pitfalls might await you if you start serious communication. Serious means kilobytes of data to be transferred. The FTDI driver does not seem to be too robust. It will eventually put processes in uninterruptible mode so they can not be killed. Shutdown needs to be forced by pressing the power off button for a couple of seconds! This happened just when utilising the Nano. The driver for the Uno is by far more stable. It was a hard day finding this out. So if you're experimenting with Serial take an Uno, not a Nano.

Ending the process

As long as you Perl program is running your Arduino will also keep on running. But as soon as you stop the Perl process the port will be reset which will eventually cause hiccups on the Arduino. So the best idea is to keep the process alive by inserting a sleep or <> statement right before the exit.

Share