Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with Win32::SerialPort pm

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
hello all

I just installed the Win32::SerialPort perl module using ppm and it installed fine BUT when i run this script i get erorrs... and i don't know why I get eorrors in line 11 which is this line:


$card = Device::SerialPort->new ($port) || die "Can't open $port: $!";

please help here is the whole script code

thanks

#!/usr/bin/perl

require "SerialPort.pm";

my $port = "/dev/ttyS0";
my @line; # variable to hold our received line
my $line;

&CreateInvertArray; # initialize invert array

$card = Device::SerialPort->new ($port) || die "Can't open $port: $!";
$card->baudrate(9600);
$card->parity("odd") || die "fail setting parity";
$card->databits(8) || die "fail setting databits";
$card->stopbits(1) || die "fail setting stopbits";
#$card->handshake("rts") || die "fail setting handshake";
$card->handshake("none") || die "fail setting handshake";

@line=resetcard(); # resets and returns ATR
$line=invbytes2string(@line); # do invert/reverse to display format
print "ATR=$line\n" if $line; # display our received atr bytes
# change our baud rate to 38400 for normal CAM communications
$card->baudrate(38400) || die "fail setting baudrate";
$verbosity=4;


sub processcmd90 {
my ($header,$body)=@_;
my @ins=string2invbytes($header);
my @body=string2invbytes($body);
sendbytes($card,@ins); #send cmd90 header
@line=getbytes($card); # receive our CAM reply
sendbytes($card,@body); # send cmd90 body
@line=getbytes($card); # get decoded cmd90
$line=invbytes2string(@line); # convert for display/logging
$line=~s/ *90 .. *$//;
return $line;
}



# resets HU card and returns ATR
sub resetcard {
$card->pulse_rts_on(1); # reset card by pulsing atr 0.5 milliseconds
delay(100); # wait for setup
$r=1;
@line=getbytes($card); # receive ATR bytes from card
$r=0;
return @line;
}

# send bytes to serial port and add an interbyte delay of 40 milliseconds
sub sendbytes {
my ($ob,@bytes)=@_;
my @temp=@bytes;
my $in;
my $ret;
delay(5);
while (@bytes) {
$_=shift @bytes;
$ob->write($_);
# print invbytes2string(($_));
delay(0.005);
do {
($ret,$in)=$ob->read(1);
delay(0.005);
}
until ($in eq $_);
}
}

# receive serial bytes and convert to normal by inverting/reversing
sub getbytes {
my ($ob)=@_;
my @line;
my $ret;
my $in;
delay(15);
do {
($ret,$in)=$ob->read(1) ;
push @line,$in if $ret;
delay(0.005);
#print invbytes2string(($in));

} until (!$ret);
return @line;
}

# create our invert/reverse lookup array
sub CreateInvertArray {
my $x=0;
@invert_array=();

while ($x<256) {
$byte=$x;
$c=0;
$y=0;
while ($y<8) {
$c |= (($byte & (1 << $y)) != 0) << (7 - $y);
$y++;
}
push @invert_array,~$c & 255; # push reversed/inverted byte into @invert_array.
$x++;
}

}

# our delay routine
sub delay {
my ($msec)=@_;
$msec=$msec/1000;
select (undef,undef,undef,$msec);
}

# convert inverted/reversed char byte array to normal hex string for logging/display
sub invbytes2string {
my @line=@_;
my $string;
while (@line) {
$_=shift @line;
$_=sprintf &quot;%.2X&quot;,$invert_array[ord($_)];
#$_=sprintf &quot;%.2X&quot;,ord($_);
$string.=$_ . &quot; &quot;;
}
return $string;
}

# convert a hex text string to an inverted/reversed byte char array
sub string2invbytes {
my ($string)=@_;
my @temp;
$string=~s/^ +//;
$string=~s/ +$//;
$string=~s/\n/ /gs;
my @temp2=split / +/,$string;
while (@temp2) {
$_=shift @temp2;
push @temp,chr($invert_array[hex($_)]);
#push @temp,chr(hex($_));
}
return @temp;
}

1;

 
What is the error? I have not used Win32::Serialport before but do you think it might be something to do with your port name? i.e. /dev/ttyS0, shouldn't it be COM1?
 
yea i tryied chnaging the /dev/ttyS0, to com 1 no go the eorror i get is at line 11. somethimg about invaild line command
 
I have even tried chnaging this line:

$card = device::SerialPort->new ($port) || die &quot;Can't open $port: $!&quot;;


to this

$card = win32::SerialPort->new ($port) || die &quot;Can't open $port: $!&quot;;

no go :(





 
Try changing the following lines:
require &quot;SerialPort.pm&quot;;
$card = Device::SerialPort->new ($port) || die &quot;Can't open $port: $!&quot;;

to:
require Win32::SerialPort;
$card = Win32::SerialPort->new ($port) || die &quot;Can't open $port: $!&quot;;
 
well i got it so there are no errors BUT it just hangs there :( no eorrs no msg, just hangs there :(


 
It is probably hanging on a read of the serial port. Unless you set a timeout, it will wait forever. I've used Win32::SerialPort many times. Here is some sample code I have for opening, setting up, writing to, and reading from a serial port.
Code:
BEGIN {
     eval &quot;require Win32::SerialPort&quot;;
     die &quot;$@\n&quot; if ($@);
     eval &quot;use Win32::API&quot;;
     die &quot;$@\n&quot; if ($@);
} 
use Win32;

$Port = &quot;COM1&quot;;

# Open serial port connection
$ob = Win32::SerialPort->new ($Port);
die(&quot;Can't open serial port $Port: $^E&quot;) unless ($ob);
my $SetupStat = SetupPort;
die(&quot;Could not set up serial port&quot;) if ($SetupStat != 0);

# Login
$ob->write($Username\r);
ReadIt('Password:', '5000', '1', '1'); 
$ob->write($Password\r);
ReadIt($SysPrompt, '5000', '1', '1');

# Setup the serial port
sub SetupPort() {
    my $SetBaud=57600;
    my $SetParity='none';
    my $SetData=8;
    my $SetStop=1;
    my $SetHandShake='none';

    # Read serial port parameters
    my $Baud = $ob->baudrate;
    my $Parity = $ob->parity;
    my $Data = $ob->databits;
    my $Stop = $ob->stopbits;
    my $HandShake = $ob->handshake;

    # Write serial port parameters
    $ob->baudrate($SetBaud) || return(1);
    $ob->parity($SetParity) || return(1);
    $ob->databits($SetData) || return(1);
    $ob->stopbits($SetStop) || return(1);
    $ob->handshake($SetHandShake) || return(1);
    $ob->write_settings || return(1);
    return(0);
} # End of SetupPort 

sub ReadIt($$$$) {
    (my $String, my $TimeOut, my $Display, my $Fatal) = @_;
    my $Reply = &quot;&quot;;
    my ($Buffer);
    print &quot;Waiting for '$String' \n&quot;;
    $ob->read_const_time($TimeOut);        # Setup timeout value in mS
    my $Count;
    do {
        ($Count, $Buffer) = $ob->read(1); # Read 1 byte at a time
        $Reply .= $Buffer;                # Build message
        print &quot;$Buffer&quot; if ($Display);    # Display messages if enabled
    } while($Count > 0 and $Reply !~ m/$String/);
    print &quot;\n&quot; if ($Display);
    unless ($Reply =~ m/$String/) {
        if ($Fatal) {
            die(&quot;Timed out waiting for '$String'&quot;);
        } else {
            $Reply = 'TO';
        }
    }
    return($Reply);
} # End of ReadIt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top