SDS1202XE.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import pyvisa
  2. import os
  3. import re
  4. from PIL import Image as PILImage
  5. from IPython.display import Image
  6. from retry import retry
  7. DEBUG = True
  8. # https://siglentna.com/wp-content/uploads/dlm_uploads/2017/10/ProgrammingGuide_forSDS-1-1.pdf
  9. def get_resource_manager():
  10. return pyvisa.ResourceManager()
  11. def list_resources(rm):
  12. l = rm.list_resources()
  13. if len(l) > 0:
  14. print(l)
  15. else:
  16. raise Exception("No VISA device detected.")
  17. def connect(rm, resource):
  18. sds = rm.open_resource(resource)
  19. print(sds.query("*IDN?"))
  20. return sds
  21. IMG_PATH = "./img"
  22. def screenshot(sds, name):
  23. bmpfile = os.path.join(IMG_PATH, name + ".bmp")
  24. pngfile = os.path.join(IMG_PATH, name + ".png")
  25. sds.write("SCDP")
  26. res = sds.read_raw()
  27. with open(bmpfile, 'wb') as f:
  28. f.write(res)
  29. # convert to png
  30. PILImage.open(bmpfile).save(pngfile)
  31. os.remove(bmpfile)
  32. return Image(filename=pngfile)
  33. #def get_frequency(sds):
  34. # res = sds.query("CYMOMETER?")
  35. # print("{}".format(res))
  36. def get_vertical(sds, channel):
  37. cmd = "C{}:Volt_DIV?".format(channel)
  38. res = sds.query(cmd)
  39. print("{}".format(res))
  40. def set_vertical(sds, channel, volt_by_div):
  41. cmd = "C{}:VDIV {}".format(channel, volt_by_div)
  42. sds.write(cmd)
  43. def get_tdiv(sds):
  44. print(sds.query("TDIV?"))
  45. def set_tdiv(sds, tdiv):
  46. cmd = "TDIV {}".format(tdiv)
  47. sds.write(cmd)
  48. def autosetup(sds):
  49. sds.write("ASET")
  50. def set_phase_measure(sds):
  51. sds.write("MEAD PHA,C1-C2")
  52. @retry(Exception)
  53. def get_phase(sds):
  54. res = sds.query("C1-C2:MEAD? PHA")
  55. # C1-C2:MEAD PHA,-75.72degree
  56. match = re.search(r"PHA,(.*?)degree", res)
  57. if match:
  58. return float(match.group(1))
  59. else:
  60. raise Exception("Can't read the phase in {}".format(res))
  61. def set_measure(sds, channel):
  62. sds.write("PACU PKPK,C{}".format(channel))
  63. sds.write("PACU FREQ,C{}".format(channel))
  64. @retry(Exception)
  65. def get_measure_vpp(sds, channel):
  66. res = sds.query("C{}:PAVA? PKPK".format(channel))
  67. # C1:PAVA PKPK,9.04E+00V
  68. match = re.search(r"PKPK,(.*?)V", res)
  69. if match:
  70. return float(match.group(1))
  71. else:
  72. raise Exception("Can't read Peak to Peak value in {}".format(res))
  73. @retry(Exception)
  74. def get_measure_freq(sds, channel):
  75. res = sds.query("C{}:PAVA? FREQ".format(channel))
  76. print("{}".format(res))