这是一个我做过的 modbus rtu采集脚本 格式有点不规范(下位机的原因)
但是可以给你参考一下 我删除了一些定义数据对象的内容
!ArrayResize(SendByteArr,6)
SendByteArr[1] = &H01
SendByteArr[2] = &H03
SendByteArr[3] = &H00
SendByteArr[4] = &H6B
nReturn = !SvrByteArrayModbusCRC(SendByteArr,1,4)
SendByteArr[5] = !BitAnd(nReturn,&H000000FF)
nTmp = !BitAnd(nReturn,&H0000FF00)
SendByteArr[6] = !BitRShift(nTmp ,8)
‘发送和接收数据按如下方式:
!ArrayResize(RecByteArr,9)
nReturn = !DevWriteAndReadByteArr(SendByteArr, 6, RecByteArr, 9, 通讯延时)
if nReturn <> 9 then
!SetSingleChannelValueByName("通讯状态", 1)
exit
endif
nIndex = 0
!ArrayResize(DataByteArr,1)
while nIndex < 7
DataByteArr[1] = RecByteArr[nIndex*1+1]
nReturn = !SvrGetByteFromByteArr(DataByteArr,1,开关数据,0)
数值数据 = 开关数据
!SetSingleChannelValueByNum(nIndex+nChlIndex+2,数值数据)
nIndex = nIndex + 1
endwhile
nChlIndex = nChlIndex + nIndex‘设置通讯标志,解析都正确
!SetSingleChannelValueByName("通讯状态", 0)
!SetSingleChannelValueByName("AI01",RecByteArr[1] )
!SetSingleChannelValueByName("AI02",RecByteArr[2] )
!SetSingleChannelValueByName("AI03",RecByteArr[3] )
!SetSingleChannelValueByName("AI04",RecByteArr[4] )
!SetSingleChannelValueByName("AI05",RecByteArr[5] )
!SetSingleChannelValueByName("AI06",RecByteArr[6] )
!SetSingleChannelValueByName("AI07",RecByteArr[7] )