| 1 | package Device::Modbus::Response; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use parent 'Device::Modbus'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Device::Modbus::Exception; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | my %parameters_for = ( | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 'Read Coils' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | => [qw(code bytes values)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 'Read Discrete Inputs' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | => [qw(code bytes values)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 'Read Holding Registers' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | => [qw(code bytes values)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 'Read Input Registers' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | => [qw(code bytes values)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 'Write Single Coil' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | => [qw(code address value)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 'Write Single Register' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | => [qw(code address value)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 'Write Multiple Coils' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | => [qw(code address quantity)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 'Write Multiple Registers' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | => [qw(code address quantity)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 'Read/Write Multiple Registers' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | => [qw(code bytes values)], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 10 | my %format_for = ( | |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0x01 => 'CCC*', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0x02 => 'CCC*', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0x03 => 'CCn*', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0x04 => 'CCn*', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0x05 => 'Cnn', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0x06 => 'Cnn', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0x0F => 'Cnn', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0x10 => 'Cnn', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0x17 => 'CCn*', | ||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 44 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=29 SCAR=33.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 44 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # Must receive either a function name or a function code | ||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | croak 'A function name or code is required when creating a response' | ||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 44 | ▶ | unless $args{function} || $args{code}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"function"} or $args{"code"}
MC/DC100%2/2
$args{"function"} or $args{"code"}
$args{"function"}
$args{"code"}
Truth table100%3/3
A or B
A= $args{"function"}B= $args{"code"}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 43 | ▶ | if ($args{function}) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | croak "Function $args{function} is not supported" | ||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 17 | ▶ | unless exists $Device::Modbus::code_for{$args{function}}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 16 | $args{code} = $Device::Modbus::code_for{$args{function}}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | croak "Code $args{code} is not supported" | ||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 26 | ▶ | unless exists $Device::Modbus::function_for{$args{code}}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 25 | $args{function} = $Device::Modbus::function_for{$args{code}}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # Validate we have all the needed parameters | ||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 41 | foreach (@{$parameters_for{$args{function}}}) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # This is calculated | ||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 123 | ▶ | next if $_ eq 'bytes'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # But the rest are required | ||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | croak "Response for function $args{function} requires '$_'" | ||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 103 | ▶ | unless exists $args{$_}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # Validate parameters | ||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 39 | ▶ | if ($args{code} == 0x01 || $args{code} == 0x02) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
Condition100%3/3
$args{"code"} == 1 or $args{"code"} == 2
MC/DC100%2/2
$args{"code"} == 1 or $args{"code"} == 2
$args{"code"} == 1
$args{"code"} == 2
Truth table100%3/3
A or B
A= $args{"code"} == 1B= $args{"code"} == 2
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 9 | ▶ | unless (@{$args{values}} > 0 && @{$args{values}} <= 0x7D0) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$args{"values"};} > 0 and @{$args{"values"};} <= 2000
MC/DC100%2/2
@{$args{"values"};} > 0 and @{$args{"values"};} <= 2000
@{$args{"values"};} > 0
@{$args{"values"};} <= 2000
Truth table100%3/3
A and B
A= @{$args{"values"};} > 0B= @{$args{"values"};} <= 2000
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | die Device::Modbus::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | code => $args{code} + 0x80, | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | exception_code => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ▶ | elsif ($args{code} == 0x03 || $args{code} == 0x04 || $args{code} == 0x17) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$args{"code"} == 3 or $args{"code"} == 4 or $args{"code"} == 23
$args{"code"} == 3 or $args{"code"} == 4
MC/DC100%3/3
$args{"code"} == 3 or $args{"code"} == 4 or $args{"code"} == 23
$args{"code"} == 3
$args{"code"} == 4
$args{"code"} == 23
Truth table100%4/4
A or B or C
A= $args{"code"} == 3B= $args{"code"} == 4C= $args{"code"} == 23
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 10 | ▶ | unless (@{$args{values}} > 0 && @{$args{values}} <= 0x7D) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{$args{"values"};} > 0 and @{$args{"values"};} <= 125
MC/DC100%2/2
@{$args{"values"};} > 0 and @{$args{"values"};} <= 125
@{$args{"values"};} > 0
@{$args{"values"};} <= 125
Truth table100%3/3
A and B
A= @{$args{"values"};} > 0B= @{$args{"values"};} <= 125
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | die Device::Modbus::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | code => $args{code} + 0x80, | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | exception_code => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | elsif ($args{code} == 0x05) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | unless (defined $args{value}) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | die Device::Modbus::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | code => $args{code} + 0x80, | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | exception_code => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | elsif ($args{code} == 0x06) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 5 | ▶ | unless ($args{value} >= 0 && $args{value} <= 0xFFFF) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"value"} >= 0 and $args{"value"} <= 65535
MC/DC100%2/2
$args{"value"} >= 0 and $args{"value"} <= 65535
$args{"value"} >= 0
$args{"value"} <= 65535
Truth table100%3/3
A and B
A= $args{"value"} >= 0B= $args{"value"} <= 65535
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | die Device::Modbus::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | code => $args{code} + 0x80, | |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | exception_code => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | elsif ($args{code} == 0x0F) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 4 | ▶ | unless ($args{quantity} > 0 && $args{quantity} <= 0x7B0) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"quantity"} > 0 and $args{"quantity"} <= 1968
MC/DC100%2/2
$args{"quantity"} > 0 and $args{"quantity"} <= 1968
$args{"quantity"} > 0
$args{"quantity"} <= 1968
Truth table100%3/3
A and B
A= $args{"quantity"} > 0B= $args{"quantity"} <= 1968
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | die Device::Modbus::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | code => $args{code} + 0x80, | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | exception_code => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | elsif ($args{code} == 0x10) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 5 | ▶ | unless ($args{quantity} >= 1 && $args{quantity} <= 0x7B) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"quantity"} >= 1 and $args{"quantity"} <= 123
MC/DC100%2/2
$args{"quantity"} >= 1 and $args{"quantity"} <= 123
$args{"quantity"} >= 1
$args{"quantity"} <= 123
Truth table100%3/3
A and B
A= $args{"quantity"} >= 1B= $args{"quantity"} <= 123
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | die Device::Modbus::Exception->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | code => $args{code} + 0x80, | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | exception_code => 3 | ||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 27 | return bless \%args, $class; | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 9 | ▶ | sub pdu { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pdu: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 9 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 9 | ▶ | if ($self->{code} == 0x01 || $self->{code} == 0x02) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition100%3/3
$self->{"code"} == 1 or $self->{"code"} == 2
MC/DC100%2/2
$self->{"code"} == 1 or $self->{"code"} == 2
$self->{"code"} == 1
$self->{"code"} == 2
Truth table100%3/3
A or B
A= $self->{"code"} == 1B= $self->{"code"} == 2
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | my $values = $self->flatten_bit_values($self->{values}); | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | return pack('CC', $self->{code}, scalar(@$values)) | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | . join '', @$values; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | ▶ | elsif ($self->{code} == 0x03 || $self->{code} == 0x04 || $self->{code} == 0x17) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$self->{"code"} == 3 or $self->{"code"} == 4 or $self->{"code"} == 23
$self->{"code"} == 3 or $self->{"code"} == 4
MC/DC66%2/3
$self->{"code"} == 3 or $self->{"code"} == 4 or $self->{"code"} == 23
$self->{"code"} == 3
$self->{"code"} == 4
$self->{"code"} == 23
Truth table75%3/4
A or B or C
A= $self->{"code"} == 3B= $self->{"code"} == 4C= $self->{"code"} == 23
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | my $bytes = 2 * scalar @{$self->{values}}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | return pack $format_for{$self->{code}}, | ||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 2 | $self->{code}, $bytes, @{$self->{values}}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | ▶ | elsif ($self->{code} == 0x05 || $self->{code} == 0x06) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"code"} == 5 or $self->{"code"} == 6
MC/DC100%2/2
$self->{"code"} == 5 or $self->{"code"} == 6
$self->{"code"} == 5
$self->{"code"} == 6
Truth table100%3/3
A or B
A= $self->{"code"} == 5B= $self->{"code"} == 6
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | my $value = $self->{value}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 3 | ▶ | $value = 0xFF00 if $self->{code} == 0x05 && $self->{value}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"code"} == 5 and $self->{"value"}
MC/DC100%2/2
$self->{"code"} == 5 and $self->{"value"}
$self->{"code"} == 5
$self->{"value"}
Truth table100%3/3
A and B
A= $self->{"code"} == 5B= $self->{"value"}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | return pack $format_for{$self->{code}}, | ||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 3 | $self->{code}, $self->{address}, $value; | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | ▶ | elsif ($self->{code} == 0x0F || $self->{code} == 0x10) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"code"} == 15 or $self->{"code"} == 16
MC/DC0%0/2
$self->{"code"} == 15 or $self->{"code"} == 16
$self->{"code"} == 15
$self->{"code"} == 16
Truth table66%2/3
A or B
A= $self->{"code"} == 15B= $self->{"code"} == 16
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | return pack $format_for{$self->{code}}, | ||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | $self->{code}, $self->{address}, $self->{quantity}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||