serialCommands.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import time
  2. import serial
  3. class SerialCommands:
  4. def __init__(self):
  5. self.ser = serial.Serial()
  6. def setConnectionParameters(self, port):
  7. print("con: set connection parameters")
  8. self.ser.port = port
  9. self.ser.baudrate = 115200
  10. self.ser.parity = serial.PARITY_NONE
  11. self.ser.stopbits = serial.STOPBITS_ONE
  12. self.ser.bytesize = serial.EIGHTBITS
  13. self.ser.timeout = None
  14. def setAndOpenConnection(self, port):
  15. print("con: set connection parameters")
  16. self.ser.port = port
  17. self.ser.baudrate = 115200
  18. self.ser.parity = serial.PARITY_NONE
  19. self.ser.stopbits = serial.STOPBITS_ONE
  20. self.ser.bytesize = serial.EIGHTBITS
  21. self.ser.timeout = None
  22. try:
  23. self.ser.open()
  24. print("con: connection was successful")
  25. except serial.serialutil.SerialException as e:
  26. print(e)
  27. def openConnection(self):
  28. try:
  29. self.ser.open()
  30. print("con: connection was successful")
  31. except serial.serialutil.SerialException as e:
  32. print(e)
  33. def closeConnection(self):
  34. try:
  35. self.ser.close()
  36. print("con: disconnection was successful")
  37. except serial.serialutil.SerialException as e:
  38. print(e)
  39. def setVoltageMode(self, mode):
  40. try:
  41. self.ser.write(bytearray((":mode " + str(mode) + "\r\n").encode()))
  42. print("ser: :mode " + str(mode))
  43. except serial.serialutil.SerialException as e:
  44. print(e)
  45. def setVoltageRange(self, range):
  46. try:
  47. self.ser.write(bytearray((":voltage:range " + str(range) + "\r\n").encode()))
  48. print("ser: :voltage:range " + str(range))
  49. except serial.serialutil.SerialException as e:
  50. print(e)
  51. def setVoltageDC(self, volts):
  52. try:
  53. self.ser.write(bytearray((":voltage:offset " + str(volts) + "\r\n").encode()))
  54. print("ser: :voltage:offset " + str(volts))
  55. except serial.serialutil.SerialException as e:
  56. print(e)
  57. def setVoltageAC(self, volts):
  58. try:
  59. self.ser.write(bytearray((":voltage " + str(volts) + "\r\n").encode()))
  60. print("ser: :voltage " + str(volts))
  61. except serial.serialutil.SerialException as e:
  62. print(e)
  63. def setVoltageSQU(self, volts):
  64. volts = str(int(volts)/2)
  65. try:
  66. self.ser.write(bytearray((":voltage:offset " + str(volts) + "\r\n").encode()))
  67. self.ser.write(bytearray((":voltage " + str(volts) + "\r\n").encode()))
  68. print("ser: :voltage " + str(volts))
  69. except serial.serialutil.SerialException as e:
  70. print(e)
  71. def setMaxCurrent(self, ampers):
  72. try:
  73. self.ser.write(bytearray((":current:limit:rms " + str(ampers) + "\r\n").encode()))
  74. print("ser: :current:limit:rms " + str(ampers))
  75. except serial.serialutil.SerialException as e:
  76. print(e)
  77. def setFrequency(self, hertz):
  78. try:
  79. self.ser.write(bytearray((":frequency " + str(hertz) + "\r\n").encode()))
  80. print("ser: :frequency " + str(hertz))
  81. except serial.serialutil.SerialException as e:
  82. print(e)
  83. def setFunctionSIN(self):
  84. try:
  85. self.ser.write(bytearray(":function 16\r\n".encode()))
  86. print("ser: :function 16 (SIN)")
  87. except serial.serialutil.SerialException as e:
  88. print(e)
  89. def setFunctionSQU(self):
  90. try:
  91. self.ser.write(bytearray(":function 17\r\n".encode()))
  92. print("ser: :function 17 (SQU)")
  93. except serial.serialutil.SerialException as e:
  94. print(e)
  95. def setOutputOn(self):
  96. try:
  97. self.ser.write(bytearray(":output 1\r\n".encode()))
  98. print("ser: :output 1")
  99. except serial.serialutil.SerialException as e:
  100. print(e)
  101. def setOutputOff(self):
  102. try:
  103. self.ser.write(bytearray(":output 0\r\n".encode()))
  104. print("ser: :output 0")
  105. except serial.serialutil.SerialException as e:
  106. print(e)
  107. def measureVoltage(self):
  108. out = ''
  109. try:
  110. self.ser.write(bytearray(":measure:voltage?\r\n".encode()))
  111. time.sleep(0.1)
  112. out = self.ser.readline()
  113. except serial.serialutil.SerialException as e:
  114. print(e)
  115. return out
  116. def measureCurrent(self):
  117. out = ''
  118. try:
  119. self.ser.write(bytearray(":measure:current?\r\n".encode()))
  120. time.sleep(0.1)
  121. out = self.ser.readline()
  122. except serial.serialutil.SerialException as e:
  123. print(e)
  124. return out