Capture the display of a Rigol DS1000Z series oscilloscope by LAN using LXI SCPI commands
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

OscScreenGrabLAN.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. __author__ = 'RoGeorge'
  2. #
  3. # TODO: Add command line parameters for IP, file path and file format
  4. # TODO: Add data capture/export capabilities
  5. # TODO: Add GUI
  6. # TODO: Add browse and custom filename selection
  7. # TODO: Create executable distributions
  8. #
  9. import telnetlib_receive_all
  10. import time
  11. import Image
  12. import StringIO
  13. import sys
  14. import os
  15. import platform
  16. # Update the next lines for your own default settings:
  17. path_to_save = ""
  18. save_format = "PNG"
  19. IP_DS1104Z = "192.168.1.3"
  20. # Rigol/LXI specific constants
  21. port = 5555
  22. expected_len = 1152068
  23. TMC_header_len = 11
  24. terminator_len = 3
  25. big_wait = 10
  26. small_wait = 1
  27. company = 0
  28. model = 1
  29. serial = 2
  30. # Check parameters
  31. script_name = os.path.basename(sys.argv[0])
  32. # Print usage
  33. print
  34. print "Usage:"
  35. print " " + script_name + " [oscilloscope_IP [save_path [png | bmp]]]"
  36. print
  37. print "Usage examples:"
  38. print " " + script_name
  39. print " " + script_name + " 192.168.1.3"
  40. print " " + script_name + " 192.168.1.3 my_place_for_osc_captures"
  41. print " " + script_name + " 192.168.1.3 my_place_for_osc_captures BMP"
  42. print
  43. print "This program capture the image displayed"
  44. print " by a Rigol DS1000Z series oscilloscope, then save it on the computer"
  45. print " as a PNG or BMP file with a timestamp in the file name."
  46. print
  47. print " The program is using LXI protocol, so the computer"
  48. print " must have LAN connection with the oscilloscope."
  49. print " USB and/or GPIB connections are not used by this software."
  50. print
  51. print " No VISA, IVI or Rigol drivers are needed."
  52. print
  53. # Create/check if 'path' exists
  54. # Check network response (ping)
  55. if platform.system() == "Windows":
  56. response = os.system("ping -n 1 " + IP_DS1104Z + " > nul")
  57. else:
  58. response = os.system("ping -c 1 " + IP_DS1104Z + " > /dev/null")
  59. if response != 0:
  60. print
  61. print "No response pinging " + IP_DS1104Z
  62. print "Check network cables and settings."
  63. print "You should be able to ping the oscilloscope."
  64. # Open a modified telnet session
  65. # The default telnetlib drops 0x00 characters,
  66. # so a modified library 'telnetlib_receive_all' is used instead
  67. tn = telnetlib_receive_all.Telnet(IP_DS1104Z, port)
  68. tn.write("*idn?") # ask for instrument ID
  69. instrument_id = tn.read_until("\n", 1)
  70. # Check if instrument is set to accept LAN commands
  71. if instrument_id == "command error":
  72. print instrument_id
  73. print "Check the oscilloscope settings."
  74. print "Utility -> IO Setting -> RemoteIO -> LAN must be ON"
  75. sys.exit("ERROR")
  76. # Check if instrument is indeed a Rigol DS1000Z series
  77. id_fields = instrument_id.split(",")
  78. if (id_fields[company] != "RIGOL TECHNOLOGIES") or \
  79. (id_fields[model][:3] != "DS1") or (id_fields[model][-1] != "Z"):
  80. print
  81. print "ERROR: No Rigol from series DS1000Z found at ", IP_DS1104Z
  82. sys.exit("ERROR")
  83. print "Instrument ID:"
  84. print instrument_id
  85. # Prepare filename as C:\MODEL_SERIAL_YYYY-MM-DD_HH.MM.SS
  86. timestamp = time.strftime("%Y-%m-%d_%H.%M.%S", time.localtime())
  87. filename = path_to_save + id_fields[model] + "_" + id_fields[serial] + "_" + timestamp
  88. # Ask for an oscilloscope display print screen
  89. tn.write("display:data?")
  90. print "Receiving..."
  91. buff = tn.read_until("\n", big_wait)
  92. # Just in case the transfer did not complete in the expected time
  93. while len(buff) < expected_len:
  94. tmp = tn.read_until("\n", small_wait)
  95. if len(tmp) == 0:
  96. break
  97. buff += tmp
  98. # Strip TMC Blockheader and terminator bytes
  99. buff = buff[TMC_header_len:-terminator_len]
  100. # Save as PNG
  101. im = Image.open(StringIO.StringIO(buff))
  102. im.save(filename + ".png", "PNG")
  103. print "Saved file:", filename + ".png"
  104. # Save as BMP
  105. # scr_file = open(filename + ".bmp", "wb")
  106. # scr_file.write(buff)
  107. # scr_file.close()
  108. tn.close()