File Coverage

blib/lib/Device/Modbus/TCP/ADU.pm
Criterion Covered Total %
statement 33 33 100.0
branch 11 12 91.6
condition 3 3 100.0
subroutine 9 9 100.0
pod 0 5 0.0
total 56 62 90.3


line stmt bran cond sub pod time code
1             package Device::Modbus::TCP::ADU;
2              
3 8     8   17174 use parent 'Device::Modbus::ADU';
  8         9  
  8         39  
4 8     8   5351 use Carp;
  8         8  
  8         310  
5 8     8   24 use strict;
  8         11  
  8         97  
6 8     8   24 use warnings;
  8         36  
  8         1671  
7              
8             sub id {
9 16     16 0 1120 my ($self, $value) = @_;
10 16 100       35 if (defined $value) {
11 5         22 $self->{id} = $value;
12             }
13             croak "ID has not been declared"
14 16 100       152 unless exists $self->{id};
15 15         33 return $self->{id};
16             }
17              
18             sub length {
19 5     5 0 686 my ($self, $value) = @_;
20 5 100       14 if (defined $value) {
21 3         12 $self->{length} = $value;
22             }
23             croak "PDU length has not been declared"
24 5 100       81 unless exists $self->{length};
25 4         8 return $self->{length};
26             }
27              
28             # Modbus TCP states unit number is 0xFF by default
29             sub unit {
30 22     22 0 718 my $self = shift;
31 22 100 100     58 $_[0] = 0xFF unless exists $self->{unit} || defined $_[0];
32 22         68 return $self->SUPER::unit(@_);
33             }
34              
35             sub binary_message {
36 6     6 0 8 my $self = shift;
37 6 50       10 croak "Please include a unit number in the ADU."
38             unless $self->unit;
39 6         42 my $header = $self->build_header;
40 6         12 my $pdu = $self->message->pdu();
41 6         97 return $header . $pdu;
42             }
43              
44             #### APU building
45              
46             sub build_header {
47 7     7 0 222 my $self = shift;
48 7         11 my $header = pack 'nnnC',
49             $self->id, # Transaction id
50             0x0000, # Protocol number (Modbus)
51             CORE::length($self->message->pdu) + 1, # Length of PDU + 1 byte for unit
52             $self->unit; # Unit number
53 7         43 return $header;
54             }
55              
56             1;