嵌入式产品都会用到串口,在linux下使用minicom可以满足自动化需求。windows应该选择什么工具,我试了plink和ss。
ss (SimplySerial) 开源工具源地址
plink,putty的命令行工具,安装putty即可。
ss (SimplySerial) 相比plink的好处是可以通过 -log:{logfile} 参数直接保存log。但是通过pexcept调用会有问题。
import pexpect
from pexpect import popen_spawn
app = 'ss.exe -com:COM3 -baud:115200'
p = pexpect.popen_spawn.PopenSpawn(app, timeout=2, encoding="gbk")
print(p.before)
p.sendline('show')
p.expect(pexpect.TIMEOUT, timeout=3)
print(p.before)
p.sendline('exit')
p.sendeof()
结果
None
Unhandled Exception: System.IO.IOException: The handle is invalid.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
at System.Console.Clear()
at SimplySerial.SimplySerial.ProcessArguments(String[] args)
at SimplySerial.SimplySerial.Main(String[] args)
Process finished with exit code 0
改用plink
import pexpect
from pexpect import popen_spawn
app = 'plink -v -serial COM3 -sercfg 115000,8,1,n,N'
p = pexpect.popen_spawn.PopenSpawn(app, timeout=2, encoding="gbk")
print(p.before)
p.sendline('show')
p.expect(pexpect.TIMEOUT, timeout=3)
print(p.before)
p.sendline('exit')
p.sendeof()
结果
None
Opening serial device COM3
Configuring baud rate 115000
Configuring 8 data bits
Configuring 1 stop bit
Configuring no parity
Configuring no flow control
/system/bin/sh: show: inaccessible or not found
127|console:/ $
Process finished with exit code 0
plink的log可以用 "| tee plink.log" 保存。