用python写的一个简单的串口程序实现自动化测试
用例1:#!/usr/bin/python# -*- coding: UTF-8 -*-import serialfrom time import sleepdef recv(serial):while True:data = serial.readline()if data == '':continueelse:breaksleep(0.02)re...
·
用例1:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import serial
from time import sleep
def recv(serial):
while True:
data = serial.readline()
if data == '':
continue
else:
break
sleep(0.02)
return data
if __name__ == '__main__':
serial = serial.Serial('/dev/ttyUSB0', 115200, rtscts=False, timeout=0.5)
if serial.isOpen() :
print("pen success")
else :
print("open failed")
print("===========start to execute test 1 ============")
cmd="mytest get_gpio_status 1\n"
serial.write(cmd.encode())
while True:
data =recv(serial)
if data != b'' :
data1=str(data).split('#')
if data1[len(data1)-1] == " \'":
break
ret=str(data).split('@')
if len(ret) == 1:
print("")
else:
if ret[len(ret)-1][0] == "1":
print("OK: recv 1\n")
else:
print("FAIL_flm\n")
sleep(4)
print("OVER\n")
serial.close()
用例2:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import serial
from time import sleep
def recv(serial):
while True:
data = serial.readline()
if data == '':
continue
else:
break
sleep(0.02)
return data
if __name__ == '__main__':
serial = serial.Serial('/dev/ttyUSB0', 115200, rtscts=False, timeout=0.5)
if serial.isOpen() :
print("open success")
else :
print("open failed")
print("===========start to execute test 2 ============")
cmd="mytest get_gpio_status 2\n"
serial.write(cmd.encode())
while True:
data =recv(serial)
if data != b'' :
data1=str(data).split('#')
if data1[len(data1)-1] == " \'":
break
ret=str(data).split('@')
if len(ret) == 1:
print("")
else:
if ret[len(ret)-1][0] == "2":
print("OK: recv 2\n")
else:
print("FAIL\n")
sleep(4)
print("OVER!")
serial.close()
在一个脚本文件m
python3 1.py
python3 2.py
注意: File "c:\work\share\xxx\python\ssh\uart.py", line 21, in <module>
ser = serial.Serial(node='/dev/ttyUSB0', baudrate=115200, timeout=0.5)
AttributeError: module 'serial' has no attribute 'Serial'
很有可能是串口硬件没有连接导致的
更多推荐
已为社区贡献2条内容
所有评论(0)