python实现控制COM口的示例
使用RS232串口线或者是RS232转USB的这一类的接口,会需要com口作为接口来进行输入输出调式,
写了个脚本来控制COM口,用到了Python内建的serial库
代码如下:
#coding=utf-8
importserial
importtime
defsetTout(t):
print"OldTimeoutis:[%s]"%po1.getTimeout()
po1.setTimeout(t)
print"NewTimeoutis:[%s]"%po1.getTimeout()
defsendShell(sp,cmd):
sp.write(cmd+"\n")
print"sendshellcmd:[%s]"%cmd
str=sp.readall()
returnstr
defshell_io(sp,cmd,sleepTime):
str=sendShell(sp,cmd)
printstr
time.sleep(sleepTime)
po1=serial.Serial('com1',115200)
timeStart=time.time()
portnow=po1.portstr
print"COMportnowis:[%s]"%portnow
setTout(5)
shell_io(po1,"ls",2)
shell_io(po1,"pwd",2)
shell_io(po1,"ls-l",2)
po1.close()
以上这篇python实现控制COM口的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。