Hello Guest it is March 28, 2024, 09:09:40 AM

Author Topic: Modbus BitOfWord (Bit Packing/Unpacking) for VB/Brains/Screens/PLCs(etc.)  (Read 7050 times)

0 Members and 1 Guest are viewing this topic.

Offline poppabear

*
  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
'Bit of Word or Bit Packing/unpacking VB function for Modbus transfer

'Pick the "Start" OEM number for the UserLEDs you want to use,
'the function will use the start ULED number, plus the next 15 ULEDs a
'after that, so the 16 ULEDs in that range must not be used by anything else.
'For instance, if you pass 2001 (for ULED2001), to the function
'then the range of ULED 2001-2016 will be packed into a UDRO
'Of your choice, and you use that UDRO in your Brain to send out to
'Your PLC or whatever your using that can use BitOfWord registers.
'So you would also pass the OEM number of the DRO that you want
'to pack bits into. UnPacking works the same way, but in reverse, it unpacks
'a UserDRO into 16 (in series) Uleds..

'****This could also be done in wxLua as well for M4 just use the Lua equivs*****

'//////////////////////////////////
'       There is a screen Set (mill) for testing Bit packing and unpacking attached.
'   it is called: "1024BitPackUnPackTest.set"
'   Load this screen in your testing profile, when it loads goto the Diag. page
'   Once there, you will see a large button toward the bottom corner of the toolpath
'   window its label says "Go To Bit Pack/Unpack test page"
'   There is a columb of ULEDs, you can turn them on off as you like
'   they are listed as to what Bit in a 16 bit word they represent
'   once you have picked them, push the "Pack this UDRO >>" button
'   you will see it will populate the UDRO with the Decimal equiv.
'   To go the other way, just put in a decimal number and press the
'   button called "UnPack This UDRO <<" it will take the decimal value
'   and populate the ULEDS in what would be their Bit Binary pattern
'      Obviously this could be used to send and receive BitPacked UINTs to/from
'   Brains across to/from a PLC or the like. To handle discreate I/O
'   The functions are set up where you can pick your own ULED ranges,
'   and UDROs

'Enjoy Scott Shafer!!

'the below scripts are in the Pack and UnPack buttons on the test screen

'/////////////  Pack 16bit UINT Button, Script or Macro example //////////////////

Call Pack16BitWord(2001, 2000)'start at ULED 2001, and PackDro UDRO 2000

Function Pack16BitWord( ULedOEMstartNum, UDroForPacking ) As Integer
   pUledCounter    = 0
   pDroValue       = 0
   
   For SixteenBit  = 1 To 16   
      If(pUledCounter = 0) Then
         If(GetUserLED(ULedOEMstartNum)) Then
            pDroValue = pDroValue + 1
         End If
      End If   
      
      If(pUledCounter > 0) then
         If(GetUserLED(ULedOEMstartNum + pUledCounter)) Then
            pDroValue = pDroValue + (2^pUledCounter)      
         End If
      End If      
      pUledCounter = pUledCounter + 1      
   Next SixteenBit   
   SetUserDRO(UDroForPacking, pDroValue)   
End Function

'/////////////  UnPack 16bit UINT Button, Script or Macro example //////////////////

Call UnPack16BitWord(2001, 2001)'start at ULED 2001, and PackDro UDRO 2001

Function UnPack16BitWord( ULedOEMstartNum, UDroForUnPacking ) As Integer
   Dim BinaryArray(16) As Integer
   WhileLoopCounter       = 0   
   PackedDroValue          = Int(GetUserDRO(UDroForUnPacking))   
   LeadingZeros          = 0
   
   While PackedDroValue >= 1
      If(PackedDroValue Mod 2) Then            
         BinaryArray(WhileLoopCounter) = 1   
      Else
         BinaryArray(WhileLoopCounter) = 0
      End If      
      PackedDroValue = Int(PackedDroValue/2)
      WhileLoopCounter = (WhileLoopCounter + 1)      
   Wend   
   
   For ArrayLoop = 0 To WhileLoopCounter
      ArrayElementValue = BinaryArray(ArrayLoop)
      SetUserLED((ULedOEMstartNum + ArrayLoop), ArrayElementValue)      
   Next ArrayLoop
   
   LeadingZeros = (16 - WhileLoopCounter)
   For RemainingNeedToZeroBits = 0 To LeadingZeros
      SetUserLED((ULedOEMstartNum + WhileLoopCounter + RemainingNeedToZeroBits), 0)
   Next RemainingNeedToZeroBits   
End Function

fun times

Offline simpson36

*
  •  1,369 1,369
    • View Profile

'****This could also be done in wxLua as well for M4 just use the Lua equivs*****


. . . .   use the Lua equivalents  ???

Basically it seems you are passing UserLED data thru a brain. That's what I do now (sans bit packing). There is no equivalent to that in MACH4 unless I have missed something really obvious.

 

 

Offline poppabear

*
  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
Simp.

the example code is for packing and unpacking a 16 bit word, to pass back and forth to an external MB device like a PLC or VFD etc.

The code concept can be used with Lua equiv, if you want to pack/unpack a register (or word) to tie into your screen, or other macros/scripts.

Scott
fun times

Offline simpson36

*
  •  1,369 1,369
    • View Profile
Simp.

the example code is for packing and unpacking a 16 bit word, to pass back and forth to an external MB device like a PLC or VFD etc.

The code concept can be used with Lua equiv, if you want to pack/unpack a register (or word) to tie into your screen, or other macros/scripts.

Scott

OK, I get it now.

FYI, in another thread, one of the developers stated that MACH4 has bit pack/unpack functions that will be included in the next release.

I may still klep your fine code for another project though . . .  >:D