blib/lib/Electronics/PSU/DPSxxxx.pm

filter: Statementstmt 97.3% 146 / 150 Branchbran 64.2% 9 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 93.3% 28 / 30 Podpod 100% 8 / 8 totaltotal 92.4% 196 / 212 CC 14 SCAR 8.8
CC
14
Cov
94%
read_multiple
6.1
_recv_response
4.1
_calc_crc
4.0
CRAP 2.4
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2022 -- leonerd@leonerd.org.uk
5  
6 2 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Object::Pad 0.73 ':experimental(adjust_params init_expr)';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Electronics::PSU::DPSxxxx 0.04;
12 class Electronics::PSU::DPSxxxx;
13  
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Fcntl qw( O_NOCTTY O_NDELAY );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use IO::Termios;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Protocol::Modbus exists but it doesn't do async, and it doesn't do proper
22 # frame detection of incoming data :(
23 # We'll just write this all ourselves; it's not hard
24  
25 # See also
26 # https://autoit.de/wcf/attachment/88980-dps3005-cnc-communication-protocol-v1-2-pdf/
27  
28 =head1 NAME
29  
30 C<Electronics::PSU::DPSxxxx> - control a F<DPS> power supply
31  
32 =head1 SYNOPSIS
33  
34 use Future::AsyncAwait;
35  
36 use Electronics::PSU::DPSxxxx;
37  
38 my $psu = Electronics::PSU::DPSxxxx->new( dev => "/dev/ttyUSB0" );
39  
40 await $psu->set_voltage( 1.23 ); # volts
41 await $psu->set_current( 0.200 ); # amps
42  
43 await $psu->set_output_state( 1 ); # turn it on!
44  
45 =head1 DESCRIPTION
46  
47 This module allows control of a F<RDTech> F<DPS>-series power supply, such as
48 the F<DPS3005>, when connected over a serial port.
49  
50 =head2 Interface Design
51  
52 The interface is currently an ad-hoc collection of whatever seems to work
53 here, but my hope is to find a more generic shareable interface that multiple
54 different modules can use, to provide interfaces to various kinds of
55 electronics test equipment.
56  
57 The intention is that it should eventually be possible to write a script for
58 performing automated electronics testing or experimentation, and easily swap
59 out modules to suit the equipment available. Similar concepts apply in fields
60 like L<DBI>, or L<Device::Chip>, so there should be plenty of ideas to borrow.
61  
62 =cut
63  
64 field $_fh :param = undef;
65 2 field $_addr :param = 1;
66  
67 2 ADJUST :params (
68 :$dev = undef,
69 ) {
70 unless( $_fh ) {
71 $_fh = IO::Termios->open( $dev, "9600,8,n,1", O_NOCTTY, O_NDELAY ) or
72 croak "Cannot open $dev - $!";
73  
74 $_fh->cfmakeraw;
75 $_fh->setflag_clocal( 1 );
76 $_fh->blocking( 1 );
77 $_fh->autoflush;
78 }
79 }
80  
81 2 ▶sub _calc_crc ( $data )
Subroutine _calc_crc: called CC=4 SCAR=13.9
82 {
83 # This is not the standard CRC16 algorithm
84 # Stolen and adapted from
85 # https://ctlsys.com/support/how_to_compute_the_modbus_rtu_message_crc/
86 20 my $crc = 0xFFFF;
87 20 foreach my $d ( split //, $data ) {
88 115 $crc ^= ord $d;
89 115 foreach ( 1 .. 8 ) {
90 920 ▶ if( $crc & 0x0001 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crc & 1) { } 427 493 920
91 427 $crc >>= 1;
92 427 $crc ^= 0xA001;
93 }
94 else {
95 493 $crc >>= 1;
96 }
97 }
98 }
99  
100 20 return $crc;
101 }
102  
103 10 ▶async method _send_command ( $func, $format, @args )
Subroutine _send_command: called CC=1 SCAR=0.0
104 10 {
105 10 my $request = pack "C C $format", $_addr, $func, @args;
106  
107 # CRC is appended in little-endian format
108 10 $request .= pack "S<", _calc_crc( $request );
109  
110 10 await Future::IO->syswrite( $_fh, $request );
111 2 }
112  
113 10 ▶async method _recv_response ( $func, $len, $format )
Subroutine _recv_response: called CC=4 SCAR=14.1
114 10 {
115 10 my $response = await Future::IO->sysread_exactly( $_fh, 2 + $len + 2 );
116  
117 10 my $got_crc = unpack "S<", substr( $response, -2, 2, "" );
118 10 ▶ if( $got_crc != ( my $want_crc = _calc_crc( $response ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_crc != (my $want_crc = _calc_crc($response))) 0 10 10
119 # Just warn for now
120 0 warn sprintf "Received PDU CRC %04X, expected %04X\n", $got_crc, $want_crc;
121 }
122  
123 10 my ( $got_addr, $got_func, $payload ) = unpack "C C a*", $response;
124 10 ▶ $got_addr == $_addr or croak "Received response from unexpected addr";
Branch: 1 branch
BranchTrueFalseTotal
$got_addr == $_addr or croak("Received response from unexpected addr") 10 0 10
125 10 ▶ $got_func == $func or croak "Received response for unexpected function";
Branch: 1 branch
BranchTrueFalseTotal
$got_func == $func or croak("Received response for unexpected function") 10 0 10
126  
127 10 return unpack $format, $payload;
128 2 }
129  
130 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
131 2 FUNC_READ_HOLDING_REGISTER => 0x03,
132 FUNC_WRITE_SINGLE_REGISTER => 0x06,
133 2 };
134  
135 7 ▶async method _read_holding_registers ( $reg, $count = 1 )
Subroutine _read_holding_registers: called CC=2 SCAR=6.9
136 7 {
137 7 await $self->_send_command( FUNC_READ_HOLDING_REGISTER, "S> S>", $reg, $count );
138  
139 7 my ( $nbytes, @regs ) =
140 await $self->_recv_response( FUNC_READ_HOLDING_REGISTER, 1 + 2*$count, "C (S>)*" );
141  
142 7 return @regs;
143 2 }
144  
145 3 ▶async method _write_single_register ( $reg, $value )
Subroutine _write_single_register: called CC=1 SCAR=0.0
146 3 {
147 3 await $self->_send_command( FUNC_WRITE_SINGLE_REGISTER, "S> S>", $reg, $value );
148  
149 # ignore result
150 3 await $self->_recv_response( FUNC_WRITE_SINGLE_REGISTER, 2 + 2, "S> S>" );
151  
152 3 return;
153 2 }
154  
155 =head1 METHODS
156  
157 =cut
158  
159 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
160 2 REG_USET => 0x00, # centivolts
161 REG_ISET => 0x01, # miliamps
162 REG_UOUT => 0x02, # centivolts (RO)
163 REG_IOUT => 0x03, # miliamps (RO)
164 REG_POWER => 0x04, # (RO)
165 REG_UIN => 0x05, # centivolts (RO)
166 REG_LOCK => 0x06, # 0=off 1=on
167 REG_PROTECT => 0x07, # 0=ok, 1=OVP 2=OCP 3=OPP
168 REG_CVCC => 0x08, # 0=CV 1=CC
169 REG_ONOFF => 0x09, # 0=off 1=on
170 REG_B_LED => 0x0A,
171 REG_MODEL => 0x0B, # (RO)
172 REG_VERSION => 0x0C, # (RO)
173 2 };
174  
175 =head2 set_voltage
176  
177 await $psu->set_voltage( $volts );
178  
179 Sets the output voltage, in volts.
180  
181 =cut
182  
183 1 ▶async method set_voltage ( $voltage )
Subroutine set_voltage: called CC=1 SCAR=0.0
Pod documented
184 1 {
185 1 await $self->_write_single_register( REG_USET, $voltage * 100 );
186 2 }
187  
188 =head2 set_current
189  
190 await $psu->set_current( $amps );
191  
192 Sets the output current, in amps.
193  
194 =cut
195  
196 1 ▶async method set_current ( $current )
Subroutine set_current: called CC=1 SCAR=0.0
Pod documented
197 1 {
198 1 await $self->_write_single_register( REG_ISET, $current * 1000 );
199 2 }
200  
201 2 my %READINGS;
202  
203 # Build up all the reading methods
204 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
205 2 ▶ use Object::Pad qw( :experimental(mop) );
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 2 my $metaclass = Object::Pad::MOP::Class->for_caller;
208  
209 %READINGS = ( # register, scale
210 2 ▶ output_voltage => [ REG_UOUT, sub { $_ / 100 } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
211 2 ▶ output_current => [ REG_IOUT, sub { $_ / 1000 } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
212 1 ▶ input_voltage => [ REG_UIN, sub { $_ / 100 } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
213 0 ▶ output_protect => [ REG_PROTECT, sub { (qw( ok OVP OCP OPP ))[$_]} ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
214 2 ▶ output_mode => [ REG_CVCC, sub { (qw( CV CC ))[$_] } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
215 );
216  
217 2 foreach my $name ( keys %READINGS ) {
218 $metaclass->add_method(
219 0 ▶ "read_$name" => async method { return await $self->read_multiple( $name ) }
Subroutine read_input_voltage: not called CC=1 SCAR=0.6
Subroutine read_output_voltage: called CC=1 SCAR=0.6
Pod documented
Pod documented
220 );
221 }
222 }
223  
224 =head2 read_output_voltage
225  
226 $volts = await $psu->read_output_voltage;
227  
228 Returns the measured voltage at the output terminals, in volts.
229  
230 =head2 read_output_current
231  
232 $amps = await $psu->read_output_current;
233  
234 Returns the measured current at the output terminals, in amps.
235  
236 =head2 read_input_voltage
237  
238 $volts = await $psu->read_input_voltage;
239  
240 Returns the input voltage to the PSU module, in volts.
241  
242 =head2 read_output_protect
243  
244 $protect = await $psu->read_output_protect;
245  
246 Returns the output protection state as a string, either C<"ok"> if protection
247 has not been triggered, or one of C<"OVP">, C<"OCP"> or C<"OPP"> if any of the
248 protection mechanisms have been triggered.
249  
250 =head2 read_output_mode
251  
252 $mode = await $psu->read_output_mode;
253  
254 Returns the output mode, as a string either C<"CV"> for constant-voltage or
255 C<"CC"> for constant-current.
256  
257 =cut
258  
259 =head2 read_multiple
260  
261 @readings = await $psu->read_multiple( @names )
262  
263 Returns multiple measurements in a single query. This is faster than
264 performing several individual read requests. C<@names> should be a list of
265 string names, taken from the C<read_...> method names. For example:
266  
267 my ( $volts, $amps ) =
268 await $psu->read_multiple(qw( output_voltage output_current ));
269  
270 Results are returned in the same order as the requested names.
271  
272 =cut
273  
274 5 ▶async method read_multiple ( @names )
Subroutine read_multiple: called CC=6 SCAR=18.1
Pod documented
275 5 {
276 5 my ( $minreg, $maxreg );
277  
278 my @regs = map {
279 5 ▶ my $m = $READINGS{$_} or croak "Measurement '$_' is not recognised";
Branch: 1 branch
BranchTrueFalseTotal
my $m = $READINGS{$_} or croak("Measurement '${_}' is not recognised") 6 0 6
280 6 my $reg = $m->[0];
281 6 ▶ $minreg = $reg if !defined $minreg or $reg < $minreg;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $minreg or $reg < $minreg 5 1 6
Condition66%2/3
not defined $minreg or $reg < $minreg
l!l&&r!l&&!r
501
MC/DC50%1/2
not defined $minreg or $reg < $minreg
not defined $minreg $reg < $minreg
Truth table66%2/3
not(A) or B
A= not defined $minregB= $reg < $minreg
ABresult
1X1
011
000
282 6 ▶ $maxreg = $reg if !defined $maxreg or $reg > $maxreg;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $maxreg or $reg > $maxreg 6 0 6
Condition66%2/3
not defined $maxreg or $reg > $maxreg
l!l&&r!l&&!r
510
MC/DC0%0/2
not defined $maxreg or $reg > $maxreg
not defined $maxreg $reg > $maxreg
Truth table66%2/3
not(A) or B
A= not defined $maxregB= $reg > $maxreg
ABresult
1X1
011
000
283 } @names;
284  
285 5 my @values = await $self->_read_holding_registers( $minreg, $maxreg-$minreg+1 );
286  
287 return map {
288 5 my $m = $READINGS{$_};
289 6 $m->[1]->( local $_ = $values[$m->[0] - $minreg] );
290 } @names;
291 2 }
292  
293 =head2 set_output_state
294  
295 await $psu->set_output_state( $on );
296  
297 Switches output on / off.
298  
299 =cut
300  
301 1 ▶async method set_output_state ( $on )
Subroutine set_output_state: called CC=1 SCAR=0.0
Pod documented
302 1 {
303 1 await $self->_write_single_register( REG_ONOFF, !!$on );
304 2 }
305  
306 =head2 read_model
307  
308 $model = await $psu->read_model;
309  
310 Returns the model number (e.g. 3005 for F<DPS3005>).
311  
312 =cut
313  
314 async method read_model
315 1 ▶{
Subroutine read_model: called CC=1 SCAR=0.0
Pod documented
316 1 return await $self->_read_holding_registers( REG_MODEL );
317 2 }
318  
319 =head2 read_version
320  
321 $version = await $psu->read_version;
322  
323 Returns firmware version as an integer.
324  
325 =cut
326  
327 async method read_version
328 1 ▶{
Subroutine read_version: called CC=1 SCAR=0.0
Pod documented
329 1 return await $self->_read_holding_registers( REG_VERSION );
330 2 }
331  
332 =head1 AUTHOR
333  
334 Paul Evans <leonerd@leonerd.org.uk>
335  
336 =cut
337  
338 2 0x55AA;
↑ Summary