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

OscScreenGrabLAN.py 3.6KB

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