Can't use string ("00 00 00 00 00 00 00 00") as a HASH ref while "stri
+ct refs" in use at q:\tmp.pl line 24, <DATA> line 1.
If you change the code to always use hash references, it works for me:
#$last_value{ $id } = $payload;
$last_value{ $id } ||= {};
if(defined $last_value{ $id } )
{
$last_value{ $id }{COUNT} += 1;
# If we last saw something from this device, note the
+time difference
if( $last_value{ $id }{TIME} ) {
$time_diff = $now_milli - $last_value{ $id }{TIME};
} else {
# Otherwise, print 0
$time_diff= 0;
};
When printing out, you print $last_value{ $id }, which will not be a useful value. Change it to print $last_value{ $id }{PAYLOAD}.
Maybe now, %last_value should be renamed to %last_response in your code...
The following code works for me:
#!/usr/bin/perl -w
use strict;
use Time::HiRes qw/gettimeofday/;
my $PCAN="/dev/pcan32";
my $now_milli = 1000 * gettimeofday();
my %last_value;
my $time_diff;
#open my $can_bus, "receivetest -f=$PCAN | sed -e \'s/receivetest: m s
+ 0x//g\' |"
# or die "Couldn't read from CAN bus: $!";
my $can_bus= *DATA;
while (<$can_bus>) {
if( /^(........\.\d{1,3}) (........) (.*)$/ )
{
# system('clear'); #clear screen
my ($something,$id, $payload) = ($1,$2,$3);
$last_value{ $id } ||= {};
if(defined $last_value{ $id } )
{
$last_value{ $id }{COUNT} += 1;
if( $last_value{ $id }{TIME} ) {
$time_diff = $now_milli - $last_value{ $id }{TIME};
} else {
$time_diff= 0;
};
$last_value{ $id }{TIME} = $now_milli;
$last_value{ $id }{DIFF} = $time_diff;
$last_value{ $id }{PAYLOAD} = $payload;
}
else
{
#$last_value{ $id }{COUNT} = 1;
$last_value{ $id }{TIME} = $now_milli;
$last_value{ $id }{DIFF} = "NA";
$last_value{ $id }{PAYLOAD} = $payload;
}
#Print Table
for my $id (sort keys %last_value) {
print "$id\t $last_value{ $id }{PAYLOAD}\t $last_value{ $i
+d }{COUNT}\t $last_value{ $id }{DIFF}\n";
}
}
else {
warn "ignore unknown line: ";
warn $_;
}
}
#/^(........\.\d{1,3}) (........) (.*)
__DATA__
12345678.123 0CFFE000 00 00 00 00 00 00 00 00
87654321.321 18FEF501 00 FF 32 C4 FF 00 00 00
43215678.132 0C11FF00 FF FF FF FF FF FF FF FF
12348765.312 18FFE100 23 E0 C1 0E FF 00 AC 00
43218765.112 00000000 01 FF FF FF FF FF FF FF
12345678.123 0CFFE000 01 00 00 00 00 00 00 00
87654321.321 18FEF501 00 00 00 00 00 00 00 00
43215678.132 0C11FF00 00 00 00 00 00 00 00 00
12348765.312 18FFE100 00 00 00 00 00 00 00 00
43218765.112 00000000 00 00 00 00 00 00 00 00
12345678.123 0CFFE000 00 00 00 00 00 00 00 00
|