Capture the display of a Rigol DS1000Z series oscilloscope by LAN using LXI SCPI commands
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OscScreenGrabLAN.py 3.6KB

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