• 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("
    ")  
        data5=split_1(data2,",")
        data6=split_1(data5,"{")
        data=split_1(data6,"}")
        data4=split_1(data,"
    ")
        
    
        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("
    ") 
         #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("
    ") 
         #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
       
    
     
  • 相关阅读:
    JSP 和Servlet 有有什么关系?
    转发(forward)和重定向(redirect)的区别?
    get和post请求的区别?
    软件的三大类型-单机类型、BS类型、CS类型
    Redis集群搭建
    Tomcat网站上的core和deployer的区别
    spring 事务处理
    mybatis ${}与#{}的区别
    Quartz--Spring 定时任务
    @JsonSerialize @JsonIgnoreProperties @JsonIgnore @JsonFormat
  • 原文地址:https://www.cnblogs.com/classics/p/11378464.html
Copyright © 2020-2023  润新知