=NULL ### Error

Error checking highlights a core state of being for computational machines. Things break. Code has to be written with an expectation that that data will stop flowing, or variables will become corrupted. This is particularly the case when dealing with input generated by users, where there is sure knowledge that people cannot be trusted to input data in a structured or 'correct' manner. The language that surrounds this places fault on the user, not on the limitations of a computational machine, perpetuating a drive for ever more controlled interface, saving the user from themselves.

While developing project ideas I investigated communicating with an Arduino board using perl. Evolving some scripts I found online, I incorporated some simple error checking which I thought people might find useful. The process also raised questions relating to error checking on computational machines. Code cannot be written that error checks for all eventualities, things burn, get dropped, stop working, yet a belief system is upheld and perpetuated that all errors can be ironed out, machines can be trusted, they are reliable and dependable, they don't make errors, humans do.

#!/usr/bin/perl
# SERIAL ERROR CHECK
# Tom Keene - www.theanthillsocial.co.uk - 2011
# This script uses the SerialPort module for perl.
# - Install using this comandline: cpan Device::SerialPort
# List serial port device names on the terminal with: dmesg | grep tty
# On linux, device names seem to be called: /dev/ttyUSB0 OR /dev/ttyUSB1 ect 
# Corresponding arduino code is provided at the bottom of this script

use warnings;
use strict;
 
# Connect to a serial port, specifying the device to connect to
my $port = &connectme("/dev/ttyUSB0");
 
# continually write a message to the serial port and wait for a response
my $msg = 1;
while(1){
  &callresponse($port, $msg, 300);
  $msg++;
  sleep(1);
}
 
# SERIAL CONNECTION
sub connectme(){
  # Sample Perl script to transmit number
  # to Arduino then listen for the Arduino
  # to echo it back
  use Device::SerialPort;
  # Set up the serial port
  # 19200, 81N on the USB ftdi driver
  my $PortName = $_[0];
  my $port = Device::SerialPort->new($PortName) || die "ERROR WITH SERIAL PORT NAME: \"$PortName\" \n$!\n\n";
  $port->databits(8);
  $port->baudrate(19200);
  $port->parity("none");
  $port->stopbits(1);
  return $port;
}
 
# WRITE TO THE SERIAL PORT AND WAIT FOR A RESPONSE
sub callresponse(){
  my $port = $_[0];
  my $msg = $_[1];
  my $trys = $_[2];
  my $char;
  sleep(1);
  $port->write("$msg\n");
  print "Sent: $msg ";
  while($trys>=0){
      $char = $port->lookfor();
      if($char){
         print "Recieved: " . $char . " \n";
         $trys=-1;
      }
      $trys=$trys-1;
  }
  if($char ne $msg){
    print "Failed: recieved weirdness \"$char\" \n";
  }
  return $char;
}
 
=for#================ARDUINO-CODE====================================
char incomingByte;              // for incoming serial data
char str1[50];
int count = 0;
 
void setup ()
{
  Serial.begin(19200);
  digitalWrite (13, HIGH);      //turn on debugging LED
}
 
 
//  MAIN CODE
void loop ()
{
  // send data only when you receive data:
  if (Serial.available () > 0)
    {
      // read the incoming byte:
      incomingByte = Serial.read ();
 
      // Store it in a character array
      str1[count] = incomingByte;
      count++;
 
      // check if we have over 49 characaters or we recieve a return or line feed
      if (count > 49 || incomingByte == 10 || incomingByte == 13)
        {
          // Send the string back
          Serial.print (str1);
          count = 0;
        }
    }
}
=cut#===============================================================