blib/lib/Electronics/PSU/DPSxxxx.pm

filter: Statementstmt 100% 23 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 100% 31 / 31 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ 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.70 ':experimental(adjust_params)';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Electronics::PSU::DPSxxxx 0.03;
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 differenet 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 has $_fh :param = undef;
65 has $_addr :param = 1;
66  
67 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 sub _calc_crc ( $data )
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 my $crc = 0xFFFF;
87 foreach my $d ( split //, $data ) {
88 $crc ^= ord $d;
89 foreach ( 1 .. 8 ) {
90 if( $crc & 0x0001 ) {
91 $crc >>= 1;
92 $crc ^= 0xA001;
93 }
94 else {
95 $crc >>= 1;
96 }
97 }
98 }
99  
100 return $crc;
101 }
102  
103 async method _send_command ( $func, $format, @args )
104 {
105 my $request = pack "C C $format", $_addr, $func, @args;
106  
107 # CRC is appended in little-endian format
108 $request .= pack "S<", _calc_crc( $request );
109  
110 await Future::IO->syswrite( $_fh, $request );
111 }
112  
113 async method _recv_response ( $func, $len, $format )
114 {
115 my $response = await Future::IO->sysread_exactly( $_fh, 2 + $len + 2 );
116  
117 my $got_crc = unpack "S<", substr( $response, -2, 2, "" );
118 if( $got_crc != ( my $want_crc = _calc_crc( $response ) ) ) {
119 # Just warn for now
120 warn sprintf "Received PDU CRC %04X, expected %04X\n", $got_crc, $want_crc;
121 }
122  
123 my ( $got_addr, $got_func, $payload ) = unpack "C C a*", $response;
124 $got_addr == $_addr or croak "Received response from unexpected addr";
125 $got_func == $func or croak "Received response for unexpected function";
126  
127 return unpack $format, $payload;
128 }
129  
130 use constant {
131 FUNC_READ_HOLDING_REGISTER => 0x03,
132 FUNC_WRITE_SINGLE_REGISTER => 0x06,
133 };
134  
135 async method _read_holding_registers ( $reg, $count = 1 )
136 {
137 await $self->_send_command( FUNC_READ_HOLDING_REGISTER, "S> S>", $reg, $count );
138  
139 my ( $nbytes, @regs ) =
140 await $self->_recv_response( FUNC_READ_HOLDING_REGISTER, 1 + 2*$count, "C (S>)*" );
141  
142 return @regs;
143 }
144  
145 async method _write_single_register ( $reg, $value )
146 {
147 await $self->_send_command( FUNC_WRITE_SINGLE_REGISTER, "S> S>", $reg, $value );
148  
149 # ignore result
150 await $self->_recv_response( FUNC_WRITE_SINGLE_REGISTER, 2 + 2, "S> S>" );
151  
152 return;
153 }
154  
155 =head1 METHODS
156  
157 =cut
158  
159 use constant {
160 REG_USET => 0x00, # centivolts
161 REG_ISET => 0x01, # miliamps
162 REG_UOUT => 0x02, # centivolts (RO)
163 REG_IOUT => 0x03, # miliiamps (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 };
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 async method set_voltage ( $voltage )
184 {
185 await $self->_write_single_register( REG_USET, $voltage * 100 );
186 }
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 async method set_current ( $current )
197 {
198 await $self->_write_single_register( REG_ISET, $current * 1000 );
199 }
200  
201 my %READINGS;
202  
203 # Build up all the reading methods
204 BEGIN {
205 use Object::Pad qw( :experimental(mop) );
206  
207 my $metaclass = Object::Pad::MOP::Class->for_caller;
208  
209 %READINGS = ( # register, scale
210 output_voltage => [ REG_UOUT, sub { $_ / 100 } ],
211 output_current => [ REG_IOUT, sub { $_ / 1000 } ],
212 input_voltage => [ REG_UIN, sub { $_ / 100 } ],
213 output_protect => [ REG_PROTECT, sub { (qw( ok OVP OCP OPP ))[$_]} ],
214 output_mode => [ REG_CVCC, sub { (qw( CV CC ))[$_] } ],
215 );
216  
217 foreach my $name ( keys %READINGS ) {
218 $metaclass->add_method(
219 "read_$name" => async method { return await $self->read_multiple( $name ) }
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 async method read_multiple ( @names )
275 {
276 my ( $minreg, $maxreg );
277  
278 my @regs = map {
279 my $m = $READINGS{$_} or croak "Measurement '$_' is not recognised";
280 my $reg = $m->[0];
281 $minreg = $reg if !defined $minreg or $reg < $minreg;
282 $maxreg = $reg if !defined $maxreg or $reg > $maxreg;
283 } @names;
284  
285 my @values = await $self->_read_holding_registers( $minreg, $maxreg-$minreg+1 );
286  
287 return map {
288 my $m = $READINGS{$_};
289 $m->[1]->( local $_ = $values[$m->[0] - $minreg] );
290 } @names;
291 }
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 async method set_output_state ( $on )
302 {
303 await $self->_write_single_register( REG_ONOFF, !!$on );
304 }
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 {
316 return await $self->_read_holding_registers( REG_MODEL );
317 }
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 {
329 return await $self->_read_holding_registers( REG_VERSION );
330 }
331  
332 =head1 AUTHOR
333  
334 Paul Evans <leonerd@leonerd.org.uk>
335  
336 =cut
337  
338 0x55AA;
« Build ↑ Summary