File Coverage

blib/lib/Device/Modbus/Exception.pm
Criterion Covered Total %
statement 33 33 100.0
branch 10 10 100.0
condition 16 17 94.1
subroutine 9 9 100.0
pod 0 3 0.0
total 68 72 94.4


line stmt bran cond sub pod time code
1             package Device::Modbus::Exception;
2              
3 13     13   23209 use parent 'Device::Modbus';
  13         355  
  13         71  
4 13     13   651 use Carp;
  13         28  
  13         828  
5 13     13   18674 use overload '""' => \&stringify;
  13         13599  
  13         157  
6              
7 13     13   715 use strict;
  13         26  
  13         287  
8 13     13   65 use warnings;
  13         20  
  13         292  
9 13     13   135 use v5.10;
  13         85  
10              
11             my %exc_descr_for = (
12             1 => '1 - Function code not supported',
13             2 => '2 - Incorrect address or address not supported',
14             3 => '3 - Invalid data (either quantity or values)',
15             4 => '4 - Execution failed',
16             );
17              
18             sub new {
19 67     67 0 4206 my ($class, %args) = @_;
20              
21             # Must receive either a function name or a function code
22 67 100 66     291 unless (exists $args{function} or exists $args{code}) {
23 1         215 croak 'Please specify either a function name or code to instantiate an exception';
24             }
25              
26 66 100 100     498 if (exists $args{function} && exists $Device::Modbus::code_for{$args{function}}) {
    100 100        
    100          
27 12         28 $args{code} = $Device::Modbus::code_for{$args{function}} + 0x80;
28             }
29             elsif (exists $args{code} && exists $Device::Modbus::function_for{$args{code} - 0x80}) {
30 51         140 $args{function} = $Device::Modbus::function_for{$args{code} - 0x80};
31             }
32             elsif (exists $args{code}) {
33 2         5 $args{function} = 'Non-supported function';
34             }
35             else {
36 1         137 croak "Function $args{function} is not supported";
37             }
38              
39             # Must receive an exception code
40             croak 'A valid exception code (between 1 and 4) is required'
41 65 100 100     843 unless exists $args{exception_code} && $args{exception_code} > 0 && $args{exception_code} <= 4;
      100        
42              
43 62         441 return bless \%args, $class;
44             }
45              
46             sub pdu {
47 1     1 0 5 my $self = shift;
48 1         10 return pack('CC', $self->{code}, $self->{exception_code});
49             }
50              
51             sub stringify {
52 52     52 0 66 my $self = shift;
53              
54 52   100     164 my $str = $Device::Modbus::function_for{$self->{code}-0x80} // 'Unknown';
55 52         104 my $descr = $exc_descr_for{$self->{exception_code}};
56              
57 52         214 return "Exception for function <$str>: $descr";
58             }
59              
60             1;