File Coverage

blib/lib/Device/Modbus.pm
Criterion Covered Total %
statement 25 25 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 33 35 94.2


line stmt bran cond sub pod time code
1             package Device::Modbus;
2              
3 14     14   23985 use Carp;
  14         27  
  14         1051  
4 14     14   66 use strict;
  14         27  
  14         280  
5 14     14   65 use warnings;
  14         24  
  14         336  
6 14     14   243 use v5.10;
  14         82  
7              
8             our $VERSION = '0.020';
9              
10             our %code_for = (
11             'Read Coils' => 0x01,
12             'Read Discrete Inputs' => 0x02,
13             'Read Holding Registers' => 0x03,
14             'Read Input Registers' => 0x04,
15             'Write Single Coil' => 0x05,
16             'Write Single Register' => 0x06,
17             'Write Multiple Coils' => 0x0F,
18             'Write Multiple Registers' => 0x10,
19             'Read/Write Multiple Registers' => 0x17,
20             );
21              
22             our %function_for = reverse %code_for;
23              
24             #### Helper methods
25              
26             # Receives an array reference of bit values and builds an array
27             # of 8-bit numbers. Each number starts with the lower address
28             # in the LSB.
29             # Returns the quantity of bits packed and a reference to the array
30             # of 8-bit numbers
31             sub flatten_bit_values {
32 6     6 0 868 my ($self, $values) = @_;
33              
34             # Values must be either 1 or 0
35 6 100       10 my @values = map { $_ ? 1 : 0 } @{$values};
  75         155  
  6         16  
36              
37             # Turn the values array into an array of binary numbers
38 6         12 my @values_binary;
39 6         20 while (@values) {
40 13         102 push @values_binary, pack 'b*', join '', splice @values, 0, 8;
41             }
42 6         21 return \@values_binary;
43             }
44              
45             # Receives a quantity of bits and an array of 8-bit numbers.
46             # The numbers are exploded into an array of bit values.
47             # The numbers start with the lower address in the LSB,
48             # and the first number contains the lower address.
49             # Returns an array of ones and zeros.
50             sub explode_bit_values {
51 9     9 0 1053 my ($self, @values) = @_;
52 9         19 @values = map { sprintf "%08B", $_ } @values;
  263         614  
53 9         34 @values = map { reverse split // } @values;
  263         1504  
54 9         906 return @values;
55             }
56              
57             1;
58              
59             __END__