Home: Perl Programming Help: Win32 Programming Help:
Win32 SerialPort script

New User

Oct 4, 2008, 5:29 AM

Views: 6025
Win32 SerialPort script


I am trying to understand the following code written in unix. I am porting it to Win32::SerialPort.

# Chatboard test utility
# Answer 'OK' to everything and spit out the communication

$| = 1;

system('stty 9600 -F /dev/tts/5');

open(TTS, "+< /dev/tts/5") or die;
print TTS "OK\n";

while(<TTS>) {
print TTS "OK\n";

# cut them into pieces...
if (/^(AT\+CKPD=")([0-9a-f]+)(".*)$/) {
my $pre = $1;
my $pieces = $2;
my $post = $3;
$pieces =~ s/(..)/$1 /g;

$_ = "$pre$pieces$post\n";


Installed Win32::SerialPort. Works ok.Now i dont understand the if( ) part in while loop in code above. Can someone please explain it a bit or help with little code snippet. I have no previous background in perl. Done some Python and C thougth.

My win32 code is here. Runs nice so far. But how to code this if( ) ?

#! perl 
use strict;
use Win32::SerialPort;
$| = 1;
my $ob = Win32::SerialPort->new('COM1') or die "fail opening COM1";
print "COM1 opened successfully \n";

$ob->baudrate(9600) || die "fail setting baud";
$ob->parity('none') || die "fail setting parity";
$ob->databits(8) || die "fail setting databits";
$ob->stopbits(1) || die "fail setting stopbits";
$ob->handshake('none') || die "fail setting handshake";
$ob->write_settings || die "no settings";

my $result = $ob->input;

$result = $ob->input;

# Help here:

undef $ob;
print "COM1 closed..\n";