之前获取canon相机与镜头协议采用逻辑分析仪进行协议数据波形的获取,可以导出为txt文档,由于canon协议不是标准的通信协议,只有通过编写逻辑分析仪的解析程序才能进行相应的数据解析,对我做硬件来说,难度挺高。这几天正在学习python,所以决定用python编写相应的解析程序,通过对导出数据进行观察,找到相应的规律,通过百度,目前程序能够按照我的要求,直接输出格式化后的协议,具体代码如下:

'''
Created on 2016年11月1日

@author: pc-zmx

function:用于将逻辑分析仪抓取的txt数据格式化为HEX数据,并保存在txt中
                          获取source.txt的数据转化为target.txt文件
'''

import PyQt5.QtWidgets as Ui
import PyQt5.QtCore as QCore
import PyQt5.QtGui as QGui
import sys

'''
转换数据处理
'''
class CanonPyTxtClass(object):
    def __init__(self,f):
        self.start = False  #开始标识
        self.sample = False  #采样数据标识
        self.clkbit = 1
        self.dclbit = 1
        self.dlcbit = 1
        
        self.samplecount=0
        self.dcl = 0
        self.dlc = 0
        self.outfile = f

def LineDatProcess(self, strr):
        a = strr.split(",")
        
        self.clkbit,self.dclbit,self.dlcbit=int(a[1]),int(a[2]),int(a[3])

if(True==self.start):
            
            if(True == self.sample):
                
                if(1 == self.clkbit):
                    
                    self.sampleDat()    #上升沿获取数据
                    
                    self.sample = False
            else:
                if(0 == self.clkbit):
                    self.sample = True
        else:
            if(0 == self.clkbit):
                self.start=True
                self.sample=True

def sampleDat(self):        
        self.samplecount = self.samplecount +1
        if(self.samplecount <9):
            self.dcl +=self.dclbit<<(8-self.samplecount)
            self.dlc +=self.dlcbit<<(8-self.samplecount)

if(self.samplecount ==9):
            self.samplecount=0

self.ProcessDat()
                      
            self.dcl =0
            self.dlc =0
            
    def ProcessDat(self):  
        if(self.dcl <17):
            a = hex(self.dcl)
            b = a[2:]
            c='0'+b                            
        else:
            a = hex(self.dcl)
            c = a[2:]                 
        if(self.dlc <17):
            d = hex(self.dlc)
            e = d[2:]
            f='0'+e                            
        else:
            d = hex(self.dlc)
            f = d[2:]
        #print(c.upper(),f.upper())
        strr =c.upper()+ ' ' + f.upper()+ '  '+';'
        
        self.outfile.writelines(strr+'\n')

'''
转换界面设计
'''        
class FileSelDlg(Ui.QDialog):
    def __init__(self, parent=None):
        super(FileSelDlg, self).__init__(parent)
        FileLable = Ui.QLabel("转换源文件:") 
        self.filepathLineEdit = Ui.QLineEdit()
        openfileBtn = Ui.QPushButton("打开") 
        #self.SourceFilePath = Ui.QFileDialog()
        
        gridLayout = Ui.QGridLayout()
        gridLayout.addWidget(FileLable, 0, 0, 1, 1)
        gridLayout.addWidget(self.filepathLineEdit, 0, 1, 1, 4)
        gridLayout.addWidget(openfileBtn, 0, 5, 1, 1)
                
        startBtn = Ui.QPushButton("转换")
        completeBtn = Ui.QPushButton("完成")
        
        btnLayout = Ui.QHBoxLayout()       
        btnLayout.setSpacing(50)
        btnLayout.addWidget(startBtn)
        btnLayout.addWidget(completeBtn)
                
        dlgLayout = Ui.QVBoxLayout()    #垂直布局器
        dlgLayout.setContentsMargins(20, 20, 20, 20)        
        dlgLayout.addLayout(gridLayout) 
        dlgLayout.addStretch(200)   #平均分配  
        dlgLayout.addLayout(btnLayout)
               
        self.setLayout(dlgLayout)
        
        openfileBtn.clicked.connect(self.openfileBtnclicked)
        startBtn.clicked.connect(self.startBtnclicked)
        completeBtn.clicked.connect(self.reject)
        self.setWindowTitle("canon相机镜头协议转换TXT文本软件")
        self.resize(600, 150)

def openfileBtnclicked(self):
        self.files,ok1 = Ui.QFileDialog.getOpenFileName(
            self,
            "Open Document",
            "C:/Users/pc-zmx/Desktop/pytxt",
            "Document files (*.txt)")
            #"Document files (*.txt);;All files(*.*)")
        if ok1:
            self.filepathLineEdit.setText(self.files)
            print(self.files)
            
    def startBtnclicked(self):                                           
        if self.filepathLineEdit.text():    #判断文件是否存在
            sourcefilename = self.filepathLineEdit.text()
            pos = sourcefilename.find(".txt")
            targetfilename = sourcefilename[:pos] + "_格式化数据" + sourcefilename[pos:]
            f = open(sourcefilename, "r")
            line1 = f.readline()
            a = line1.split(",")
            if a[0][:4] != "Time":
                Ui.QMessageBox.information(self,                         #使用infomation信息框  
                                    "警告",  
                                    "源文件错误",  
                                    Ui.QMessageBox.Yes)                            
            else:
                ff = open(targetfilename,'w')
               
                canonpy = CanonPyTxtClass(ff)
                while True:
                    line = f.readline()
                    if line:
                        canonpy.LineDatProcess(line)            
                    else:
                        break                                                        
                Ui.QMessageBox.information(self,                         #使用infomation信息框  
                                    "提示",  
                                    "格式化完成",  
                                    Ui.QMessageBox.Yes)
                print('格式化完成')
                ff.close()
            f.close()
        else:
            Ui.QMessageBox.information(self,                         #使用infomation信息框  
                                    "警告",  
                                    "文件不能为空",  
                                    Ui.QMessageBox.Yes)
                         
