Perl is my favorite programming language. Text based protocol for PowerAnt designed first of all for programming that device on Perl. Each command and each device answer contains only unical characters and that make it easy to parse.
If for Linux users the question "Where I can get Perl?" is not actual, then Windows users can use ActivePerl.
In standard way the Perl is do not know anithing about RS-232 port. To add support RS-232 port to Perl you must install special module Win32::SerialPort (For ActivePerl required: Win32-API.ppd, Win32-SerialPort.ppd).
#!/usr/bin/perl -w
use Win32::SerialPort;
$PowerAnts = new Win32::SerialPort('COM2','','') || die "Can't open COM2";
$PowerAnts->baudrate(9600) || die "Can't set 9600";
$PowerAnts->parity('none') || die "Can't set parity none";
$PowerAnts->databits(8) || die "Can't set 8 data bits";
$PowerAnts->stopbits(1) || die "Can't set one stopbit";
$PowerAnts->handshake('none') || die "Can't HW flow control off";
$PowerAnts->write_settings;
#...........
#........... $PowerAnts->close() || die "Can't close device, something wrong!\n";; undef $PowerAnts;
In this peace of source code is presented the procedure which use the "write" function to put bytes into devices and "read" function to get bytes from devices. The standard procedure "input" is not be usefull for this (this procedure waits byte with code 0x0A).
sub pwrant_cmd($){
my $cmd = shift;
$PowerAnts->write($cmd."\r");
$_ = '';
do {
$_ .= $PowerAnts->read(1);
} while (! /\r/);
return $_;
};
$_ = pwrant_cmd("!??");
If the device answer conatin '-' character, that device ansver is negative. It's mean - something wrong happens. For error detection in Perl you can use one line of text:
die "Something wrong\n" if (/-/);
For example, for PowerAnt with type SwSe sensor 'K' state changing (from 'k' to 'K') may be detected by following program:
$_ = pwrant_cmd('?=');
while( ! /K/ ){
die "Something wrong\n" if (/-/);
$_ = pwrant_cmd('?%');
};
# Sensor 'K' is On
For example, make the "running light" (program can be used for PowerAnt with types: SwSe and SwSw):
#!/usr/bin/perl -w
use Win32::SerialPort;
$PowerAnts = new Win32::SerialPort('COM2','','') || die "Can't open COM2";
$PowerAnts->baudrate(9600) || die "Can't set 9600";
$PowerAnts->parity('none') || die "Can't set parity none";
$PowerAnts->databits(8) || die "Can't set 8 data bits";
$PowerAnts->stopbits(1) || die "Can't set one stopbit";
$PowerAnts->handshake('none') || die "Can't HW flow control off";
$PowerAnts->write_settings;
sub pwrant_cmd($){
my $cmd = shift;
$PowerAnts->write($cmd."\r");
$_ = '';
do {
$_ .= $PowerAnts->read(1);
} while (! /\r/);
return $_;
};
$x_on = 'H';
$x_off = 'h';
pwrant_cmd("=abcdefgh"); # Turn off all switches
while( 1 ){
$_ = pwrant_cmd("=".$x_off); # Turn off switch with name in var. $x_off
# Check the answer
die "Something wrong\n" if (/-/);
$x_on++;
$x_off++;
if( $x_on gt 'H' ){
$x_on = 'A';
$x_off = 'a';
};
$_ = pwrant_cmd("=".$x_on); # Turn on switch with name in var. $x_on
# Check the answer
die "Something wrong\n" if (/-/);
sleep( 1 ); # Sleep 1 second
};
$PowerAnts->close() || die "Something wrong\n";;
undef $PowerAnts;
If the Perl is your favorite programming language, and you have programmers manual PowerAnt, then other talks about "how to write programms for PowerAnt on Perl", I think, no scense.