import time import serial class SerialCommands: def __init__(self): self.ser = serial.Serial() def setConnectionParameters(self, port): print("con: set connection parameters") self.ser.port = port self.ser.baudrate = 115200 self.ser.parity = serial.PARITY_NONE self.ser.stopbits = serial.STOPBITS_ONE self.ser.bytesize = serial.EIGHTBITS self.ser.timeout = None def setAndOpenConnection(self, port): print("con: set connection parameters") self.ser.port = port self.ser.baudrate = 115200 self.ser.parity = serial.PARITY_NONE self.ser.stopbits = serial.STOPBITS_ONE self.ser.bytesize = serial.EIGHTBITS self.ser.timeout = None try: self.ser.open() print("con: connection was successful") except serial.serialutil.SerialException as e: print(e) def openConnection(self): try: self.ser.open() print("con: connection was successful") except serial.serialutil.SerialException as e: print(e) def closeConnection(self): try: self.ser.close() print("con: disconnection was successful") except serial.serialutil.SerialException as e: print(e) def setVoltageMode(self, mode): try: self.ser.write(bytearray((":mode " + str(mode) + "\r\n").encode())) print("ser: :mode " + str(mode)) except serial.serialutil.SerialException as e: print(e) def setVoltageRange(self, range): try: self.ser.write(bytearray((":voltage:range " + str(range) + "\r\n").encode())) print("ser: :voltage:range " + str(range)) except serial.serialutil.SerialException as e: print(e) def setVoltageDC(self, volts): try: self.ser.write(bytearray((":voltage:offset " + str(volts) + "\r\n").encode())) print("ser: :voltage:offset " + str(volts)) except serial.serialutil.SerialException as e: print(e) def setVoltageAC(self, volts): try: self.ser.write(bytearray((":voltage " + str(volts) + "\r\n").encode())) print("ser: :voltage " + str(volts)) except serial.serialutil.SerialException as e: print(e) def setVoltageSQU(self, volts): volts = str(int(volts)/2) try: self.ser.write(bytearray((":voltage:offset " + str(volts) + "\r\n").encode())) self.ser.write(bytearray((":voltage " + str(volts) + "\r\n").encode())) print("ser: :voltage " + str(volts)) except serial.serialutil.SerialException as e: print(e) def setMaxCurrent(self, ampers): try: self.ser.write(bytearray((":current:limit:rms " + str(ampers) + "\r\n").encode())) print("ser: :current:limit:rms " + str(ampers)) except serial.serialutil.SerialException as e: print(e) def setFrequency(self, hertz): try: self.ser.write(bytearray((":frequency " + str(hertz) + "\r\n").encode())) print("ser: :frequency " + str(hertz)) except serial.serialutil.SerialException as e: print(e) def setFunctionSIN(self): try: self.ser.write(bytearray(":function 16\r\n".encode())) print("ser: :function 16 (SIN)") except serial.serialutil.SerialException as e: print(e) def setFunctionSQU(self): try: self.ser.write(bytearray(":function 17\r\n".encode())) print("ser: :function 17 (SQU)") except serial.serialutil.SerialException as e: print(e) def setOutputOn(self): try: self.ser.write(bytearray(":output 1\r\n".encode())) print("ser: :output 1") except serial.serialutil.SerialException as e: print(e) def setOutputOff(self): try: self.ser.write(bytearray(":output 0\r\n".encode())) print("ser: :output 0") except serial.serialutil.SerialException as e: print(e) def measureVoltage(self): out = '' try: self.ser.write(bytearray(":measure:voltage?\r\n".encode())) time.sleep(0.1) out = self.ser.readline() except serial.serialutil.SerialException as e: print(e) return out def measureCurrent(self): out = '' try: self.ser.write(bytearray(":measure:current?\r\n".encode())) time.sleep(0.1) out = self.ser.readline() except serial.serialutil.SerialException as e: print(e) return out