我想使用Pyserial与路由器(更改IP地址)进行通信。它只在我运行以下代码时有效:
def ipaddr_set():
ser = serial.Serial("/dev/tty_dgrp_aa_0",baudrate=115200)
ser.open()
channel =fdpexpect.fdspawn(ser.fd)
channel.send("\n\n")
time.sleep(2)
channel.send("\nusername\npassword\n")
time.sleep(2)
channel.send("\n ip-adress 114.293.232.20\n")
time.sleep(2)
channel.send("exit\n")
ser.close()
但是如果我使用pexpect添加一个函数:
def ftp_firmware_update():
child = pexpect.spawn('ftp 114.293.232.20')
child.delaybeforesend = 0.1
child.expect('(?i)name .*: ')
child.sendline('username')
child.expect('(?i)Password')
child.sendline('pass')
child.expect('ftp> ')
child.sendline('put firmware.bin')
child.expect('Upgrade')
child.sendline('close')
child.expect('221')
print child.before,child.after
child.sendline('quit')
child.close()
print "Upgrade firmware finished, sleep..."
time.sleep(20)
ipaddr_set()运行不正常(它被执行但ip没有改变)。有人解决这种情况吗?
非常感谢!!