JDS6600.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import serial.tools.list_ports
  2. import serial
  3. # https://joy-it.net/files/files/Produkte/JT-JD6600/JT-JDS6600-Communication-protocol.pdf
  4. DEBUG = False
  5. def list_com_ports():
  6. if len(serial.tools.list_ports.comports()) > 0:
  7. for port in serial.tools.list_ports.comports():
  8. print("{}".format(port))
  9. else:
  10. raise Exception("No serial port detected.")
  11. def connect(port):
  12. return serial.Serial(
  13. port=port, baudrate=115200, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE
  14. )
  15. SINE = 0
  16. SQUARE = 1
  17. PULSE = 2
  18. TRIANGULAR = 3
  19. def set_waveform(serialPort, channel, wave):
  20. if channel == 1:
  21. cmd = b'w21'
  22. else:
  23. cmd = b'w22'
  24. # :w21=0.\r\n sine wave channel 1
  25. line = b':' + cmd + b'=' + str(wave).encode() + b'.\r\n'
  26. if DEBUG:
  27. print("Sending: {}".format(line))
  28. serialPort.write(line)
  29. res = serialPort.readline()
  30. if res != b':ok\r\n':
  31. raise Exception("Can't set waveform {}".format(res))
  32. # frequency in Hz
  33. def set_frequency(serialPort, channel, frequency):
  34. if channel == 1:
  35. cmd = b'w23'
  36. else:
  37. cmd = b'w24'
  38. # :w23=500000,0.\r\n 5KHz channel 1
  39. line = b':' + cmd + b'=' + str(frequency*100).encode() + b',0.\r\n'
  40. if DEBUG:
  41. print("Sending: {}".format(line))
  42. serialPort.write(line)
  43. res = serialPort.readline()
  44. if res != b':ok\r\n':
  45. raise Exception("Can't set frequency {}".format(res))
  46. # amplitude in V
  47. def set_amplitude(serialPort, channel, amplitude):
  48. if channel == 1:
  49. cmd = b'w25'
  50. else:
  51. cmd = b'w26'
  52. # :w25=1000.\r\n 1Vpp channel 1
  53. line = b':' + cmd + b'=' + str(amplitude*1000).encode() + b'.\r\n'
  54. if DEBUG:
  55. print("Sending: {}".format(line))
  56. serialPort.write(line)
  57. res = serialPort.readline()
  58. if res != b':ok\r\n':
  59. raise Exception("Can't set amplitude {}".format(res))
  60. # offset in V
  61. # TODO not sure about the calculation!
  62. def set_offset(serialPort, channel, offset):
  63. if channel == 1:
  64. cmd = b'w27'
  65. else:
  66. cmd = b'w28'
  67. # :w27=1000.\r\n 0Vpp channel 1
  68. line = b':' + cmd + b'=' + str(offset*100+1000).encode() + b'.\r\n'
  69. if DEBUG:
  70. print("Sending: {}".format(line))
  71. serialPort.write(line)
  72. res = serialPort.readline()
  73. if res != b':ok\r\n':
  74. raise Exception("Can't set amplitude {}".format(res))
  75. def set_output(serialPort, channel1=0, channel2=0):
  76. line = b':w20=' + str(channel1).encode() + b',' + str(channel2).encode() + b'r\n'
  77. if DEBUG:
  78. print("Sending: {}".format(line))
  79. serialPort.write(line)
  80. res = serialPort.readline()
  81. if res != b':ok\r\n':
  82. raise Exception("Can't set output state {}".format(res))