Skip to content

Commit be2ab0a

Browse files
committed
Add basic error handling to ppk2 API
1 parent c2f8e19 commit be2ab0a

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/ppk2_api.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import serial
88
import time
99
import struct
10+
import logging
1011

1112

1213
class PPK2_Command():
@@ -45,6 +46,7 @@ class PPK2_Modes():
4546
class PPK2_API():
4647
def __init__(self, port):
4748

49+
self.ser = None
4850
self.ser = serial.Serial(port)
4951
self.ser.baudrate = 9600
5052

@@ -79,16 +81,23 @@ def __init__(self, port):
7981

8082
def __del__(self):
8183
"""Destructor"""
82-
self.ser.close()
84+
try:
85+
if self.ser:
86+
self.ser.close()
87+
except Exception as e:
88+
logging.error(e)
8389

8490
def _pack_struct(self, cmd_tuple):
8591
"""Returns packed struct"""
8692
return struct.pack("B" * len(cmd_tuple), *cmd_tuple)
8793

8894
def _write_serial(self, cmd_tuple):
8995
"""Writes cmd bytes to serial"""
90-
cmd_packed = self._pack_struct(cmd_tuple)
91-
self.ser.write(cmd_packed)
96+
try:
97+
cmd_packed = self._pack_struct(cmd_tuple)
98+
self.ser.write(cmd_packed)
99+
except Exception as e:
100+
logging.error(e)
92101

93102
def _twos_comp(self, val):
94103
"""Compute the 2's complement of int32 value"""
@@ -212,7 +221,7 @@ def stop_measuring(self):
212221
self._write_serial((PPK2_Command.AVERAGE_STOP, ))
213222

214223
def set_source_voltage(self, mV):
215-
"""Inits device - based on observation only REGULATOR_SET is the command.
224+
"""Inits device - based on observation only REGULATOR_SET is the command.
216225
The other two values correspond to the voltage level.
217226
218227
800mV is the lowest setting - [3,32] - the values then increase linearly
@@ -223,13 +232,16 @@ def set_source_voltage(self, mV):
223232

224233
def toggle_DUT_power(self, state):
225234
"""Toggle DUT power based on parameter"""
226-
if state == "ON":
227-
self._write_serial(
228-
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.TRIGGER_SET)) # 12,1
235+
try:
236+
if state == "ON":
237+
self._write_serial(
238+
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.TRIGGER_SET)) # 12,1
229239

230-
if state == "OFF":
231-
self._write_serial(
232-
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.NO_OP)) # 12,0
240+
if state == "OFF":
241+
self._write_serial(
242+
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.NO_OP)) # 12,0
243+
except:
244+
pass
233245

234246
def use_ampere_meter(self):
235247
"""Configure device to use ampere meter"""

0 commit comments

Comments
 (0)