文章目录1.通讯脚本基础知识1.1 通信脚本基础知识简介1.2 通信脚本的优势2. 接收事件-脚本2.1 getOutputParam() 2.2 handleMessage(info):3.发送事件-脚本3.1 getInputParam() 3.2 handleMessage(list):4. 协议解析-脚本4.1 getOutputParam()4.2 handleMessage(info)5.通讯脚本案例介绍5.1 接收事件-脚本示例介绍5.1.1 创建TCP通信连接5.1.2 创建接收事件-脚本5.1.3 使用PyCharm或者Notepad编写脚本内容5.1.4 配置全局触发5.1.5 效果展示5.2 发送事件-脚本示例介绍5.2.1 创建TCP通信连接5.2.2 创建发送事件-脚本5.2.3 编写发送事件-脚本代码5.2.4 添加数据模块6.补充资料1.通讯脚本基础知识在VM中通信管理中实现了接收事件和发送事件的功能可以满足一般的接收数据解析和组装数据发送的功能但是对于 一些复杂和特定需求的数据无法完成解析和组装。所以在接收事件、发送事件、协议解析模块中增加了通信脚本对复杂的数据进行解析和组装以满足特定的应用需求。1.1 通信脚本基础知识简介下面对通信脚本的基础知识进行介绍一.通信脚本编程语言——python二.通信python脚本涉及模块或功能——接收事件、发送事件、协议解析1.2 通信脚本的优势通信脚本使用的是python语言编程且是独立的python具备以下优势用户完全控制通信脚本中解析和组装代码用户可以任意实现编程复杂度较低使用python脚本进行程序编写代码简单且提供简单的示例程序灵活性强在协议解析和协议组装中用户可以自行编写解析和组装的代码可以灵活实现复杂数据的 解析和组装工作支持多种数据类型目前所有的脚本都支持stringintfloatbyte四种数据类型的输入或输出2. 接收事件-脚本功能描述对通信接收到的数据进行解析后输出到组装列表。VM4.2.0接收事件-脚本示例路径\VisionMaster4.2.0\Applications\VmModuleProxy\x64\RecvEventTest.pyVM4.4.0接收事件-脚本示例路径\VisionMaster4.4.0\Applications\ModuleProxy\x64\RecvEventTest.py# hex data parsing demo# 接收事件脚本文件示例importstructimportloggingimportctypes################################################## output logging messages to DbgView via OutputDebugString (Windows only!)# OutputDebugString ctypes.windll.kernel32.OutputDebugStringW# class DbgViewHandler(logging.Handler):# def emit(self, record):# OutputDebugString(self.format(record))# log logging.getLogger(output.debug.string.example)# def config_logging():# # format# fmt logging.Formatter(fmtRecvEventTest %(asctime)s.%(msecs)03d [%(thread)5s] %(levelname)-8s %(funcName)-20s %(lineno)d %(message)s, datefmt%Y:%m:%d %H:%M:%S)# log.setLevel(logging.DEBUG)# # OutputDebugString\DebugView# ods DbgViewHandler()# ods.setLevel(logging.DEBUG)# ods.setFormatter(fmt)# log.addHandler(ods)# config_logging()defgetOutputParam(): definition output params,this params will be displayed on interface of VM : return:the dict of params,the key value is parameter name,the length of parameter name does not exceed 32. the value of dict is parameter type.the parameter type only support stringintfloat. egdefinition three params 获取输出参数 : return:返回输出参数字典字典的key值为输出参数名称大小不超过32个字符长度 value值为类型只支持stringintfloat,byte四种类型 示例定义三个输出参数 params{}params[outPut1]intparams[outPut2]floatparams[outPut3]stringparams[outPut4]bytereturnparamsdefhandleMessage(info): 处理数据函数 param info: 待处理的数据块数据类型为bytes return: 输出参数为列表其中的数据和getOutputParam中的数据顺序一致且数据类型要一致 示例: #分割出数据并分别对应getOutputParam的类型然后把数据按照顺序列表输出 #log.debug(start handlemessage)returnlist[]splitdata#.encode()#分割符ifinfo[len(info)-1]!splitdata:infosplitdata#最后一个数据不是分割符人为增加一个nCountinfo.count(splitdata)#计算分割后数据个数strinfo.split(splitdata)#进行数据分割fornIndexinrange(nCount):ifnIndex0:returnlist.append(struct.pack(i,int(bytes.decode(str[nIndex]))))#根据getOutputParam的数据得知第一个数据为int,先把bytes转为str,再转为int分割后的数据存入listelifnIndex1:returnlist.append(struct.pack(f,float(bytes.decode(str[nIndex]))))elifnIndex2:returnlist.append(str[nIndex])elifnIndex3:returnlist.append(str[nIndex])returnreturnlist2.1 getOutputParam() defgetOutputParam(): definition output params,this params will be displayed on interface of VM : return:the dict of params,the key value is parameter name,the length of parameter name does not exceed 32. the value of dict is parameter type.the parameter type only support stringintfloat. egdefinition three params 获取输出参数 : return:返回输出参数字典字典的key值为输出参数名称大小不超过32个字符长度 value值为类型只支持stringintfloat,byte四种类型 示例定义三个输出参数 params{}params[outPut1]intparams[outPut2]floatparams[outPut3]stringparams[outPut4]bytereturnparams1.getOutputParam() 创建输出列表函数。定义事件输出内容及数据类型2.return返回输出参数字典 字典的key值为输出参数名称 大小不超过32个字符长度3.value值为数据类型只支 持stringintfloat,byte四种类型2.2 handleMessage(info):defhandleMessage(info): 处理数据函数 param info: 待处理的数据块数据类型为bytes return: 输出参数为列表其中的数据和getOutputParam中的数据顺序一致且数据类型要一致 示例: #分割出数据并分别对应getOutputParam的类型然后把数据按照顺序列表输出 #log.debug(start handlemessage)returnlist[]splitdata#.encode()#分割符ifinfo[len(info)-1]!splitdata:infosplitdata#最后一个数据不是分割符人为增加一个nCountinfo.count(splitdata)#计算分割后数据个数strinfo.split(splitdata)#进行数据分割fornIndexinrange(nCount):ifnIndex0:returnlist.append(struct.pack(i,int(bytes.decode(str[nIndex]))))#根据getOutputParam的数据得知第一个数据为int,先把bytes转为str,再转为int分割后的数据存入listelifnIndex1:returnlist.append(struct.pack(f,float(bytes.decode(str[nIndex]))))elifnIndex2:returnlist.append(str[nIndex])elifnIndex3:returnlist.append(str[nIndex])returnreturnlist1.handleMessage(info):数据解析函数2.param info:待处理的数据块 数据类型为bytes3.return:输出数据列表其中的数据和getOutputParam中的数据顺序一致且数据类型要一致handleMessage(info):中对接收的数据进行解析。1).解析结果满足要求会在VM数据结果一栏显示解析结果 并上传接收事件用于全局触发使用2).解析失败数据结果一栏不显示且不会上传事件3.发送事件-脚本功能描述配合发送数据模块使用对组装列表中的数据进行自定义组装后发送到外部设备。VM4.2.0发送事件-脚本示例路径\VisionMaster4.2.0\Applications\VmModuleProxy\x64\SendEventTest.pyVM4.4.0发送事件-脚本示例路径\VisionMaster4.4.0\Applications\ModuleProxy\x64\SendEventTest.pyimportstructimportloggingimportctypes######################### 此协议的输入数据由getInputParam产生最终输出数据为数据块######################### output logging messages to DbgView via OutputDebugString (Windows only!)# OutputDebugString ctypes.windll.kernel32.OutputDebugStringW# class DbgViewHandler(logging.Handler):# def emit(self, record):# OutputDebugString(self.format(record))# log logging.getLogger(output.debug.string.example)# def config_logging():# format# fmt logging.Formatter(fmtSendEventTest %(asctime)s.%(msecs)03d [%(thread)5s] %(levelname)-8s %(funcName)-20s %(lineno)d %(message)s, datefmt%Y:%m:%d %H:%M:%S)# log.setLevel(logging.DEBUG)# OutputDebugString\DebugView# ods DbgViewHandler()# ods.setLevel(logging.DEBUG)# ods.setFormatter(fmt)# log.addHandler(ods)# config_logging()defgetInputParam(): definition output params,this params will be displayed on interface of VM : return:the dict of params,the key value is parameter name,the length of parameter name does not exceed 32. the value of dict is parameter type.the parameter type only support stringintfloat. egdefinition three params 获取输出参数 : return:返回输出参数字典字典的key值为输出参数名称大小不超过32个字符长度 value值为类型只支持stringintfloat,byte4种类型 示例定义三个输出参数 params{}params[SendoutPut1]intparams[SendoutPut2]floatparams[SendoutPut3]stringparams[SendoutPut4]bytereturnparamsdefhandleMessage(list):#此Demo是把输入数据全部加1后进行输出,各个数据用#分割,最终此接口输出一个数据块returnbufbnIndex0forinfoinlist:ifnIndex0:#输入数据的顺序和getInputParam的数据顺序一致returnbufstruct.pack(i,info1)elifnIndex1:returnbufstruct.pack(f,info1.0)elifnIndex2:returnbufvmtest.encode()elifnIndex3:returnbufinfo returnbufb\xffreturnbuf#.encode()nIndex1#loginfo returnbuf.hex()#log.error(%s,loginfo)returnreturnbuf3.1 getInputParam() defgetInputParam(): definition output params,this params will be displayed on interface of VM : return:the dict of params,the key value is parameter name,the length of parameter name does not exceed 32. the value of dict is parameter type.the parameter type only support stringintfloat. egdefinition three params 获取输出参数 : return:返回输出参数字典字典的key值为输出参数名称大小不超过32个字符长度 value值为类型只支持stringintfloat,byte4种类型 示例定义三个输出参数 params{}params[SendoutPut1]intparams[SendoutPut2]floatparams[SendoutPut3]stringparams[SendoutPut4]bytereturnparams1.getInputParam()创建输入列表2.return返回输入参数字典字典的key值为输出参数名称大小不超过32个字符长度3.value值为数据类型只支持stringintfloat,byte四种类型3.2 handleMessage(list):defhandleMessage(list):#此Demo是把输入数据全部加1后进行输出,各个数据用#分割,最终此接口输出一个数据块returnbufbnIndex0forinfoinlist:ifnIndex0:#输入数据的顺序和getInputParam的数据顺序一致returnbufstruct.pack(i,info1)elifnIndex1:returnbufstruct.pack(f,info1.0)elifnIndex2:returnbufvmtest.encode()elifnIndex3:returnbufinfo returnbufb\xffreturnbuf#.encode()nIndex1#loginfo returnbuf.hex()#log.error(%s,loginfo)returnreturnbuf1.handleMessage(list):数据组装函数。对发送数据里面发送的多个数据进行处理组装后返回。2.param list:需要发送的数据通过发送数据模块输入。输入数据的顺序和getInputParam的数据顺序一致3.return:返回组装的数据块最后VM通过通信设备发出去4. 协议解析-脚本功能描述对传入协议解析模块的数据进行解析后输出供后续模块订阅使用。协议解析-脚本示例程序在模块DLL路径下可通过两种方式进入1.在VM流程中选中协议解析模块CtrlM快捷方式可直接进入模块DLL所在文件夹2.直接浏览VM安装包路径VisionMaster4.4.0\Applications\Module(sp)\x64\Communication\DataAnalysisModule\Receive.pyimportstructimportloggingimportctypes######################### 此协议的输入数据由getInputParam产生最终输出数据为数据块######################### output logging messages to DbgView via OutputDebugString (Windows only!)# OutputDebugString ctypes.windll.kernel32.OutputDebugStringW# class DbgViewHandler(logging.Handler):# def emit(self, record):# OutputDebugString(self.format(record))# log logging.getLogger(output.debug.string.example)# def config_logging():# format# fmt logging.Formatter(fmtSendEventTest %(asctime)s.%(msecs)03d [%(thread)5s] %(levelname)-8s %(funcName)-20s %(lineno)d %(message)s, datefmt%Y:%m:%d %H:%M:%S)# log.setLevel(logging.DEBUG)# OutputDebugString\DebugView# ods DbgViewHandler()# ods.setLevel(logging.DEBUG)# ods.setFormatter(fmt)# log.addHandler(ods)# config_logging()defgetInputParam(): definition output params,this params will be displayed on interface of VM : return:the dict of params,the key value is parameter name,the length of parameter name does not exceed 32. the value of dict is parameter type.the parameter type only support stringintfloat. egdefinition three params 获取输出参数 : return:返回输出参数字典字典的key值为输出参数名称大小不超过32个字符长度 value值为类型只支持stringintfloat,byte4种类型 示例定义三个输出参数 params{}params[SendoutPut1]intparams[SendoutPut2]floatparams[SendoutPut3]stringparams[SendoutPut4]bytereturnparamsdefhandleMessage(list):#此Demo是把输入数据全部加1后进行输出,各个数据用#分割,最终此接口输出一个数据块returnbufbnIndex0forinfoinlist:ifnIndex0:#输入数据的顺序和getInputParam的数据顺序一致returnbufstruct.pack(i,info1)elifnIndex1:returnbufstruct.pack(f,info1.0)elifnIndex2:returnbufvmtest.encode()elifnIndex3:returnbufinfo returnbufb\xffreturnbuf#.encode()nIndex1#loginfo returnbuf.hex()#log.error(%s,loginfo)returnreturnbuf4.1 getOutputParam()defgetOutputParam(): definition output params,this params will be displayed on interface of VM : return:the dict of params,the key value is parameter name,the length of parameter name does not exceed 32. the value of dict is parameter type.the parameter type only support stringintfloat. egdefinition three params 获取输出参数 : return:返回输出参数字典字典的key值为输出参数名称大小不超过32个字符长度 value值为类型只支持stringintfloat三种类型 示例定义三个输出参数 params{}params[outPut1]intparams[outPut2]floatparams[outPut3]stringreturnparams1.getOutputParam()获取输出参数2.return:返回输出参数字典字典的key值为输出参数名称大小不超过32个字符长度3.value值为数据类型类型只支持stringintfloat三种类型4.2 handleMessage(info)defhandleMessage(info): unpack message param info: hex string eg: 0000006400000012 return: dict. the key value is parameter name, the value of dict is parameter value eg: info 0000006400000064 two int params build up hex data string 处理数据函数 param info: 16进制字符串 return: 输出参数字典key为getOutputParam函数中定义过的参数名称value为具体值 示例: 传入的参数为两个int二进制:00 00 00 64 00 00 00 64 # hex data string convert to hex datainfobytearray.fromhex(info)params{}outDatastruct.unpack(ii,info)params[outPut1]outData[0]params[outPut2]outData[1]returnparams1.handleMessage(info)处理数据函数2.param info:16进制字符串3.return:输出参数字典key为getOutputParam函数中定义过的参数名称4.Value值未具体输出参数解析出来的值5.通讯脚本案例介绍5.1 接收事件-脚本示例介绍实现功能使用TCP通信发送字符串给VMVM接收事件通过脚本将接收到的字符串解析出int、float、string、bytes四个数据并通过事件解析清空触发运行1运行通过解析出的int值控制流程中特征匹配模块的查找个数5.1.1 创建TCP通信连接5.1.2 创建接收事件-脚本5.1.3 使用PyCharm或者Notepad编写脚本内容1.在getOutputParam()修改编写需要输出的参数和类型。2.在handleMessage(info)中对接收的info数据进行解析。在本示例中通过“#”对字符分割后按顺序复制给getOutputParam()中定义的参数如下面的流程所示5.1.4 配置全局触发5.1.5 效果展示5.2 发送事件-脚本示例介绍实现功能将发送数据模块绑定的数据全部1后使用TCP通信发送给外部设备5.2.1 创建TCP通信连接在通信管理-通信设备中建立TCP连接5.2.2 创建发送事件-脚本5.2.3 编写发送事件-脚本代码1.getInputParam():在这个函数中编写发送事件的参数列表2.handleMessage(list):在这个函数中对参数列表中的数据进行处理组装5.2.4 添加数据模块在流程中添加发送数据模块绑定发送事件-脚本运行流程发送数据6.补充资料1.python3 教程文档2.python3 标准库示例教程