使用RS232串口线或者是RS232转USB的这一类的接口,会需要com口作为接口来进行输入输出调式,
写了个脚本来控制COM口,用到了Python内建的serial库
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# coding=utf-8 import serial import time def setTout(t): print "Old Timeout is:[%s]" % po1.getTimeout() po1.setTimeout(t) print "New Timeout is:[%s]" % po1.getTimeout() def sendShell(sp,cmd): sp.write(cmd + "\n" ) print "send shell cmd:[%s]" % cmd str = sp.readall() return str def shell_io(sp,cmd,sleepTime): str = sendShell(sp,cmd) print str time.sleep(sleepTime) po1 = serial.Serial( 'com1' , 115200 ) timeStart = time.time() portnow = po1.portstr print "COM port now is:[%s]" % portnow setTout( 5 ) shell_io(po1, "ls" , 2 ) shell_io(po1, "pwd" , 2 ) shell_io(po1, "ls -l" , 2 ) po1.close() |
以上这篇python实现控制COM口的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/runheartt/article/details/53258525