【WINCC】16个Bit转Word

来源:互联网 发布:windows操作系统 编辑:程序博客网 时间:2024/06/11 17:38

一、在全局脚本里面新建两个函数:


代码如下:

''''''16个Bit转Word'''接收参数为包含16个元素的数组'''其中每个元素依次对应16个Bit中的一个Function bits2Word(Byval arr)Dim resres = 0If Not IsArray(arr) ThenMsgbox "arr is not a array."Elseif UBound(arr) <> 15 ThenMsgbox "the length of array must be 16."ElseDim x,y,idxFor idx = 0 To 15x = arr(idx)y = pow(2,(15 - idx))res = res + (x * y) NextEnd Ifbits2Word = resEnd Function'''辅助函数:求num的n次方Function pow(Byval num,Byval n)If n = 0 Thenpow = 1Elsepow = pow(num , n - 1) * numEnd IfEnd Function

二、在VBS脚本中调用:


代码调用实例如下:

Dim wordword = bits2Word(Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))Msgbox word



原创粉丝点击