| 1 | # Perl module: Client ModBus / TCP class 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # Version: 1.56 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # Website: https://github.com/sourceperl/MBclient/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Date: 2014-08-05 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # License: MIT (http://http://opensource.org/licenses/mit-license.php) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # Description: Client ModBus / TCP command line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Support functions 3 and 16 (class 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # 1,2,4,5,6 (Class 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # Charset: us-ascii, unix end of line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # todo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # - add support for MEI function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | package MBclient; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ## Required Modules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use 5.006_001; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use vars qw($AUTOLOAD $VERSION @ISA @EXPORT); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | @EXPORT = qw(MODBUS_TCP MODBUS_RTU | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | EXP_ILLEGAL_FUNCTION EXP_DATA_ADDRESS EXP_DATA_VALUE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | EXP_SLAVE_DEVICE_FAILURE EXP_ACKNOWLEDGE EXP_SLAVE_DEVICE_BUSY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | EXP_MEMORY_PARITY_ERROR EXP_GATEWAY_PATH_UNAVAILABLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | EXP_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | MB_NO_ERR MB_RESOLVE_ERR MB_CONNECT_ERR MB_SEND_ERR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | MB_RECV_ERR MB_TIMEOUT_ERR MB_FRAME_ERR MB_EXCEPT_ERR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use Socket; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | our $VERSION = '1.56'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ## Constant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ## ModBus/TCP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | use constant MODBUS_PORT => 502; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | ## ModBus RTU | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | use constant FRAME_RTU_MAXSIZE => 512; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ## Modbus mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | use constant MODBUS_TCP => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | use constant MODBUS_RTU => 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ## Modbus function code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # standard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | use constant READ_COILS => 0x01; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | use constant READ_DISCRETE_INPUTS => 0x02; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | ▶ | use constant READ_HOLDING_REGISTERS => 0x03; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | use constant READ_INPUT_REGISTERS => 0x04; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | use constant WRITE_SINGLE_COIL => 0x05; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | use constant WRITE_SINGLE_REGISTER => 0x06; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | use constant WRITE_MULTIPLE_REGISTERS => 0x10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | use constant MODBUS_ENCAPSULATED_INTERFACE => 0x2B; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | ## Modbus except code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | use constant EXP_ILLEGAL_FUNCTION => 0x01; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | use constant EXP_DATA_ADDRESS => 0x02; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | use constant EXP_DATA_VALUE => 0x03; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | ▶ | use constant EXP_SLAVE_DEVICE_FAILURE => 0x04; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | use constant EXP_ACKNOWLEDGE => 0x05; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | use constant EXP_SLAVE_DEVICE_BUSY => 0x06; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | use constant EXP_MEMORY_PARITY_ERROR => 0x08; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | use constant EXP_GATEWAY_PATH_UNAVAILABLE => 0x0A; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | use constant EXP_GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND => 0x0B; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | ## Module error codes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | use constant MB_NO_ERR => 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | ▶ | use constant MB_RESOLVE_ERR => 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | use constant MB_CONNECT_ERR => 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | use constant MB_SEND_ERR => 3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | use constant MB_RECV_ERR => 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | use constant MB_TIMEOUT_ERR => 5; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | use constant MB_FRAME_ERR => 6; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | use constant MB_EXCEPT_ERR => 7; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ## Constructor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $this = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table0%0/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ## UPPERCASE items have documented accessor functions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ## lowercase items are reserved for internal use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $self->{VERSION} = $VERSION; # version number | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $self->{HOST} = undef; # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $self->{PORT} = MODBUS_PORT; # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $self->{UNIT_ID} = 1; # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $self->{LAST_ERROR} = MB_NO_ERR; # last error code | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $self->{LAST_EXCEPT} = 0; # last expect code | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $self->{MODE} = MODBUS_TCP; # by default modbus/tcp | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $self->{sock} = undef; # socket handle | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $self->{timeout} = 30; # socket timeout | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $self->{hd_tr_id} = 0; # store transaction ID | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $self->{debug} = 0; # enable debug trace | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # object bless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | ## Get current version number. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | sub version { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return $self->{VERSION}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ## Get last error code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | sub last_error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | return $self->{LAST_ERROR}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | ## Get last except code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | sub last_except { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_except: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | return $self->{LAST_EXCEPT}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | ## Get or set host field (IPv4 or hostname like "plc.domain.net"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | sub host { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
host: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $hostname = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # return last hostname if no arg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return $self->{HOST} unless defined $hostname; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | # if host is IPv4 address or valid URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (($hostname =~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ or $hostname =~ /^[a-z][a-z0-9\.\-]+$/
MC/DC0%0/2
$hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ or $hostname =~ /^[a-z][a-z0-9\.\-]+$/
$hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
$hostname =~ /^[a-z][a-z0-9\.\-]+$/
Truth table0%0/3
A or B
A= $hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/B= $hostname =~ /^[a-z][a-z0-9\.\-]+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | ($hostname =~ m/^[a-z][a-z0-9\.\-]+$/)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $self->{HOST} = $hostname; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return $self->{HOST}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | ## Get or set TCP port field. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | sub port { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
port: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $port = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # return last hostname if no arg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return $self->{PORT} unless defined $port; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # if host is IPv4 address or valid URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if (($port =~ m/^\d{1,5}$/) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$port =~ /^\d{1,5}$/ and $port < 65536
MC/DC0%0/2
$port =~ /^\d{1,5}$/ and $port < 65536
$port =~ /^\d{1,5}$/
$port < 65536
Truth table0%0/3
A and B
A= $port =~ /^\d{1,5}$/B= $port < 65536
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | ($port < 65536)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $self->{PORT} = $port; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | return $self->{PORT}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | ## Get or set unit_id field. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | sub unit_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unit_id: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $uid = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # return unit_id if no arg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | return $self->{UNIT_ID} unless defined $uid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # if uid is numeric, set unit_id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ($uid =~ m/^\d{1,3}$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | $self->{UNIT_ID} = $uid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | return $self->{UNIT_ID}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | ## Get or set modbus mode (TCP or RTU). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | sub mode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mode: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $mode = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # return mode if no arg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | return $self->{MODE} unless defined $mode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # set mode and return mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | $self->{MODE} = $mode; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | return $self->{MODE}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | ## Open TCP link. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | sub open { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | print 'call open()'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # restart TCP if already open | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | $self->close if ($self->is_open); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # name resolve | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $ad_ip = inet_aton($self->{HOST}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | unless($ad_ip) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $self->{LAST_ERROR} = MB_RESOLVE_ERR; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | print 'IP resolve error'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # set socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | socket($self->{sock}, PF_INET, SOCK_STREAM, getprotobyname('tcp')); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $connect_ok = connect($self->{sock}, sockaddr_in($self->{PORT}, $ad_ip)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($connect_ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | $self->{sock} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | $self->{LAST_ERROR} = MB_CONNECT_ERR; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | print 'TCP connect error'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | ## Check TCP link. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | sub is_open { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_open: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | return (defined $self->{sock}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | ## Close TCP link. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | sub close { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | if ($self->{sock}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | close $self->{sock}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | $self->{sock} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | ## Modbus function READ_COILS (0x01). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | ## read_coils(bit_addr, bit_number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | ## return a ref to result array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | ## or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | sub read_coils { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_coils: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | my $bit_addr = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | my $bit_nb = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # build frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | my $tx_buffer = $self->_mbus_frame(READ_COILS, pack("nn", $bit_addr, $bit_nb)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my $s_send = $self->_send_mbus($tx_buffer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | return undef unless ($s_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my $f_body = $self->_recv_mbus(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | return undef unless ($f_body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # register extract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | my ($rx_byte_count, $f_bits) = unpack 'Cb*', $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # read bit(s) string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | my @bits = split //, $f_bits; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $#bits = $bit_nb - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | return \@bits; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | ## Modbus function READ_DISCRETE_INPUTS (0x02). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | ## read_discrete_inputs(bit_addr, bit_number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | ## return a ref to result array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | ## or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | sub read_discrete_inputs { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_discrete_inputs: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | my $bit_addr = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | my $bit_nb = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | # build frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | my $tx_buffer = $self->_mbus_frame(READ_DISCRETE_INPUTS, pack("nn", $bit_addr, $bit_nb)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my $s_send = $self->_send_mbus($tx_buffer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | return undef unless ($s_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | # receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my $f_body = $self->_recv_mbus(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | return undef unless ($f_body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # register extract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | my ($rx_byte_count, $f_bits) = unpack 'Cb*', $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # read bit(s) string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | my @bits = split //, $f_bits; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | $#bits = $bit_nb - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | return \@bits; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | ## Modbus function READ_HOLDING_REGISTERS (0x03). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | ## read_holding_registers(reg_addr, reg_number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | ## return a ref to result array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | ## or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | sub read_holding_registers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_holding_registers: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | my $reg_addr = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | my $reg_nb = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # build frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | my $tx_buffer = $self->_mbus_frame(READ_HOLDING_REGISTERS, pack("nn", $reg_addr, $reg_nb)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | my $s_send = $self->_send_mbus($tx_buffer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | return undef unless ($s_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | my $f_body = $self->_recv_mbus(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | return undef unless ($f_body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | # register extract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | my ($rx_reg_count, $f_regs) = unpack 'Ca*', $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # read 16 bits register | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | my @registers = unpack 'n*', $f_regs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | return \@registers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | ## Modbus function READ_INPUT_REGISTERS (0x04). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | ## read_input_registers(reg_addr, reg_number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | ## return a ref to result array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | ## or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | sub read_input_registers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_input_registers: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | my $reg_addr = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | my $reg_nb = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # build frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my $tx_buffer = $self->_mbus_frame(READ_INPUT_REGISTERS, pack("nn", $reg_addr, $reg_nb)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | my $s_send = $self->_send_mbus($tx_buffer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | return undef unless ($s_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | # receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | my $f_body = $self->_recv_mbus(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | return undef unless ($f_body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # register extract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my ($rx_reg_count, $f_regs) = unpack 'Ca*', $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | # read 16 bits register | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | my @registers = unpack 'n*', $f_regs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | return \@registers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | ## Modbus function WRITE_SINGLE_COIL (0x05). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | ## write_single_coil(bit_addr, bit_value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | ## return 1 if write success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | ## or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | sub write_single_coil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_single_coil: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | my $bit_addr = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | my $bit_value = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # build frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | ▶ | $bit_value = ($bit_value) ? 0xFF : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | my $tx_buffer = $self->_mbus_frame(WRITE_SINGLE_COIL, pack("nCC", $bit_addr, $bit_value, 0)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | my $s_send = $self->_send_mbus($tx_buffer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | return undef unless ($s_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | my $f_body = $self->_recv_mbus(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | ▶ | return undef unless ($f_body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # register extract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | my ($rx_bit_addr, $rx_bit_value, $rx_padding) = unpack 'nCC', $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # check bit write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | ▶ | return (($rx_bit_addr == $bit_addr) and ($rx_bit_value == $bit_value)) ? 1 : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rx_bit_addr == $bit_addr and $rx_bit_value == $bit_value
MC/DC0%0/2
$rx_bit_addr == $bit_addr and $rx_bit_value == $bit_value
$rx_bit_addr == $bit_addr
$rx_bit_value == $bit_value
Truth table0%0/3
A and B
A= $rx_bit_addr == $bit_addrB= $rx_bit_value == $bit_value
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | ## Modbus function WRITE_SINGLE_REGISTER (0x06). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | ## write_single_register(reg_addr, reg_value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | ## return 1 if write success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | ## or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | sub write_single_register { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_single_register: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | my $reg_addr = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | my $reg_value = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | # build frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | my $tx_buffer = $self->_mbus_frame(WRITE_SINGLE_REGISTER, pack("nn", $reg_addr, $reg_value)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | my $s_send = $self->_send_mbus($tx_buffer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | return undef unless ($s_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | # receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | my $f_body = $self->_recv_mbus(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | return undef unless ($f_body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # register extract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | my ($rx_reg_addr, $rx_reg_value) = unpack 'nn', $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | # check bit write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | return (($rx_reg_addr == $reg_addr) and ($rx_reg_value == $reg_value)) ? 1 : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rx_reg_addr == $reg_addr and $rx_reg_value == $reg_value
MC/DC0%0/2
$rx_reg_addr == $reg_addr and $rx_reg_value == $reg_value
$rx_reg_addr == $reg_addr
$rx_reg_value == $reg_value
Truth table0%0/3
A and B
A= $rx_reg_addr == $reg_addrB= $rx_reg_value == $reg_value
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | ## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | ## Modbus function WRITE_MULTIPLE_REGISTERS (0x10). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | ## write_multiple_registers(reg_addr, ref_to_reg_array) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | ## return 1 if write success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | ## or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | sub write_multiple_registers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_multiple_registers: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | my $reg_addr = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | my $ref_array_reg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | my @reg_value = @$ref_array_reg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # build frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # register | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 0 | my $reg_nb = @reg_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # format reg value string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | my $reg_val_str; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | for (@reg_value) {$reg_val_str .= pack("n", $_);} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | my $bytes_nb = bytes::length($reg_val_str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # format modbus frame body | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | my $body = pack("nnC", $reg_addr, $reg_nb, $bytes_nb).$reg_val_str; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | my $tx_buffer = $self->_mbus_frame(WRITE_MULTIPLE_REGISTERS, $body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | my $s_send = $self->_send_mbus($tx_buffer); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | ▶ | return undef unless ($s_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | # receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | my $f_body = $self->_recv_mbus(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # check error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | ▶ | return undef unless ($f_body); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | # register extract | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | my ($rx_reg_addr, $rx_reg_nb) = unpack 'nn', $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | # check regs write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | return ($rx_reg_addr == $reg_addr) ? 1 : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | # Build modbus frame. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | # _mbus_frame(function code, body) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | # return modbus frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | sub _mbus_frame { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mbus_frame: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | my $fc = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | my $body = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | # build frame body | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | my $f_body = pack("C", $fc).$body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | # modbus/TCP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | ▶ | if ($self->{MODE} == MODBUS_TCP) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # build frame ModBus Application Protocol header (mbap) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | $self->{hd_tr_id} = int(rand 65535); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 0 | my $tx_hd_pr_id = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | my $tx_hd_length = bytes::length($f_body) + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | my $f_mbap = pack("nnnC", $self->{hd_tr_id}, $tx_hd_pr_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | $tx_hd_length, $self->{UNIT_ID}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | return $f_mbap.$f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | # modbus RTU | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | } elsif ($self->{MODE} == MODBUS_RTU) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # format [slave addr(unit_id)]frame_body[CRC16] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | my $slave_ad = pack("C", $self->{UNIT_ID}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | return $self->_add_crc($slave_ad.$f_body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # unknow mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | # Send modbus frame. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | # _send_mbus(frame) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | # return $nb_byte send | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | sub _send_mbus { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send_mbus: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | my $frame = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | # send request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | my $bytes_send = $self->_send($frame); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | return undef unless ($bytes_send); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # for debug | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | ▶ | $self->_pretty_dump('Tx', $frame) if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | # return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | return $bytes_send; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # Recv modbus frame. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # _recv_mbus() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | # return body (after func. code) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | sub _recv_mbus { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_recv_mbus: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | ## receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | # vars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | my ($rx_buffer,$rx_frame); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | my ($rx_unit_id, $rx_bd_fc, $f_body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # modbus TCP receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | ▶ | if ($self->{MODE} == MODBUS_TCP) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | # 7 bytes head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | $rx_buffer = $self->_recv(7); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | ▶ | return undef unless($rx_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | $rx_frame = $rx_buffer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | # decode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | my ($rx_hd_tr_id, $rx_hd_pr_id, $rx_hd_length, $rx_hd_unit_id) = unpack "nnnC", $rx_frame; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | # check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | ▶ | if (!(($rx_hd_tr_id == $self->{hd_tr_id}) && ($rx_hd_pr_id == 0) && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$rx_hd_tr_id == $self->{"hd_tr_id"} and $rx_hd_pr_id == 0 and $rx_hd_length < 256 and $rx_hd_unit_id == $self->{"UNIT_ID"}
$rx_hd_tr_id == $self->{"hd_tr_id"} and $rx_hd_pr_id == 0 and $rx_hd_length < 256
$rx_hd_tr_id == $self->{"hd_tr_id"} and $rx_hd_pr_id == 0
MC/DC0%0/4
$rx_hd_tr_id == $self->{"hd_tr_id"} and $rx_hd_pr_id == 0 and $rx_hd_length < 256 and $rx_hd_unit_id == $self->{"UNIT_ID"}
$rx_hd_tr_id == $self->{"hd_tr_id"}
$rx_hd_pr_id == 0
$rx_hd_length < 256
$rx_hd_unit_id == $self->{"UNIT_ID"}
Truth table0%0/5
A and B and C and D
A= $rx_hd_tr_id == $self->{"hd_tr_id"}B= $rx_hd_pr_id == 0C= $rx_hd_length < 256D= $rx_hd_unit_id == $self->{"UNIT_ID"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | ($rx_hd_length < 256) && ($rx_hd_unit_id == $self->{UNIT_ID}))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | $self->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | # end of frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | $rx_buffer = $self->_recv($rx_hd_length-1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | ▶ | return undef unless($rx_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | $rx_frame .= $rx_buffer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | # dump frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | ▶ | $self->_pretty_dump('Rx', $rx_frame) if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | # body decode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | ($rx_bd_fc, $f_body) = unpack "Ca*", $rx_buffer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | # modbus RTU receive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | } elsif ($self->{MODE} == MODBUS_RTU) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | $rx_buffer = $self->_recv(FRAME_RTU_MAXSIZE); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | ▶ | return undef unless($rx_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | $rx_frame = $rx_buffer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | # dump frame | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | ▶ | $self->_pretty_dump('Rx', $rx_frame) if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | # body decode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | ($rx_unit_id, $rx_bd_fc, $f_body) = unpack "CCa*", $rx_frame; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | # check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 0 | ▶ | if (!($rx_unit_id == $self->{UNIT_ID})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | $self->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | # check except | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 0 | ▶ | if ($rx_bd_fc > 0x80) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | # except code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | my ($exp_code) = unpack "C", $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | $self->{LAST_ERROR} = MB_EXCEPT_ERR; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | $self->{LAST_EXCEPT} = $exp_code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | ▶ | print 'except (code '.$exp_code.')'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | # return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | return $f_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | # Send data over current socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | # _send(data_to_send) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | # return the number of bytes send | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | # or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | sub _send { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | # check link, open if need | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | ▶ | unless ($self->is_open) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 0 | ▶ | print 'call _send() not open -> call open()'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 0 | ▶ | return undef unless ($self->open); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | # send data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 0 | my $data_l = bytes::length($data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 0 | my $send_l = send($self->{sock}, $data, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | # send error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 0 | ▶ | if ($send_l != $data_l) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | $self->{LAST_ERROR} = MB_SEND_ERR; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 0 | ▶ | print '_send error'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | $self->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 0 | return $send_l; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | # Recv data over current socket. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | # _recv(max_size) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | # return the receive buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | # or undef if error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | sub _recv { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_recv: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | my $max_size = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | # wait for read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | ▶ | unless ($self->_can_read()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | 0 | $self->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | # recv | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | my $buffer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 0 | my $s_recv = recv($self->{sock}, $buffer, $max_size, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 0 | ▶ | unless (defined $s_recv) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 0 | $self->{LAST_ERROR} = MB_RECV_ERR; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | ▶ | print '_recv error'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | $self->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | return $buffer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | # Wait for socket read. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | sub _can_read { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_can_read: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | my $hdl_select = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | vec($hdl_select, fileno($self->{sock}), 1) = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | my $_select = select($hdl_select, undef, undef, $self->{timeout}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | ▶ | if ($_select) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 0 | return $_select; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 0 | $self->{LAST_ERROR} = MB_TIMEOUT_ERR; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 0 | ▶ | print 'timeout error'."\n" if ($self->{debug}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | $self->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | # Compute modbus CRC16 (for RTU mode). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | # _crc(modbus_frame) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | # return the CRC | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | sub _crc { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_crc: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 0 | my $frame = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | my $crc = 0xFFFF; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | my ($chr, $lsb); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | for my $i (0..bytes::length($frame)-1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | $chr = ord(bytes::substr($frame, $i, 1)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 0 | $crc ^= $chr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | for (1..8) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | $lsb = $crc & 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 0 | $crc >>= 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | ▶ | $crc ^= 0xA001 if $lsb; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | return $crc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | # Add CRC to modbus frame (for RTU mode). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | # _add_crc(modbus_frame) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | # return modbus_frame_with_crc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | sub _add_crc { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_crc: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | my $frame = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | my $crc = pack 'v', $self->_crc($frame); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 0 | return $frame.$crc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | # Check the CRC of modbus RTU frame. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | # _crc_is_ok(modbus_frame_with_crc) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | # return true if CRC is ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | sub _crc_is_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_crc_is_ok: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 0 | my $frame = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 0 | my $crc = unpack('v', bytes::substr($frame, -2)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 0 | return ($crc == $self->_crc($frame)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | # Print modbus/TCP frame ("[header]body") or modbus RTU ("body[CRC]"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | sub _pretty_dump { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pretty_dump: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 0 | my $label = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 0 | my $data = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 0 | my @dump = map {sprintf "%02X", $_ } unpack("C*", $data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | # format for TCP or RTU | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | 0 | ▶ | if ($self->{MODE} == MODBUS_TCP) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | 0 | $dump[0] = "[".$dump[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | $dump[5] = $dump[5]."]"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | } elsif ($self->{MODE} == MODBUS_RTU) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | $dump[$#dump-1] = "[".$dump[$#dump-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | $dump[$#dump] = $dump[$#dump]."]"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | # print result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 0 | print $label."\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | for (@dump) {print $_." ";} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 0 | print "\n\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||