...
#! /usr/bin/python
# coding=utf-8
import sys
import struct
import os
sys.path.append(os.path.abspath('.') + '\\..\\..')
sys.path.append(os.path.abspath('.') + '\\..\\..\\python')
import pydriver
def InitDriver(pkDriver):
print "----InitDriver----"
print(pkDriver)
return 0
def UnInitDriver(pkDriver):
print "----UnInitDriver----"
print(pkDriver)
return 0
def InitDevice(pkDevice):
print "----InitDevice----"
print(pkDevice)
tags=[]
for tag in pkDevice['tags']:
tags.append(tag)
timerObj=pydriver.CreateTimer(pkDevice, 3000, tags)
return 0
def UnInitDevice(pkDevice):
print "UnInitDevice"
return 0
def OnTimer(pkDevice, timerObj, pkTimerParam):
print "----OnTimer----"
tags = pkTimerParam
print timerObj
print tags
id = 1
tag="100"
version = 2
count = 3
buffer = struct.pack("!H4s2I", id, tag, version, count)
print "before send..."
result = pydriver.Send(pkDevice, buffer, 2000)
print "SendToDevice:",result
if(result <= 0):
print "send to device return <-0"
print "send to device:", len(buffer) , "return :" , result
return
retCode, bufferRecv = pydriver.Recv(pkDevice, 10000, 200)
print "Recv,retcode:", retCode, ",length:",len(bufferRecv)
if(retCode != 0):
return
# 解析数据,得到值
tagValue =struct.unpack("!H", bufferRecv)
print "recv tagValue:",tagValue[0]
#print "recv,id:",id,",tag:",tag,",version:",version,",count:",count
tagAddr = "AI:10"
print tagAddr
tagList = pydriver.GetTagsByAddr(pkDevice, tagAddr)
pydriver.SetTagsValue(pkDevice,tagList, str(tagValue[0])) #传入的值必须转化为字符串类型!!!!!!
pydriver.UpdateTagsData(pkDevice, tagList)
print "-OnTimer- end"
return 0
def OnControl(pkDevice, pkTag, strTagValue):
print "-!--OnControl--!-"
print pkDevice,pkTag
print "device name:",pkDevice['name'], "tagname:", pkTag['name'], "tagaddress:",pkTag['address'], "tagvalue:",strTagValue
return 0
#仅供调试使用,可以直接运行这个python文件进行调试
if __name__ == '__main__':
pkDriver={'name': '', '_InternalRef': 0, 'param4': '', 'param3': '', 'param2': '', 'param1': '', 'type': '', 'desc': ''}
pkDevice={'recvtimeout': 500, 'name': 'pytestdevice', 'tags': [{'_ctag': 7981384, 'name': 'CELL06_BLD1', 'lenbits': 16, 'datatype': 'uint16', 'address': 'AI:17023', 'pollrate': 3000}, {'_ctag': 7983144, 'name': 'CELL06_BLD2', 'lenbits': 16, 'datatype': 'uint16', 'address': 'AI:17023', 'pollrate': 3000}, {'_ctag': 7984904, 'name': 'CELL06_OKjishu', 'lenbits': 16, 'datatype': 'uint16', 'address': 'AI:17007', 'pollrate': 3000}, {'_ctag': 7986664, 'name': 'CELL06_NGjishu', 'lenbits': 16, 'datatype': 'uint16', 'address': 'AI:17036', 'pollrate': 3000}], '_InternalRef': 7947760, 'driver': {'name': '', '_InternalRef': 0, 'param4': '', 'param3': '', 'param2': '', 'param1': '', 'type': '', 'desc': ''}, 'conntype': 'tcpclient', 'param4': '', 'param3': '', 'param2': '', 'param1': '', 'conntimeout': 3000, 'connparam': 'ip=127.0.0.1;port=502', 'desc': ''}
InitDriver(pkDriver)
InitDevice(pkDevice)
...
tagList[输入参数]:变量列表,可能是pydriver.GetTagsByAddr返回的值
strTagValue[输入参数]:字符串类型的变量值,如”1000”
返回值:返回0表示成功
6.3 更新一组变量数据
pydriver.UpdateTagsData(pkDevice, tagList)
...