| 1 | package Device::Modbus::Client; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Device::Modbus; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Device::Modbus::Request; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Device::Modbus::Response; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Device::Modbus::Exception; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | ### Request building | ||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | sub read_coils { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_coils: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | $args{function} = 'Read Coils'; | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub read_discrete_inputs { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_discrete_inputs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | $args{function} = 'Read Discrete Inputs'; | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub read_input_registers { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_input_registers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | $args{function} = 'Read Input Registers'; | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | sub read_holding_registers { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_holding_registers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | $args{function} = 'Read Holding Registers'; | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | sub write_single_coil { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_single_coil: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | $args{function} = 'Write Single Coil'; | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | sub write_single_register { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_single_register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | $args{function} = 'Write Single Register'; | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 3 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub write_multiple_coils { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_multiple_coils: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $args{function} = 'Write Multiple Coils'; | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | sub write_multiple_registers { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_multiple_registers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | $args{function} = 'Write Multiple Registers'; | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | sub read_write_registers { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_write_registers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | my ($self, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | $args{function} = 'Read/Write Multiple Registers'; | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | return Device::Modbus::Request->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | ### Send request | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub send_request { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my ($self, $request) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $adu = $self->new_adu($request); | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $self->write_port($adu); | |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | ### Response parsing | ||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | # Parse the Application Data Unit | ||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub receive_response { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
receive_response: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $self->read_port; | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $adu = $self->new_adu(); | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $self->parse_header($adu); | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | $self->parse_pdu($adu); | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->parse_footer($adu); | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return $adu; | |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 21 | ▶ | sub parse_pdu { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_pdu: called CC=17 SCAR=28.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 21 | my ($self, $adu) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 21 | my $response; | |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | 21 | my $code = $self->parse_buffer(1,'C'); | |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 20 | ▶ | if ($code == 0x01 || $code == 0x02) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition100%3/3
$code == 1 or $code == 2
MC/DC100%2/2
$code == 1 or $code == 2
$code == 1
$code == 2
Truth table100%3/3
A or B
A= $code == 1B= $code == 2
| |||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Read coils and discrete inputs | ||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | my ($byte_count) = $self->parse_buffer(1, 'C'); | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | croak "Invalid byte count: <$byte_count>" | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 98 | unless $byte_count > 0; | ||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | my @values = $self->parse_buffer($byte_count, 'C*'); | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | @values = Device::Modbus->explode_bit_values(@values); | |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | $response = Device::Modbus::Response->new( | |||||||||||||||||||||||||||||||||||||||||||
| 104 | code => $code, | ||||||||||||||||||||||||||||||||||||||||||||
| 105 | bytes => $byte_count, | ||||||||||||||||||||||||||||||||||||||||||||
| 106 | values => \@values | ||||||||||||||||||||||||||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 109 | ▶ | elsif ($code == 0x03 || $code == 0x04 || $code == 0x17) { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$code == 3 or $code == 4 or $code == 23
$code == 3 or $code == 4
MC/DC100%3/3
$code == 3 or $code == 4 or $code == 23
$code == 3
$code == 4
$code == 23
Truth table100%4/4
A or B or C
A= $code == 3B= $code == 4C= $code == 23
| |||||||||||||||||||||||||||||||||||||||||||||
| 110 | # Read holding and input registers; read/write registers | ||||||||||||||||||||||||||||||||||||||||||||
| 111 | 6 | my ($byte_count) = $self->parse_buffer(1, 'C'); | |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 6 | ▶ | croak "Invalid byte count: <$byte_count>" | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$byte_count > 0 and $byte_count <= 250 and $byte_count % 2 == 0
$byte_count > 0 and $byte_count <= 250
MC/DC100%3/3
$byte_count > 0 and $byte_count <= 250 and $byte_count % 2 == 0
$byte_count > 0
$byte_count <= 250
$byte_count % 2 == 0
Truth table100%4/4
A and B and C
A= $byte_count > 0B= $byte_count <= 250C= $byte_count % 2 == 0
| |||||||||||||||||||||||||||||||||||||||||||||
| 114 | unless $byte_count > 0 && $byte_count <= 250 && $byte_count % 2 == 0; | ||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | my @values = $self->parse_buffer($byte_count, 'n*'); | |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | $response = Device::Modbus::Response->new( | |||||||||||||||||||||||||||||||||||||||||||
| 119 | code => $code, | ||||||||||||||||||||||||||||||||||||||||||||
| 120 | bytes => $byte_count, | ||||||||||||||||||||||||||||||||||||||||||||
| 121 | values => \@values | ||||||||||||||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 124 | ▶ | elsif ($code == 0x05 || $code == 0x06) { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$code == 5 or $code == 6
MC/DC100%2/2
$code == 5 or $code == 6
$code == 5
$code == 6
Truth table100%3/3
A or B
A= $code == 5B= $code == 6
| |||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Write single coil and single register | ||||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | my ($address, $value) = $self->parse_buffer(4, 'n*'); | |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | if ($code == 0x05) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | ▶ | $value = 1 if $value; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | $response = Device::Modbus::Response->new( | |||||||||||||||||||||||||||||||||||||||||||
| 133 | code => $code, | ||||||||||||||||||||||||||||||||||||||||||||
| 134 | address => $address, | ||||||||||||||||||||||||||||||||||||||||||||
| 135 | value => $value | ||||||||||||||||||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 138 | ▶ | elsif ($code == 0x0F || $code == 0x10) { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$code == 15 or $code == 16
MC/DC100%2/2
$code == 15 or $code == 16
$code == 15
$code == 16
Truth table100%3/3
A or B
A= $code == 15B= $code == 16
| |||||||||||||||||||||||||||||||||||||||||||||
| 139 | # Write multiple coils, multiple registers | ||||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | my ($address, $qty) = $self->parse_buffer(4, 'n*'); | |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | $response = Device::Modbus::Response->new( | |||||||||||||||||||||||||||||||||||||||||||
| 143 | code => $code, | ||||||||||||||||||||||||||||||||||||||||||||
| 144 | address => $address, | ||||||||||||||||||||||||||||||||||||||||||||
| 145 | quantity => $qty | ||||||||||||||||||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 148 | 45 | elsif (grep { $code == $_ } 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x8F, 0x90, 0x97) { | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | my ($exc_code) = $self->parse_buffer(1, 'C'); | |||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | $response = Device::Modbus::Exception->new( | |||||||||||||||||||||||||||||||||||||||||||
| 152 | code => $code, | ||||||||||||||||||||||||||||||||||||||||||||
| 153 | exception_code => $exc_code | ||||||||||||||||||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 157 | 4 | croak "Unimplemented function: <$code>"; | |||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 12 | $adu->message($response); | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 12 | return $response; | |||||||||||||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||