blib/lib/MBclient.pm

filter: Statementstmt 28.7% 114 / 397 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 56.9% 37 / 65 Podpod 100% 18 / 18 totaltotal 26.1% 169 / 646 SCAR 80.2
CC
77
Cov
21%
_recv_mbus
380.0
open
56.0
write_single_coil
42.0
CRAP 3026.4
↑ Summary Build »
1 # Perl module: Client ModBus / TCP class 1
2 # Version: 1.57
3 # Website: https://github.com/sourceperl/MBclient/
4 # Date: 2014-11-27
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.57';
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 => 256;
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 2 ▶use constant MB_CRC_ERR => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
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
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless defined $hostname 0 0 0
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
BranchTrueFalseTotal
if ($hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ or $hostname =~ /^[a-z][a-z0-9\.\-]+$/) 0 0 0
Condition0%0/3
$hostname =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ or $hostname =~ /^[a-z][a-z0-9\.\-]+$/
l!l&&r!l&&!r
000
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\.\-]+$/
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless defined $port 0 0 0
160 # if host is IPv4 address or valid URL
161 0 ▶ if (($port =~ m/^\d{1,5}$/) and
Branch: 1 branch
BranchTrueFalseTotal
if ($port =~ /^\d{1,5}$/ and $port < 65536) 0 0 0
Condition0%0/3
$port =~ /^\d{1,5}$/ and $port < 65536
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $uid 0 0 0
177 # if uid is numeric, set unit_id
178 0 ▶ if ($uid =~ m/^\d{1,3}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uid =~ /^\d{1,3}$/) 0 0 0
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
BranchTrueFalseTotal
unless defined $mode 0 0 0
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
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
205 # restart TCP if already open
206 0 ▶ $self->close if ($self->is_open);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_open 0 0 0
207 # name resolve
208 0 my $ad_ip = inet_aton($self->{HOST});
209 0 ▶ unless($ad_ip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ad_ip) 0 0 0
210 0 $self->{LAST_ERROR} = MB_RESOLVE_ERR;
211 0 ▶ print 'IP resolve error'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
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
BranchTrueFalseTotal
if ($connect_ok) { } 0 0 0
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
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
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
BranchTrueFalseTotal
if ($self->{'sock'}) { } 0 0 0
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
BranchTrueFalseTotal
unless $s_send 0 0 0
267 # receive
268 0 my $f_body = $self->_recv_mbus();
269 # check error
270 0 ▶ return undef unless ($f_body);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_body 0 0 0
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
BranchTrueFalseTotal
unless $s_send 0 0 0
295 # receive
296 0 my $f_body = $self->_recv_mbus();
297 # check error
298 0 ▶ return undef unless ($f_body);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_body 0 0 0
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
BranchTrueFalseTotal
unless $s_send 0 0 0
324 # receive
325 0 my $f_body = $self->_recv_mbus();
326 # check error
327 0 ▶ return undef unless ($f_body);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_body 0 0 0
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
BranchTrueFalseTotal
unless $s_send 0 0 0
351 # receive
352 0 my $f_body = $self->_recv_mbus();
353 # check error
354 0 ▶ return undef unless ($f_body);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_body 0 0 0
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
BranchTrueFalseTotal
$bit_value ? : 0 0 0
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
BranchTrueFalseTotal
unless $s_send 0 0 0
379 # receive
380 0 my $f_body = $self->_recv_mbus();
381 # check error
382 0 ▶ return undef unless ($f_body);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_body 0 0 0
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
BranchTrueFalseTotal
$rx_bit_addr == $bit_addr && $rx_bit_value == $bit_value ? : 0 0 0
Condition0%0/3
$rx_bit_addr == $bit_addr and $rx_bit_value == $bit_value
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $s_send 0 0 0
405 # receive
406 0 my $f_body = $self->_recv_mbus();
407 # check error
408 0 ▶ return undef unless ($f_body);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_body 0 0 0
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
BranchTrueFalseTotal
$rx_reg_addr == $reg_addr && $rx_reg_value == $reg_value ? : 0 0 0
Condition0%0/3
$rx_reg_addr == $reg_addr and $rx_reg_value == $reg_value
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless $s_send 0 0 0
440 # receive
441 0 my $f_body = $self->_recv_mbus();
442 # check error
443 0 ▶ return undef unless ($f_body);
Branch: 1 branch
BranchTrueFalseTotal
unless $f_body 0 0 0
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
BranchTrueFalseTotal
$rx_reg_addr == $reg_addr ? : 0 0 0
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
BranchTrueFalseTotal
if ($self->{'MODE'} == 1) { } 0 0 0
elsif ($self->{'MODE'} == 2) { } 0 0 0
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
BranchTrueFalseTotal
unless $bytes_send 0 0 0
488 # for debug
489 0 ▶ $self->_pretty_dump('Tx', $frame) if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
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=19 SCAR=59.4
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
BranchTrueFalseTotal
if ($self->{'MODE'} == 1) { } 0 0 0
elsif ($self->{'MODE'} == 2) { } 0 0 0
505 # 7 bytes head
506 0 $rx_buffer = $self->_recv(7);
507 0 ▶ return undef unless($rx_buffer);
Branch: 1 branch
BranchTrueFalseTotal
unless $rx_buffer 0 0 0
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
BranchTrueFalseTotal
unless ($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"}) 0 0 0
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"}
!ll&&!rl&&r
000
$rx_hd_tr_id == $self->{"hd_tr_id"} and $rx_hd_pr_id == 0 and $rx_hd_length < 256
!ll&&!rl&&r
000
$rx_hd_tr_id == $self->{"hd_tr_id"} and $rx_hd_pr_id == 0
!ll&&!rl&&r
000
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"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
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
BranchTrueFalseTotal
unless $rx_buffer 0 0 0
520 0 $rx_frame .= $rx_buffer;
521 # dump frame
522 0 ▶ $self->_pretty_dump('Rx', $rx_frame) if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
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
BranchTrueFalseTotal
unless $rx_buffer 0 0 0
529 0 $rx_frame = $rx_buffer;
530 # dump frame
531 0 ▶ $self->_pretty_dump('Rx', $rx_frame) if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
532 # RTU frame min size is 5: check this here
533 0 ▶ if (bytes::length($rx_frame) < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (&bytes::length($rx_frame) < 5) 0 0 0
534 0 $self->{LAST_ERROR} = MB_RECV_ERR;
535 0 ▶ print 'short frame error'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
536 0 $self->close;
537 0 return undef;
538 }
539 # check CRC
540 0 ▶ if (! $self->_crc_is_ok($rx_frame)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_crc_is_ok($rx_frame)) 0 0 0
541 0 $self->{LAST_ERROR} = MB_CRC_ERR;
542 0 ▶ print 'CRC error'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
543 0 $self->close;
544 0 return undef;
545 }
546 # remove CRC
547 0 $rx_frame = bytes::substr($rx_frame, 0, -2);
548 # body decode
549 0 ($rx_unit_id, $rx_bd_fc, $f_body) = unpack "CCa*", $rx_frame;
550 # check
551 0 ▶ if (!($rx_unit_id == $self->{UNIT_ID})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rx_unit_id == $self->{"UNIT_ID"}) 0 0 0
552 0 $self->close;
553 0 return undef;
554 }
555 }
556 # check except
557 0 ▶ if ($rx_bd_fc > 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rx_bd_fc > 128) { } 0 0 0
558 # except code
559 0 my ($exp_code) = unpack "C", $f_body;
560 0 $self->{LAST_ERROR} = MB_EXCEPT_ERR;
561 0 $self->{LAST_EXCEPT} = $exp_code;
562 0 ▶ print 'except (code '.$exp_code.')'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
563 0 return undef;
564 } else {
565 # return
566 0 return $f_body;
567 }
568 }
569  
570 # Send data over current socket.
571 # _send(data_to_send)
572 # return the number of bytes send
573 # or undef if error
574 sub _send {
575 0 ▶ my $self = shift;
Subroutine _send: not called CC=6 SCAR=37.4
576 0 my $data = shift;
577 # check link, open if need
578 0 ▶ unless ($self->is_open) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_open) 0 0 0
579 0 ▶ print 'call _send() not open -> call open()'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
580 0 ▶ return undef unless ($self->open);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->open 0 0 0
581 }
582 # send data
583 0 my $data_l = bytes::length($data);
584 0 my $send_l = send($self->{sock}, $data, 0);
585 # send error
586 0 ▶ if ($send_l != $data_l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($send_l != $data_l) { } 0 0 0
587 0 $self->{LAST_ERROR} = MB_SEND_ERR;
588 0 ▶ print '_send error'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
589 0 $self->close;
590 0 return undef;
591 } else {
592 0 return $send_l;
593 }
594 }
595  
596 # Recv data over current socket.
597 # _recv(max_size)
598 # return the receive buffer
599 # or undef if error
600 sub _recv {
601 0 ▶ my $self = shift;
Subroutine _recv: not called CC=4 SCAR=30.0
602 0 my $max_size = shift;
603 # wait for read
604 0 ▶ unless ($self->_can_read()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_can_read) 0 0 0
605 0 $self->close;
606 0 return undef;
607 }
608 # recv
609 0 my $buffer;
610 0 my $s_recv = recv($self->{sock}, $buffer, $max_size, 0);
611 0 ▶ unless (defined $s_recv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s_recv) 0 0 0
612 0 $self->{LAST_ERROR} = MB_RECV_ERR;
613 0 ▶ print '_recv error'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
614 0 $self->close;
615 0 return undef;
616 }
617 0 return $buffer;
618 }
619  
620 # Wait for socket read.
621 sub _can_read {
622 0 ▶ my $self = shift;
Subroutine _can_read: not called CC=3 SCAR=24.8
623 0 my $hdl_select = "";
624 0 vec($hdl_select, fileno($self->{sock}), 1) = 1;
625 0 my $_select = select($hdl_select, undef, undef, $self->{timeout});
626 0 ▶ if ($_select) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_select) { } 0 0 0
627 0 return $_select;
628 } else {
629 0 $self->{LAST_ERROR} = MB_TIMEOUT_ERR;
630 0 ▶ print 'timeout error'."\n" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
631 0 $self->close;
632 0 return undef;
633 }
634 }
635  
636 # Compute modbus CRC16 (for RTU mode).
637 # _crc(modbus_frame)
638 # return the CRC
639 sub _crc {
640 0 ▶ my $self = shift;
Subroutine _crc: not called CC=4 SCAR=30.0
641 0 my $frame = shift;
642 0 my $crc = 0xFFFF;
643 0 my ($chr, $lsb);
644 0 for my $i (0..bytes::length($frame)-1) {
645 0 $chr = ord(bytes::substr($frame, $i, 1));
646 0 $crc ^= $chr;
647 0 for (1..8) {
648 0 $lsb = $crc & 1;
649 0 $crc >>= 1;
650 0 ▶ $crc ^= 0xA001 if $lsb;
Branch: 1 branch
BranchTrueFalseTotal
if $lsb 0 0 0
651 }
652 }
653 0 return $crc;
654 }
655  
656 # Add CRC to modbus frame (for RTU mode).
657 # _add_crc(modbus_frame)
658 # return modbus_frame_with_crc
659 sub _add_crc {
660 0 ▶ my $self = shift;
Subroutine _add_crc: not called CC=1 SCAR=6.9
661 0 my $frame = shift;
662 0 my $crc = pack 'v', $self->_crc($frame);
663 0 return $frame.$crc;
664 }
665  
666 # Check the CRC of modbus RTU frame.
667 # _crc_is_ok(modbus_frame_with_crc)
668 # return true if CRC is ok
669 sub _crc_is_ok {
670 0 ▶ my $self = shift;
Subroutine _crc_is_ok: not called CC=1 SCAR=6.9
671 0 my $frame = shift;
672 0 return ($self->_crc($frame) == 0);
673 }
674  
675 # Print modbus/TCP frame ("[header]body") or modbus RTU ("body[CRC]").
676 sub _pretty_dump {
677 0 ▶ my $self = shift;
Subroutine _pretty_dump: not called CC=4 SCAR=30.0
678 0 my $label = shift;
679 0 my $data = shift;
680 0 my @dump = map {sprintf "%02X", $_ } unpack("C*", $data);
681 # format for TCP or RTU
682 0 ▶ if ($self->{MODE} == MODBUS_TCP) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'MODE'} == 1) { } 0 0 0
elsif ($self->{'MODE'} == 2) { } 0 0 0
683 0 $dump[0] = "[".$dump[0];
684 0 $dump[5] = $dump[5]."]";
685 } elsif ($self->{MODE} == MODBUS_RTU) {
686 0 $dump[$#dump-1] = "[".$dump[$#dump-1];
687 0 $dump[$#dump] = $dump[$#dump]."]";
688 }
689 # print result
690 0 print $label."\n";
691 0 for (@dump) {print $_." ";}
692 0 print "\n\n";
693 }
694  
695 2 1;
696  
697 __END__
↑ Summary Build »