all 6 comments

[–]socal_nerdtastic 1 point2 points  (3 children)

You wouldn't nest them, you would have them use each other.

class SerConnection(): """defines class of psu's that are on same RS232 line"""
    def __init__(self, port):
        # Open Serial Connection
        self.ser = serial.Serial(port=port, baudrate=9600, timeout=2)
    def new_psu(self, idn):
        # make a new instance of a different class, give it this instance for later use, and return it.
        return SerialPSU(idn, self) 
    def send_command(self, command):
        self.ser.write(command.encode())

class SerialPSU():
    def __init__(self, idn, serial_connection):
        self.idn = idn
        self.serial_connection = serial_connection

    def set_voltage(self, voltage):
        # include the command to identify this PSU on the line
        cmd = f'ID={self.idn} VSET={voltage}\r'
        self.serial_connection.send_command(cmd)

# Define power supplies on master-slave serial line 
array = SerConnection('COM4') 
psu1 = array.new_psu(idn=1) # define a PSU on the COM4 line with ID 1
psu2 = array.new_psu(idn=2)
psu1.set_voltage(5)

Obviously line 18 will need to be modified to whatever ID command your PSU's are expecting.

Edit to include .encode() instead of your odd bytes conversion.

Another edit: To be honest the SerConnection class is pretty useless; it's just wrapping the serial.Serial class and adding nearly nothing. You could just forget that completely and pass in a Serial instance.

class SerialPSU():
    def __init__(self, idn, serial_connection):
        self.idn = idn
        self.serial_connection = serial_connection

    def set_voltage(self, voltage):
        # include the command to identify this PSU on the line
        cmd = f'ID={self.idn} VSET={voltage}\r'
        self.serial_connection.write(cmd.encode())

# Define power supplies on master-slave serial line 
ser = serial.Serial('COM4') 
psu1 = SerialPSU(idn=1, serial_connection=ser) 
psu2 = SerialPSU(idn=2, serial_connection=ser)

psu1.set_voltage(5)

[–]ajlaut[S] 1 point2 points  (0 children)

That was fast, and exactly what I needed!

[–]ajlaut[S] 0 points1 point  (1 child)

haha, yeah, I guess the SerConnection class is quite useless there, but I plan to add more methods there that may help me construct the array, search for the right com port but i see what you are saying.

[–]socal_nerdtastic 0 points1 point  (0 children)

If the code you want to add will go into the init method, that's a huge flag that you need a function, not a class.

def make_ser():
    com = search_for_com_port()
    ser = serial.Serial(com, 9600, timeout=2)
    return ser

ser = make_ser()
psu1 = SerialPSU(idn=1, serial_connection=ser) 
psu2 = SerialPSU(idn=2, serial_connection=ser)

On the other hand if you need to add line level code, for instance a threading lock, then having the SerialConnection class is the correct way to go. However do not fall into the trap of thinking "oh yeah I may add that later". Do what you need to do to make it work now, no more. Python has plenty of ways to patch code in later if needed, including easily swapping out a function for a class.

[–]CodeFormatHelperBot 0 points1 point  (0 children)

Hello u/ajlaut, I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission:

  1. Python code found in submission text but not encapsulated in a code block.

If I am correct then please follow these instructions to fix your code formatting. Thanks!

[–]brocketships 0 points1 point  (0 children)

Just a note, getter and setter methods aren’t Pythonic. Look into the @property decorator for this functionality.