pyserial with USB task

Viewed 259

I need help. Objective: For instance, based on energy values, send a message to the user about high consumption based on temperature from the sensors (If temperature low, send message to the user, the heater is on

I have comment on the code from my teacher, but I can't understand how is it work, how is it writing

import serial
import time




def connect(port):
    """ Opens the serial port.\n
        Arguments:  port ('/dev/usb0', 'COM11').
    """
    source = serial.Serial(port, 9600, 8, parity='N', timeout=1)
    return source



def idn(port):
    """ Queries the Power Supply IDN. Returns the instrument identification as a string.\n
        Arguments: port number (integer).\n
        Command *IDN??
    """
    #com = 'COM' + str(int(com))
    power_source = connect(port)
    power_source.write(b'*idn?\r\n')
    id = power_source.readline().decode("utf-8")
    print('\nIDN: ', id)
    return()


def settings(port):
    """ Queries the Power Supply settings (Voltage, Current, Output). Returns the settings for OUTPUT1.\n
        Arguments: COM port number (integer).\n
        V<N>?\n
        I<N>?\n
        OP<N>?
    """
    #com = 'COM' + str(int(com))
    power_source = connect(port)
    power_source.write(b'V1?\r\n')
    voltage = power_source.readline().decode("utf-8")
    print('Voltage [V] =', voltage)
    power_source.write(b'I1?\r\n')
    current = power_source.readline().decode("utf-8")
    print('Current [A]=', current)
    power_source.write(b'OP1?\r\n')
    output = power_source.readline().decode("utf-8")
    print('Status Output1 =', output)
    return()



def setup(port):
    """ Sets the Power Supply settings (Voltage, Current). Returns the settings for OUTPUT1.
        Arguments: COM port number (integer).
        V<N> <NRF>
        I<N> <NRF>
    """
    #com = 'COM' + str(int(com))
    power_source = connect(port)
    power_source.write(b'V1 24\r\n')
    power_source.write(b'I1 0.2\r\n')
    return()


def output(port, output):
    """ Sets the OUTPUT1 on/off.
        Arguments: COM port (integer), output (integer, 0 for off, 1 for on)
        command OP<N> <NRF>
    """
    #com = 'COM' + str(int(com))
    power_source = connect(port)
    command = 'OP1 ' + str(output) + '\r\n'
    S = str.encode(command)
    power_source.write(S)
    return()

# Request IDN
port = '/dev/ttyACM1'
idn(port)


# Request voltage setup in the unit
settings(port)

# Set up
setup(port)


# Set up the unit on/off
# output(11, 0)
# settings(11)
#print('All OK')
0 Answers
Related