发表于:2006-02-09 15:17:00
19楼
贴一个Modbus协议的规约驱动程序供大家讨论,其实这些东西在upcas中是完全开放的。
#main.py
#This is a test Device Driver Script
import user
DICount = 16
AICount = 8
SleepTime = 200
def ProcInit(slave):
user.SetSlaveDICount(slave, DICount)
user.SetSlaveAICount(slave, AICount)
return
def ProcA(slave):
SlaveAddr = 0
SettingList = user.GetSlaveSetting(slave)
if (len(SettingList) >= 2):
SlaveAddr = int(SettingList[1])
msg = ""
msg += user.Int2Char(SlaveAddr)
msg += user.Int2Char(0x02)
msg += user.Int2Char(0x00)
msg += user.Int2Char(0x00)
msg += user.Int2Char(DICount / 256)
msg += user.Int2Char(DICount % 256)
msg += user.MB_Crc(msg)
user.SlaveSend(slave, msg)
user.Sleep(SleepTime)
msg = user.SlaveReceive(slave)
nLen = len(msg)
if (nLen >= 5 and msg[nLen - 2 :] == user.MB_Crc(msg[0: nLen - 2])):
i = 0
while (i < DICount):
charValue = user.Char2Int(msg[3 + i / 8])
user.SetSlaveDI(slave, i, user.GetBit(charValue, i % 8))
i = i + 1
ProcC(slave)
return
def ProcB(slave):
SlaveAddr = 0
SettingList = user.GetSlaveSetting(slave)
if (len(SettingList) >= 2):
SlaveAddr = int(SettingList[1])
msg = ""
msg += user.Int2Char(SlaveAddr)
msg += user.Int2Char(0x03)
msg += user.Int2Char(0x00)
msg += user.Int2Char(0x00)
msg += user.Int2Char(AICount / 256)
msg += user.Int2Char(AICount % 256)
msg += user.MB_Crc(msg)
user.SlaveSend(slave, msg)
user.Sleep(SleepTime)
msg = user.SlaveReceive(slave)
nLen = len(msg)
if (nLen >= 5 and msg[nLen - 2 :] == user.MB_Crc(msg[0: nLen - 2])):
i = 0
while (i < AICount):
Value = user.Char2Int(msg[3 + 2 * i]) * 256 + user.Char2Int(msg[4 + 2 * i])
user.SetSlaveAI(slave, i, Value)
i = i + 1
return
def ProcC(slave):
command = user.GetSlaveCommand(slave)
if (len(command) < 3):
return
UserID = command[0]
index = int(command[1])
value = int(command[2])
SlaveAddr = 0
SettingList = user.GetSlaveSetting(slave)
if (len(SettingList) >= 2):
SlaveAddr = int(SettingList[1])
msg = ""
msg += user.Int2Char(SlaveAddr)
msg += user.Int2Char(0x05)
msg += user.Int2Char(index / 256)
msg += user.Int2Char(index % 256)
if (value):
msg += user.Int2Char(0xff)
else:
msg += user.Int2Char(0x00)
msg += user.Int2Char(0x00)
msg += user.MB_Crc(msg)
user.SlaveSend(slave, msg)
user.Sleep(SleepTime)
msg = user.SlaveReceive(slave)
nLen = len(msg)
return
def ProcD(slave):
return
def ProcFinalize(slave):
return