M
msalerno
I have a device that uses the dsr for communications. Basically what I
am trying to do is get a count of how many pulses it sends per second.
I have a script that counts the pulses, but there is something wrong.
It is counting too many pulses. I think that what is happening is that
it is counting one pulse many times. I know my count is wrong because I
have some windows software that works, and when I compare the output of
the windows software to the output of my script, the script is way off.
If anyone out there could point me in the correct direction I would be
very happy. It's tough to find informaion about monitoring the DSR
lines. Does anyone here have any experience with a similar situation?
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(usleep);
use Device::SerialPort qw( ARAM :STAT 0.07 );
my %logged;
my @then;
# Open Serial Port
my $PortObj = new Device::SerialPort ('/dev/ttyS0') || die "Cannot open
Port: $!\n";
while () {
my $ModemStatus = $PortObj->modemlines;
my $hour = ((localtime)[2]); $hour = "0$hour" if length($hour)
< 2;
my $min = ((localtime)[1]); $min = "0$min" if length($min) < 2;
my $sec = ((localtime)[0]); $sec = "0$sec" if length($sec) < 2;
my $now = "$hour-$min-$sec";
push @then, $now;
push @then, $now if $#then < 1;
shift @then if $#then > 1;
if (!exists($logged{$now})){
$logged{$now} = 0;
my $val1 = sprintf "%.2f", $logged{$then[-2]}/3.4;
my $val2 = sprintf "%.2f", $logged{$then[-2]}/2.5;
print "Then:$then[-2] - $logged{$then[-2]} - $val1 -
$val2\n";
}
my $pulse = 0!=($ModemStatus & MS_DSR_ON);
$pulse = 0 if !$pulse;
$logged{$now} = $pulse + $logged{$now};
if ($pulse == 1){
usleep(1); # sleep one milisecond if change detected.
}
}
$PortObj->close;
am trying to do is get a count of how many pulses it sends per second.
I have a script that counts the pulses, but there is something wrong.
It is counting too many pulses. I think that what is happening is that
it is counting one pulse many times. I know my count is wrong because I
have some windows software that works, and when I compare the output of
the windows software to the output of my script, the script is way off.
If anyone out there could point me in the correct direction I would be
very happy. It's tough to find informaion about monitoring the DSR
lines. Does anyone here have any experience with a similar situation?
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw(usleep);
use Device::SerialPort qw( ARAM :STAT 0.07 );
my %logged;
my @then;
# Open Serial Port
my $PortObj = new Device::SerialPort ('/dev/ttyS0') || die "Cannot open
Port: $!\n";
while () {
my $ModemStatus = $PortObj->modemlines;
my $hour = ((localtime)[2]); $hour = "0$hour" if length($hour)
< 2;
my $min = ((localtime)[1]); $min = "0$min" if length($min) < 2;
my $sec = ((localtime)[0]); $sec = "0$sec" if length($sec) < 2;
my $now = "$hour-$min-$sec";
push @then, $now;
push @then, $now if $#then < 1;
shift @then if $#then > 1;
if (!exists($logged{$now})){
$logged{$now} = 0;
my $val1 = sprintf "%.2f", $logged{$then[-2]}/3.4;
my $val2 = sprintf "%.2f", $logged{$then[-2]}/2.5;
print "Then:$then[-2] - $logged{$then[-2]} - $val1 -
$val2\n";
}
my $pulse = 0!=($ModemStatus & MS_DSR_ON);
$pulse = 0 if !$pulse;
$logged{$now} = $pulse + $logged{$now};
if ($pulse == 1){
usleep(1); # sleep one milisecond if change detected.
}
}
$PortObj->close;