python 转化串口中的数据 ,并分组判断

# -*- coding: utf-8 -*-
import binascii



def split_1(data,split_sign):
    data2=[]
    for i in data:
        data3 = i.split(split_sign)
        for n in data3:
            n=n.strip()
            data2.append(n)
    return data2
def com_data(data1,*val):
    data1 = data1.replace(" ","")
    data2 = binascii.unhexlify(data1)
    print data2
    data2 = data2.split("\r\n")  
    data5=split_1(data2,",")
    data6=split_1(data5,"{")
    data=split_1(data6,"}")
    data4=split_1(data,"\r")
    

    print "-----------"
    print data4
    print "-----------"
    num=0
    if type(val) is tuple:
        val=list(val)
    val2=[]
    for i in val:
        val1 = i.encode('gbk')
        #print val1
        #print type(val1)
        val1 = val1.split(",") 
        for n in val1:
            n=n.strip()
            val2.append(n)
    print "-----------"
    print val2
    print "-----------"
    for i in val2:
        print i
        if i in data4:
            num = data4.index(i)
            print "%s is ok."%i
        else:
            return "not_ok",None
    return "ok",num

def com_read(data2):
     data2 = data2.replace(" ","")
     data = binascii.unhexlify(data2)
     #data = data.encode('gbk')
     #print data
     #data = data.split("\r\n") 
     #print data  
     return data
def com_read_contain(data2,val2):
     data2 = data2.replace(" ","")
     data = binascii.unhexlify(data2)
     #data = data.encode('gbk')
     val2 = val2.encode('gbk')
     #print data
     #data = data.split("\r\n") 
     #print data  
     return data,val2
    

if __name__ == "__main__":
    s = "62 72 63 74 6C 20 73 68 6F 77 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 34 30 30 30 2C 20 6F 66 66 3D 31 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 34 30 30 30 2C 20 6F 66 66 3D 31 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 0D 0A 62 72 63 74 6C 3A 20 69 66 61 63 65 20 65 74 68 33 62 72 63 74 6C 3A 20 4E 6F 20 73 75 63 68 20 64 65 76 69 63 65 0D 0A 23 20 0D 0A 23 20 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 34 30 30 30 2C 20 6F 66 66 3D 31 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 34 30 30 30 2C 20 6F 66 66 3D 31 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 34 30 30 30 2C 20 6F 66 66 3D 31 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 34 30 30 30 2C 20 6F 66 66 3D 31 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 34 30 30 30 2C 20 6F 66 66 3D 31 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A 6C 65 64 3D 39 2C 20 6F 6E 3D 31 2C 20 6F 66 66 3D 34 30 30 30 2C 20 62 6C 69 6E 6B 73 2C 3D 31 2C 20 72 65 73 65 74 3D 31 2C 20 74 69 6D 65 3D 34 30 30 30 0D 0A"
    a,b=com_data(s,"eth3")
    #a,b=com_data(s,'root@OpenWrt:~#')
    print a,b
    #c = com_read(s)
    #print c
   

 

 

转载于:https://www.cnblogs.com/classics/p/11378464.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值