1、安装 pip install robotframework-seriallibrary
https://pypi.org/project/robotframework-seriallibrary/
2、导入改库
SerialLibrary
3、简单使用 注意 格式和 波特率
https://github.com/whosaysni/robotframework-seriallibrary/blob/develop/src/SerialLibrary/__init__.py 可以查询 port的参数
#baudrate, bytesize, parity, stopbits, \ \ \ \ \ \ \ \ timeout, xonxoff, rtscts, write_timeout, dsrdtr and \ \ \ \ \ \ \ \ inter_byte_timeout.
基本用例 com——test
注意格式:
com_test SerialLibrary.Add Port COM1 baudrate=9600 #baudrate, bytesize, parity, stopbits, \ \ \ \ \ \ \ \ timeout, xonxoff, rtscts, write_timeout, dsrdtr and \ \ \ \ \ \ \ \ inter_byte_timeout. SerialLibrary.Open Port ${a} SerialLibrary.Get Encoding log ${a} SerialLibrary.Write Data \n UTF-8 SerialLibrary.Flush Port sleep 2 SerialLibrary.Write Data \n UTF-8 sleep 5 SerialLibrary.Read All Data sleep 2 Comment ${result2} ${return2} Run Keyword And Ignore Error com_data_judge ${read} ${val_condition} Comment Run Keyword If '${result2}'=='FAIL' log ok ... ELSE log not_ok Wait Until Keyword Succeeds 100X 3 com_data_judge display current-configuration local-user admin password cipher OUM!K%F<+$[Q=^Q`MAF4<1!! SerialLibrary.Flush Port SerialLibrary.Write Data \n UTF-8 sleep 3 SerialLibrary.Close Port SerialLibrary.Delete All Ports
4、关键字 com_data_judge
com_data_judge [Arguments] ${cli} ${judeg_condition} SerialLibrary.Write Data ${cli} UTF-8 sleep 2 SerialLibrary.Write Data \n UTF-8 sleep 5 ${read} SerialLibrary.Read All Data log ${read} sleep 2 ${judeg_condition} Convert To String ${judeg_condition} ${ok} ${num_idex} Com Data ${read} ${judeg_condition} log ${ok} log ${num_idex} Should Be Equal As Strings ${ok} ok
5、判断data 的py脚本
字符串的去除空格
编码转换(串口hexlify 转换到 unhexlify )
字符串 分片
字符串去除首尾空格
比对字符串的值
脚本:
# -*- coding: utf-8 -*- import binascii def com_data(data1,val): data1 = data1.replace(" ","") data = binascii.unhexlify(data1) print data data = data.split("\r\n") val = val.encode('gbk') for i in data: com_data1 = i.strip() if com_data1 == val: num = data.index(i) return "ok",num return "not_ok" if __name__ == "__main__": s = "72 6F 6F 74 6E 45 37 6A 41 25 35 6D" a,b=com_data(s,"vlan batch 4000 to 4001") print a,b
来源:https://www.cnblogs.com/classics/p/11155841.html