PowerAnt: Teach Your computer To drive The real World
PowerAnt is control from personal computer, using RS232 port, from 14 and more external electrical devices. Open, Human-Readable protocol, it is fully described in programmer's manual. Control of electrical equipment with power supply near 200W. You Wish control the real world from MS Excel?
 PowerAnt
Owerview
Applications
DataSheet
Documentation
Photos
 PowerAnt types
SwSe
SwSw
SeSe
 Programming
Perl samples
C/C++ - samples
MS Access 2000
MS Excel 2000
 Order
Order details
Price
 News RSS
 Contacts
Яндекс цитирования Каталог ресурсов ListTop.Ru Rambler's Top100

PowerAnt Perl programming

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).

Open RS-232 port in Perl

#!/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;
#...........

Close RS-232 port in Perl

#...........
$PowerAnts->close() || die "Can't close device, something wrong!\n";;
undef $PowerAnts;

Work with RS-232 port in Perl

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("!??");

Error detection

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 (/-/);

Check the sensors state in Perl

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

Switch control in Perl

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;

Conclusion

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.

PowerAnt is control from personal computer, using RS232 port, from 14 and more external electrical devices. Open, Human-Readable protocol, it is fully described in programmer's manual. Control of electrical equipment with power supply near 200W. You Wish control the real world from MS Excel?
  © 2004-2014 by  www.anthillsolutions.com