123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- 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
|