When working on a project, I sometimes want to get a quick screen capture from my Rigol DS1054Z for documentation purposes. In the past I have used DSRemote for this. However, being a tool to remotely control your scope it doesn't cope well with using the physical inputs on the scope at the same time. What I needed was a simple tool to just get a screenshot from the scope, nothing more. I also didn't want to install any requirements. Luckily, Linux comes with a USB-TMC (test and measurement class) driver built-in. I then went along trying to figure out the simplest way to get a screen capture using pure bash. The result is shown below.
#!/bin/bash ( echo ':DISPlay:DATA? ON,OFF,PNG'; head -c $(head -c $(head -c 2 | tail -c 1) | sed 's/^0*//') > capture.png ) 3<>/dev/usbtmc1 <&3 >&3
This assumes the scope shows up as /dev/usbtmc1 and you want the screen capture written to capture.png.
What does it do? Well, it starts by opening the device /dev/usbtmc1 (conveniently generated by the kernel when plugging in the scope) for reading an writing. The stdin and stdout of the block in parentheses are then connected to the device. Now for the actual magic: first, a screen capture in PNG format is requested using the appropriate SCPI commands, by echo'ing them to the scope. The scopes reply looks like this #9001152054..., where ... represents the actual binary image data. The hash sign (#) is always there and can simply be ignored. The next character is a number indicating the length of the header string. In the bash script this is separated using a combination of head and tail. Using this length, the remaining header is retrieved. This part contains the size of the image in bytes. The leading zeroes are removed using sed and then head is used to retrieve the image data itself, which is finally piped to the output file capture.png.
And there you have it, a simple screen capture tool for Rigol scopes in just four lines of bash code!