1

I am using python's socket module to send a simple SCPI command to my Keysight device, but I get a blank response. Does anyone know what I am doing wrong?

Here is my simple script.

import socket
s = socket.socket()
print("connecting")
s.connect(("192.168.25.7",5024))
print("sending")
s.settimeout(10)
s.send("*IDN?\n")
print(s.recv(2048))

I expect this script to return

➜  ~ python hack_na.py
connecting
sending
Agilent Technologies,E5063A,MY54100104,A.03.00
➜  ~ 

But if I run the script, I get this output instead.

➜  ~ python hack_na.py
connecting
sending

➜  ~ 

I can confirm that I get the desigred output with telnet.

➜  ~ telnet 192.168.25.7 5024
Trying 192.168.25.7...
Connected to 192.168.25.7.
Escape character is '^]'.

SCPI> *IDN?
Agilent Technologies,E5063A,MY54100104,A.03.00
SCPI>
telnet> Connection closed.

Thank you all in advance.

1 Answer 1

3

These two versions are not the same.

You're not seeing any output in the first case because you're not actually sending a complete message from the socket to the server. The telnet protocol uses \r\n to end lines, not just \n. So you'd need to do s.send("*IDN?\r\n").

But really, you should use a better tool for the job. Python's socket module is just direct bindings to the BSD socket interface, usually used for building low-level networking applications. As such, you'll need to worry about annoying details like the line-endings yourself. A better alternative is to use a higher-level library, more tailored for your purpose. telnetlib is a builtin module for operating as a telnet client, or you could use a third-party library explicitly for SCPI.

Sign up to request clarification or add additional context in comments.

1 Comment

telnetlib — Telnet client Source code: Lib/telnetlib.py Deprecated since version 3.11, will be removed in version 3.13: The telnetlib module is deprecated (see PEP 594 for details and alternatives).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.