远程登录会话到ESG信号发生器

2024-05-12 23:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python控制一个信号发生器。我只需要改变振幅和频率

import telnetlib
HOST = "169.145.1.1"
timeout     = 5
PORT    = 5023
tn = telnetlib.Telnet(HOST, PORT , timeout)
tn.read_until("SCPI>", timeout)
tn.write('FREQ 3ghz\r\n')
tn.read_until("SCPI>", timeout)
tn.write('AMPL 10dbm\r\n')
tn.read_until("SCPI>", timeout)
tn.close()`

当我在端口5023上直接打开telnet会话时,我可以更改Freq和Amp,但不能使用上面的脚本。调试会话显示telnet连接成功,它确实发送了命令,但sig Gen上没有任何更改。谢谢


Tags: importhostread信号porttimeoutscpitelnet
2条回答

我想出来了。我需要在每个命令之后添加\r\n,而不仅仅是\n。在实现此更改后,Sig Gen值发生了更改。我更新了剧本。谢谢

试验

我针对监听nc运行了您的代码,python2.7抱怨对telnetlib.Telnet(HOST, PORT , timeout, Debug)的调用是错误的,参数Debug应该被省略。在

实际上,在documentation中,调试参数不存在:

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.

不过,一旦修复了这个问题,telnet在原始模式下发送的命令与您的代码完全相同。这可能是一个奇怪的时机问题,但除此之外,它应该是有效的。在

为了安全起见,如果我是你的话,我还是会开tcpdump的。在

旧答案

您没有终止这些命令,因此它们不会被执行。.write()does not auto-terminate命令。在

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

另外,在关门前等待答复,以防万一。在

^{pr2}$

相关问题 更多 >