Test and Measurement Instrument control using PHP
December 11th, 2011. Posted in Test and Measurement, TutorialsThere are various ways of remote controlling instruments such as remote desktop or VNC for getting access to the screen but often you want it to do something specific in an automated way. Many people use tools such as Labiew or even full on programming languages like Visual C, C#, VB etc with drivers (VXI or LXI) to achieve robust production solutions. Often when I am asked by customers to assist with some remote control, I turn to a web scripting language called PHP. Its not the normal scripting language people think of for instrument remote control but it has some benefits for me:
- It has socket support allowing control to port 5025 by sending SCPI commands as ASCII to write commands and read results.
- Its fast to develop, is very much like C but with simplicity.
- Has built in support for Regular Expressions and Text Parsing meaning string manipulation is quick and easy.
- As I’m familiar with the language I can knock up scripts quickly and send the SCPI command sequence to the customer.
- It can be used with a web server. There are possibilities with PHP to build a scaleable instrument remote control solution that is becomes a web application, meaning easy control of instruments just by logging onto a website.
- If required a complex system could be developed to easily store and recall results from a MySQL back end.
Of course there are other languages that could be used and are often used by customers such as Perl or Python to achieve the above tasks, I just happen to get on with PHP.
A simple example might be something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | // Allow the script to hang around waiting for connections set_time_limit (0); $fsq_ip_address = "192.168.1.2"; $debug = "OFF" $port = 5025; // open socket to instrument $fsq= fsockopen ($fsq_ip_address, $port, $errno, $errstr, 10); // open the output file for results $fp = fopen ("output.txt", "w+"); // query instrument for its details and output result writescpi($fsq,$debug, "*IDN?"); $result = readscpi($fsq); logger("Instrument: " . $result); // close sockets and file fclose ($fsq); fclose ($fp); // Function Definitions // Write string to the socket (output errors if debug enabled) function writescpi($inst,$debug,$msg) { fputs ($inst, $msg . "\n"); if ($debug == true) { print ("\nCmd: " . $msg); fputs ($inst, "syst:err?\n"); $result = readscpi($inst); print ("Result: " . $result . "\n"); } } // Read string from the socket function readscpi($inst) { $buf = fgets ($inst,100000); $buf = trim ($buf); return $buf; } // output to screen and to file at the same time function logger($result) { global $fp; print ($result . "\n"); fwrite ($fp, $result); } |