if __name__ == '__main__':
    
    app = Ui.QApplication(sys.argv)
    dlg = FileSelDlg()
    dlg.show()
    dlg.exec_()
    app.exit()

程序功能比较简单,这里就不多做解释了,python3.5,pyqt5,运行界面如下所示:

canon相机镜头协议数据用python进行格式化相关推荐

  1. canon相机镜头协议

    这段时间一直在搞canon相机与镜头之间的协议,目前工作算是有一部分结果,基本功能使用起来没啥问题,能够正常的对焦,拍照了.今天上午测试了下EF 50mm 1:1.8 II镜头的工作电流,5V 23. ...

  2. Python输出格式化 格式化字符串语法 format f-string 格式化操作符% 数据类型转换 对齐方式 转换标志字符

    Python输出格式化 格式化字符串语法 1.format 1.1 Format String Syntax 格式字符串语法 str.format() 方法和 Formatter 类共享相同的格式字符 ...

  3. 如何使用python读取modbus/TCP协议数据

    文章目录 前言 一.modbus_tk是什么? 二.modbus_tk的使用步骤 三.使用modscan测试 四.32位无符号短整型数据转为64位float数据 五.总结 前言 在做项目的时候,需要使 ...

  4. python 字符串格式化是打印不同类型更简单一些

    Python 支持格式化字符串的输出 与 C 中 sprintf 函数一样的语法 下面写3中不同类型的数据合在一起打印 name = "张三丰" height = 1.88 wei ...

  5. python语言格式化输出_Python format()格式化输出方法详解

    原标题:Python format()格式化输出方法详解 format() 方法的语法格式如下: str.format(args) 此方法中,str 用于指定字符串的显示样式:args 用于指定要进行 ...

  6. 2017 年最流行的 15 个数据科学 Python 库

    转自http://www.codeceo.com/article/15-data-science-python-libraries.html 2017 年最流行的 15 个数据科学 Python 库 ...

  7. 【记录】python多线程的使用 线程同步(LOCK和RLOCK) python与mysql数据库交互实现增加和查找 python的格式化输出

    文章目录 多线程: _thread模块 threading模块: 线程同步(LOCK和RLOCK) 和mysql交互: 附:python的格式化输出 附录 多线程: _thread模块 例一 impo ...

  8. python中格式化字符串_Python中所有字符串格式化的指南

    python中格式化字符串 Strings are one of the most essential and used datatypes in programming. It allows the ...

  9. python 路径格式化_吐血整理!140种Python标准库、第三方库和外部工具都有了!...

    导读: Python数据工具箱涵盖从数据源到数据可视化的完整流程中涉及到的常用库.函数和外部工具.其中既有Python内置函数和标准库,又有第三方库和工具. 这些库可用于文件读写.网络抓取和解析.数据 ...

最新文章

  1. 华为鸿蒙系统四大特性:基于微内核,面向全场景,分布式架构
  2. WAMPSERVER安装之笑话
  3. 【工具】WPS安卓电脑无广告版
  4. 谈阿里核心业务监控平台SunFire的技术架构
  5. 基于 Istio 的全链路灰度方案探索和实践
  6. OpenCASCADE:要求
  7. linux 程序返回值 139,Linux系统监控之ssh登陆自动139邮件短信提醒
  8. 一行命令搭建内部的管道
  9. jsp怎么调用servlet_Servlet简述
  10. java 编译中没有清除之前编译出来的文件。
  11. Datawhale 零基础入门数据挖掘-Task5 模型融合
  12. 滴滴辞退2000人启示:牛逼的人,都有自己的铁饭碗
  13. 3.企业安全建设入门(基于开源软件打造企业网络安全) --- 业务网安全加固
  14. android画板笔锋实现
  15. oracle定时任务在哪,oracle定时任务
  16. 【Excel 教程系列第 15 篇】Excel 中的简单排序(升序 / 降序)、多条件排序、按颜色排序、自定义排序、以及巧用“升序“制作工资条
  17. 如何使用robots.txt及其详解
  18. relative的使用
  19. unity+cardboard细节总结
  20. Unity3D windows平台视频录制录屏插件 UnityRecorder

热门文章

  1. PCL计算ConvexHull凸包、ConcaveHull凹包
  2. 51AD转换及简易电子电压表!
  3. 用Java写的一个万年日历
  4. 华为S7900产品概述
  5. wordpress异步ajax上传文件
  6. Unity之在UI界面上显示3D模型
  7. 一、ABP启动运行项目
  8. linux下安装小米摄像头,在深度Deepin系统中安装米聊的方法(安装MiTalk deb及AppImage包)...
  9. if() 后面加分号
  10. python中sample怎么用_python – 在Keras中使用sample_weight进行序列标记