zoukankan      html  css  js  c++  java
  • 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
       
    
     
  • 相关阅读:
    All in One 你想知道的 hacker 技术都在这里
    5 个 Git 工作流,改善你的开发流程
    完全基于 Java 的开源深度学习平台,亚马逊的大佬带你上手
    感谢 Vue.js 拯救我这个前端渣渣,让 PowerJob 有了管理后台界面
    linux报错Loading mirror speeds from cached hostfile解决方法
    Linux使用mailx通过第三方SMTP发送邮件,带附件操作
    打包发送邮件
    springboot项目中使用设计模式一策略模式
    Spring Boot 开发 WebService 服务
    常见限流算法介绍(漏桶算法、令牌桶算法)及实现--待整理
  • 原文地址:https://www.cnblogs.com/classics/p/11378464.html
Copyright © 2011-2022 走看看