Thursday, July 30, 2015

How do I use Python (and no installed IO Library) to talk to my instrument?

Hello everybody,

Our newest support engineer and I have decided to learn Python (https://www.python.org/) so that we can generate programming examples.  We are hearing that more and more customers are using Python so we figured that we would get on the front side and learn some Python.  This blog represents my first attempts at doing anything in Python so there are probably better methods to do this but I wanted to get this out since I am pretty excited about it.  

What we are going to do is do use Python with Telnet and LAN to directly send SCPI commands to my instrument.  The major advantage of this is that with this method, you do not need to use an IO Library so you can use it in different operating systems.  Since we are going to be using Telnet with Python, the first thing that we are going to need to do is to make sure we understand how telnet works with our power supplies.  For all my work here, I will be using my N7953A Advanced Power System (APS) because it is on my desk and it is awesome.  The APS uses port 5024 for telnet.  On my PC running Windows 7, I enter the following in my command window:

Figure 1







Once I hit enter, I get our very friendly welcome screen:

Figure 2







Note the text “Welcome to …”, this will be important later.

To send commands or do queries, you just need to enter the SCPI command after the prompt.   The response to the query will automatically appear after you hit "Enter".  Here is how you do a *IDN? Query:

Figure 3








Note that the query response ends with a new line.  This will be important later.  The prompt also re-appears after every interaction.  In the case of the APS, the prompt is the model number ("N7953A>").  On some other instruments , the prompt is "SCPI>".  Either way, you need to know what the prompt is so that you can account for it later.

 Now that we are Telnet experts, we are going to switch to Python and send a *IDN? query to my APS.  The first thing we are going to do is to import the telnet library.  After that we are going to create our APS object by opening a telnet session to the instrument (sorry these screenshots might overflow the frame a very tiny bit):

Figure 4









Basically, we are at the same point we are at in Figure 1.  We essentially just hit the "Enter" key.  We need to get to the equivalent to Figure 2 so that we can send a command.  That means that we need to read out all of that welcome text from the power supply.  Luckily, the Python Telnet Library has a read_until function that will read text until it encounters a predefined string.  We know that our prompt to enter text in this case is "N6753A>" so that is what we are going to use:


Figure 5

Let’s send our *IDN? query.  You use the write command to write to the Telnet session.  Helpful tip: all commands sent through telnet need to be terminated with a newline ("\n"):

Figure 6

So now that we sent a query, we need to read the buffer to get the response.  Remember that the query response ends with a newline so we are going to use read_until and use the newline ("\n") as our read_until text:

Figure 7














As you can see we get the same response as before.

So now we sent our command and read the response, we need to read out the prompt again so that our power supply is ready to accept the next command sent to it.  We will just use the same read_until that we used before:

Figure 8



Now we are set to send the next command.

When you are done with programming the instrument, you end the Telnet session with the close command:

Figure 9


So that's an extremely basic example of how to use Python and Telnet.  I used the shell because that is what I used to figure this all out.  You can also write scripts.  As Chris and I learn more about Python, we will be releasing more examples.  Stay tuned for those.


3 comments:

  1. Hi Matt,

    Thanks for sharing this. I've talked to customers who would like to write Python scripts to be runned under *nix systems so that's helpful.

    I have one question regarding access protection. Usually Telnet connection asks for a password and in your example it was not mentioned. Is it possible to password protect Telnet access?

    ReplyDelete
  2. Hey Alexey,

    We do not require a login on our power supplies. If we did, you could use the read_until function to figure out where the prompts were and then use the send function to send in your information.

    ReplyDelete
  3. Excellent and Thanks for sharing this kind of info. I have some customers writing Python spcripts right now.

    Excellent Blog!!

    ReplyDelete