diff --git a/example.py b/example.py index 0ac2a09..8ff8f89 100644 --- a/example.py +++ b/example.py @@ -34,7 +34,7 @@ device.voltage1 = 12 device.current1 = 1 time.sleep(10) print("...now enabling the power output control...") -device.enable_output() +device.enable_output(0) time.sleep(2) device_status_info1 = device.get_device_status_information(0) device_status_info2 = device.get_device_status_information(1) diff --git a/pyps2000b/PS2000B.py b/pyps2000b/PS2000B.py index 321bc1d..d8122a7 100644 --- a/pyps2000b/PS2000B.py +++ b/pyps2000b/PS2000B.py @@ -8,7 +8,6 @@ # - read/write remote control # - read/write output control # -# Todo: # - wrap error results in own telegram # # References @@ -21,7 +20,7 @@ import struct import sys PY_3 = sys.version_info >= (3, 0) - +# noinspection SpellCheckingInspection __author__ = "Sören Sprößig " @@ -79,7 +78,7 @@ class Objects: # noinspection PyClassHasNoInit -class ControlParameters: +class ControlParam: """Parameters for controlling the device""" SWITCH_MODE_CMD = 0x10 SWITCH_MODE_REMOTE = 0x10 @@ -149,7 +148,7 @@ class FromPowerSupply(Telegram): # noinspection PyMethodMayBeStatic def get_error(self): - # FIXME: [1] chapter 3.6 add support for error codes here + # ToDo: [1] chapter 3.6 add support for error codes here return None @@ -175,14 +174,14 @@ class DeviceInformation: self.nominal_current = 0 self.nominal_power = 0 self.manufacturer = "" - self.device_article_number = "" + self.device_art_no = "" self.software_version = "" def __str__(self): return "%s %s [%s], SW: %s, Art-Nr: %s, [%0.2f V, %0.2f A, %0.2f W]" % \ (self.manufacturer, self.device_type, self.device_serial_no, - self.software_version, self.device_article_number, + self.software_version, self.device_art_no, self.nominal_voltage, self.nominal_current, self.nominal_power) @@ -219,7 +218,7 @@ class PS2000B: def get_device_information(self): return self.__device_information - def __read_device_information(self, channel=0): + def __read_device_information(self, channel=0): # reads static device information, usually not channel dependant result = DeviceInformation() # taken from [2] @@ -228,24 +227,23 @@ class PS2000B: result.nominal_voltage = as_float(self.__read_device_data(4, Objects.NOMINAL_VOLTAGE, channel).get_data()) result.nominal_current = as_float(self.__read_device_data(4, Objects.NOMINAL_CURRENT, channel).get_data()) result.nominal_power = as_float(self.__read_device_data(4, Objects.NOMINAL_POWER, channel).get_data()) - result.device_article_number = as_string(self.__read_device_data(16, Objects.DEVICE_ARTICLE_NO, channel).get_data()) + result.device_art_no = as_string(self.__read_device_data(16, Objects.DEVICE_ARTICLE_NO, channel).get_data()) result.manufacturer = as_string(self.__read_device_data(16, Objects.MANUFACTURER, channel).get_data()) result.software_version = as_string(self.__read_device_data(16, Objects.SOFTWARE_VERSION, channel).get_data()) return result - def __read_device_data(self, expected_length, object_id, channel): + def __read_device_data(self, expected_length, object_id, channel): # reads data from device based on object_id telegram = ToPowerSupply(0b01, [channel, object_id], expected_length) result = self.__send_and_receive(telegram.get_byte_array()) return result - def __send_and_receive(self, raw_bytes): + def __send_and_receive(self, raw_bytes): # sends request for info to device and reads reply self.__serial.write(raw_bytes) result = FromPowerSupply(self.__serial.read(Constants.MAX_LEN_IN_BYTES)) return result - def get_device_status_information(self, channel=0): - """:returns DeviceStatusInformation""" + def get_device_status_information(self, channel): # gets dynamic device information (e.g. current, voltage) if channel == 0: if self.__device_status_information1 is None: self.update_device_information(0) @@ -255,11 +253,10 @@ class PS2000B: self.update_device_information(1) info = self.__device_status_information2 else: - info = None raise ValueError("Invalid Channel") return info - def update_device_information(self, channel=0): + def update_device_information(self, channel): # updates dynamic device info stored in __device_status_information telegram = ToPowerSupply(0b01, [channel, Objects.STATUS_ACTUAL_VALUES], 6) device_information = self.__send_and_receive(telegram.get_byte_array()) if channel == 0: @@ -269,43 +266,53 @@ class PS2000B: else: raise ValueError("Invalid Channel") - def __send_device_control(self, p1, p2, channel=0): + def __send_device_control(self, p1, p2, channel): # sends commands to PSU, commands given in p1, p2 telegram = ToPowerSupply(0b11, [channel, Objects.POWER_SUPPLY_CONTROL, p1, p2], 2) - _ = self.__send_and_receive(telegram.get_byte_array()) - self.update_device_information(channel) + _ = self.__send_and_receive(telegram.get_byte_array()) # send command to PSU + self.update_device_information(channel) # update info after change def __send_device_data(self, obj, data, channel): - ''' - Send integer data with obj-id to the PSU - ''' + # Send integer data with obj-id to the PSU + telegram = ToPowerSupply(0b11, [channel, obj, data >> 8, data & 0xff], 4) _ = self.__send_and_receive(telegram.get_byte_array()) self.update_device_information(channel) def enable_remote_control(self, channel): - self.__send_device_control(ControlParameters.SWITCH_MODE_CMD, ControlParameters.SWITCH_MODE_REMOTE, channel) + self.__send_device_control(ControlParam.SWITCH_MODE_CMD, ControlParam.SWITCH_MODE_REMOTE, channel) def disable_remote_control(self, channel): - self.__send_device_control(ControlParameters.SWITCH_MODE_CMD, ControlParameters.SWITCH_MODE_MANUAL, channel) + self.__send_device_control(ControlParam.SWITCH_MODE_CMD, ControlParam.SWITCH_MODE_MANUAL, channel) def enable_output(self, channel): - self.__send_device_control(ControlParameters.SWITCH_POWER_OUTPUT_CMD, ControlParameters.SWITCH_POWER_OUTPUT_ON, channel) + self.__send_device_control(ControlParam.SWITCH_POWER_OUTPUT_CMD, ControlParam.SWITCH_POWER_OUTPUT_ON, channel) def disable_output(self, channel): - self.__send_device_control(ControlParameters.SWITCH_POWER_OUTPUT_CMD, ControlParameters.SWITCH_POWER_OUTPUT_OFF, channel) + self.__send_device_control(ControlParam.SWITCH_POWER_OUTPUT_CMD, ControlParam.SWITCH_POWER_OUTPUT_OFF, channel) @property - def output1(self,): + def output1(self): # object controlling output 1 on/off return self.get_device_status_information(0).output_active @output1.setter def output1(self, value): if value: - self.enable_output() + self.enable_output(0) else: - self.disable_output() + self.disable_output(0) - def get_voltage(self, channel=0): + @property + def output2(self): # object controlling output 2 on/off + return self.get_device_status_information(1).output_active + + @output2.setter + def output2(self, value): + if value: + self.enable_output(1) + else: + self.disable_output(1) + + def get_voltage(self, channel): self.update_device_information(channel) if channel == 0: v_perc = self.__device_status_information1.actual_voltage_percent @@ -316,19 +323,18 @@ class PS2000B: voltage = self.__device_information.nominal_voltage * v_perc return voltage / 100 - def get_voltage_setpoint(self, channel=0): + def get_voltage_setpoint(self, channel): res = self.__read_device_data(2, Objects.SET_VALUE_VOLTAGE, channel).get_data() - return self.__device_information.nominal_voltage * ((res[0]<<8) + res[1]) / 25600.0 + return self.__device_information.nominal_voltage * ((res[0] << 8) + res[1]) / 25600.0 def set_voltage(self, value, channel): self.update_device_information(channel) self.enable_remote_control(channel) - volt = int(round( (value * 25600.0) / self.__device_information.nominal_voltage )) + volt = int(round((value * 25600.0) / self.__device_information.nominal_voltage)) self.__send_device_data(Objects.SET_VALUE_VOLTAGE, volt, channel) - #self.disable_remote_control() @property - def voltage1(self): + def voltage1(self): # object storing and controlling the voltage of channel 1 return self.get_voltage(0) @voltage1.setter @@ -336,14 +342,14 @@ class PS2000B: self.set_voltage(value, 0) @property - def voltage2(self): + def voltage2(self): # object storing and controlling the voltage of channel 2 return self.get_voltage(1) @voltage2.setter - def voltage2(self, value): # voltage of channel 2 + def voltage2(self, value): self.set_voltage(value, 1) - def get_current(self, channel=0): + def get_current(self, channel): self.update_device_information(channel) if channel == 0: c_perc = self.__device_status_information1.actual_current_percent @@ -354,16 +360,15 @@ class PS2000B: current = self.__device_information.nominal_current * c_perc return current / 100 - def get_current_setpoint(self, channel=0): + def get_current_setpoint(self, channel): res = self.__read_device_data(2, Objects.SET_VALUE_CURRENT, channel).get_data() return self.__device_information.nominal_current * ((res[0] << 8) + res[1]) / 25600.0 def set_current(self, value, channel): self.update_device_information(channel) self.enable_remote_control(channel) - curr = int(round( (value * 25600.0) / self.__device_information.nominal_current)) + curr = int(round((value * 25600.0) / self.__device_information.nominal_current)) self.__send_device_data(Objects.SET_VALUE_CURRENT, curr, channel) - # self.disable_remote_control() @property def current1(self): diff --git a/pyps2000b/__init__.py b/pyps2000b/__init__.py index 66047dc..85506da 100644 --- a/pyps2000b/__init__.py +++ b/pyps2000b/__init__.py @@ -1,3 +1,4 @@ #!/usr/bin/python # coding=utf-8 +# noinspection SpellCheckingInspection __author__ = "Sören Sprößig "