File Coverage

blib/lib/Device/Modbus/ASCII/Client.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 44 44 100.0


line stmt bran cond sub pod time code
1             package Device::Modbus::ASCII::Client;
2              
3 1     1   63166 use parent 'Device::Modbus::Client';
  1         245  
  1         4  
4 1     1   7875 use Device::Modbus::ASCII::ADU;
  1         2  
  1         22  
5 1     1   545 use Role::Tiny::With;
  1         8017  
  1         42  
6              
7 1     1   6 use Carp;
  1         2  
  1         38  
8 1     1   5 use strict;
  1         2  
  1         14  
9 1     1   4 use warnings;
  1         1  
  1         162  
10              
11             with 'Device::Modbus::Serial';
12             with 'Device::Modbus::ASCII';
13              
14             sub new {
15 2     2 1 1479 my ($class, %args) = @_;
16              
17 2         9 my $self = bless \%args, $class;
18 2         13 $self->open_port;
19 2         8 return $self;
20             }
21              
22             # Parse the Application Data Unit
23             # The Device::Modbus response parsing parts require a binary string,
24             # and thus the message has to be transformed so that it can be
25             # processed. This is easier and far less risky than modifying the
26             # parse routine that already works correctly for Modbus TCP and RTU.
27             sub receive_response {
28 2     2 1 40 my $self = shift;
29 2         12 $self->read_port;
30 2         8 my $adu = $self->new_adu();
31 2         9 $self->parse_header($adu);
32            
33             # Convert the rest of the message to binary form
34 2         5 my $buffer;
35 2         8 while (length($self->{buffer}) > 4) {
36 9         41 $buffer .= pack 'H*', substr $self->{buffer}, 0, 2, '';
37             }
38 2         7 substr $self->{buffer}, 0, -4, $buffer;
39 2         14 $self->parse_pdu($adu);
40              
41             # But turn the LRC and the \r\n back to ascii
42 2         267 $buffer = unpack 'H*', $self->{buffer};
43 2         10 $self->parse_footer($adu);
44              
45 2         6 return $adu;
46             }
47              
48              
49             1;
50              
51             __END__