chipKIT® Development Platform

Inspired by Arduino™

Perl and Arduino Serial Interface

Created Sun, 03 Jun 2012 18:28:23 +0000 by jmlynesjr


jmlynesjr

Sun, 03 Jun 2012 18:28:23 +0000

It appears that Perl can also be used to interface serially to a chipKIT.

See the Comprehensive Perl Archive Network - CPAN - http://www.cpan.org and search for arduino.

Take a look at the Perl Class-> Device::SerialPort::Arduino by Simone Marzulli.

Additional information is available at http://arduino.cc/playground/interfacing/PERL.

I hope to build a Perl version of the communications->Graph example in the future.

James


jmlynesjr

Wed, 06 Jun 2012 02:01:05 +0000

The Device::SerialPort::Arduino module works as advertised.

To try this out, assuming you have Perl or Strawberry Perl installed: Install the CPAN module installer(this takes a few minutes)

cpan App::cpanminus

Execute the installer

cpanm Device::SerialPort::Arduino
     this will also install Device::SerialPort

The following is my test code:

# Perl serial interface to Arduino Example

# Reads characters being sent from the chipKIT UNO and prints them to the screen.
# The output looks like the mpide serial monitor.
# Use mpide to compile and run the communications->graph example to generate the character stream.

# From the CPAN Perl module Device::SerialPort::Arduino
# Written by Simone Marzulli
# Modified for USB interface and annotated by James Lynes, Jr. June 5,2012
# Tested under Ubuntu 10.10 and Perl v5.10.1 on the chipKIT UNO board (communicate as yet untested - requires
# a modified chipKIT sketch that expects an incoming message. "graph" only transmits characters.

# Documentation under: Perldoc Device::SerialPort
#                      Perldoc Device::SerialPort::Arduino


# Uncomment the section below which you would like to test: receive(), receive(with delay), communicate

use strict;
use warnings;

# Initialize the serial port - creates the serial port object $Arduino

use Device::SerialPort::Arduino;

  my $Arduino = Device::SerialPort::Arduino->new(
    port     => '/dev/ttyUSB0',
    baudrate => 9600,
    databits => 8,
    parity   => 'none',
  );


# Reading from Arduino via Serial - uses Device::SerialPort "lookfor" method

   while (1) {
       print $Arduino->receive(), "\n";
   }


# Reading from Arduino via Serial with a delay - uses Device::SerialPort "lookclear" method and a sleep call
# Argument is number of seconds to sleep between receives

#  while (1) {
#      print $Arduino->receive(5), "\n";
#  }


# Send something via Serial - uses Device::SerialPort "write" method

#  $Arduino->communicate('oh hi!!11')
#     or die 'Warning, empty string: ', "$!\n";

The next step is to write a test sketch to exercise the communicate method and possibly package it all together in a wxPerl GUI application.

James


jmlynesjr

Tue, 12 Jun 2012 01:22:31 +0000

See below for the test code for the "communicate"(write) method. Communicate also works as advertised.

The Perl code sends a poll(P). The chipKIT code waits for a poll and returns a message sequence number.

chipKIT Code

// Serial_Poll_Test.pde

// Title: chipKIT Serial Poll Test
// Author: James M. Lynes, Jr.
// Version: 1.0
// Creation Date: June 10, 2012
// Modification Date: June 10, 2012

void setup() 
{
  
  // Initialize the serial port
  Serial.begin(9600);
  Serial.println("Test of Serial Port Polling");
  
}

  char myAddr = 'P';
  int sequence = 0;
  char poll_code = ' ';

void loop() 
{    
  waitpoll();
  
  poll_code = readpoll();
  
  if(poll_code == myAddr) {
    Serial.println(sequence, DEC);
    sequence++;
    delay(1);
  }
}

void waitpoll()
{
  // Wait for a poll from the serial port
  // Returns when a character is available
     while(Serial.available() <= 0) {
     delay(1);
  }
} 
 
char readpoll()
{ 
  char poll_char = ' ';
  poll_char = Serial.read();
  return poll_char;
}

Perl Code

# Perl Polling Test

# Title: Perl to chipKIT Serial Poll Test
# Author: James M. Lynes, Jr.
# Version: 1.0
# Creation Date: June 10, 2012
# Modification Date: June 10, 2012

use strict;
use warnings;

# Initialize the serial port - creates the serial port object $Arduino

use Device::SerialPort::Arduino;

  my $Arduino = Device::SerialPort::Arduino->new(
    port     => '/dev/ttyUSB0',
    baudrate => 9600,
    databits => 8,
    parity   => 'none',
  );

while(1) {
  $Arduino->communicate('P');                     # Send a poll
  print ($Arduino->receive(), "\n");              # Print poll response
  sleep(1);                                       # Delay until next poll
}

The next step is to expand this test code to exchange packets. Probably will use a simplified clone of IBM BiSync protocol: syn, syn, syn, stx, address-byte, byte-count, etx, checksum or crc-16.

James