File Coverage

blib/lib/Device/Modbus.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 28 30 93.3


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