sam sam - 1 month ago 15
Python Question

Telnet session to ESG 4438E

I am trying to control a signal generator E4438C using python. I just need to change the amplitude and frequency.`my code is below:

import telnetlib


HOST = "169.145.1.1"
timeout = 5
Debug = False
PORT = 5023

def dprint(msg):
if (Debug == True):
print msg
tn = telnetlib.Telnet(HOST, PORT , timeout, Debug)
tn.set_debuglevel(9)
dprint(tn.read_until("SCPI>", timeout))
tn.write('FREQ 1.92ghz' + "\n")
tn.read_until("SCPI>", timeout)
tn.write('AMPL 10dbm' + "\n")
tn.read_until("SCPI>", timeout)
tn.close()`


when I open a telnet session directly on port 5023 i am able to change the Freq and Amp but not using the script above. the debug session shows the telnet connection is successful and it did send the commands but nothing changes on the sig Gen. thank you

Answer

Test

I've run your code against a listening nc, and Python 2.7 complained that the call to telnetlib.Telnet(HOST, PORT , timeout, Debug) was wrong - the parameter Debug should have been omitted.

Actually, in the documentation the Debug parameter isn't present:

Telnet.open(host[, port[, timeout]])

Connect to a host. The optional second argument is the port number, which defaults to the standard Telnet port (23). The optional

timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).

Do not try to reopen an already connected instance.

Changed in version 2.6: timeout was added.

Once fixed that, however, the commands were sent identical by telnet in raw mode and by your code. It could be some weird timing thing, but apart from that it should work.

Just to be safe I'd still run a tcpdump if I were you.

Old answer

You aren't terminating the commands, so they are not getting executed. .write() does not auto-terminate commands.

tn.write('FREQ 1.92ghz' + "\n")

Also, wait for an answer before closing, just in case.

tn.read_until("some answer", timeout)
tn.close()