WinCC VBS convert 32 bit Integer to 16 bit

aikona

Member
Join Date
Oct 2003
Location
B2220 plaas dorpie
Posts
182
Hi
Small problem ,can't find solution .Inside a VBS script I need to extract the low 16 bits out of a 32 bit integer .In PLC lanquage its easy

L DB1.DBD2 // 32 bit word ie FCA2C4EF
T DB1.DBW6 // 16Bit word becomes C4EF

end of story .But in VBS I can't get it to work .In VBS I extract this 32bit value ... first as a string in a database , I then convert it to a integer and then I try to 'AND' this value with &H0000FFFF but it does not give the required result . The problem is that you cannot 'DIM' a variable as a 16bit INT.

Anybody with a solution ?

the vbs part
tempvar = rst.Fields(0).Value
HMIRuntime.Tags("SQL_LN01_OF_Nr").Write tempvar
intvar2 = CInt(tempvar)
intvar3 = &HFFFF
intvar = intvar2 And intvar3

but I also tried
intvar2 = CInt(tempvar) & &HFFFF

How can you tell a var that it is a 16bit var.

Thanks

Eric
 
Hi , Yes could be a solution , not very elegant ,would also mean to add another 160 odd tags ,and there is no way I am going to rewrite the complete script to C

anybody else with a solution , partial or insight



Eric
 
Jesper

I have a plc and a homemade PC program , these two exchange information
We plan to 'destroy' the PC and replace it with WinCC.Because both PC & PLC are in operation and critical ,I cannot do changes in the PLC or the PC.To test some of the WinCC stuff all I can do is to read tags from the PLC and Query the database.One of the functions in WinCC is to show the operator what products are in use ( info from database) and amount of products used ( info from PLC) I have one common item between PLC and PC but in the PC its a 32bits nr and in the PLC its two 16bit number.

So my first idea was to disregard the HIGH word on the 32 bit because we can never have 65000 products at the same time ,and word with the low WORD .But all the DIM's VBS are variants and somehow are mostly treated as string.

My second idea : If you can't split them join them.And still working on the "assume" everything is a string ,I came to a solution

Her is the very easy VBS code
Dim V_DB60_DBW2 ,V_DB60_DBW4,V_DB60_DBD2,V_Result,V_Result1,V_result2
Dim Vtemp


V_DB60_DBW4 = HMIRuntime.Tags("M08_DB60_DBW4").Read ' Read High word
V_DB60_DBW2 = HMIRuntime.Tags("M08_DB60_DBW2").Read ' Read Low word
'V_DB60_DBD2 = HMIRuntime.Tags("M08_DB60_DBD2").Read ' Read Double word
'V_Result = V_DB60_DBD2 Now this does'nt work at all

V_result1 =Hex(V_DB60_DBW4) & Hex(V_DB60_DBW2) ' convert to HEX and just 'Stick' the two together

V_result2 = CLng("&H" & V_result1)' convert the result back to a number
' the trick here is .... "&H" &

Vtemp = MsgBox("Show result " & V_DB60_DBW4 &" " & V_DB60_DBW2 &" " & V_result1 &" " & V_result2 , vbOKCancel)

Bye

Onto the next problem

Eric
 
Assuming data pointers are not available in VBScript, something like this might work. Cf. here.

N.B. Caveats

  • I did this off the top of my head, so test it before using it!
  • This does not handle special cases (±NAN, ±Inf).
  • It returns a double-precision float; I did that to avoid overflow and underflow issues; change the last statement to real2x16 = CFlt(real32) if you are not concerned about that.
Code:
Function real_2x16(hi16arg,lo16arg)
  Dim real32
  Dim hi16
  Dim lo16
  Dim exponent
  lo16 = CLng(lo16arg)
  hi16 = CLng(hi17Arg)
  IF lo16<>0 OR (hi16<>0 AND hi16<>-32768 AND hi16<>32768) THEN
    real32 = CDbl(lo16 AND &h00007FFF)
    IF (lo16 AND &h00008000) <> 0 THEN real32 = real32 + 32768.0
    real32 = real32 + CDbl(hi16 AND &h0000007F) * 65536.0
    exponent = ((hi16 AND &h00007F80) / &h00000080) - 127
    IF exponent > -127 THEN real32 = real32 + CDbl(&h00800000)
    real32 = real32 / CDbl(&h00800000)
    IF exponent <> 0 THEN real32 = real32 * (2.0 ^ exponent)
    IF (hi16 AND &h00008000) <> 0 THEN real32 = 0.0 - real32
  ELSE
    real2 = CDbl(0.0)
    IF hi16=-32768 THEN real2 = CDbl(-0.0)
  ENDIF
  real_2x16 = real32
END
 

Similar Topics

Hello, I just started learning WinCC V8.0 3 days ago, so I'm still new to how everything works in this program. I've created a pop-up with a...
Replies
2
Views
674
Hello everyone. I'm trying to export data to excel from Wincc. My code is global action that executed every second that create excel filer per...
Replies
1
Views
5,733
Hello all, I have written a VBS script to read a group of tags, and put the values and tag name etc into a text file when I press a button on one...
Replies
1
Views
1,877
Hi, In my Plant i have PCS7 (WinCC V6.0+SP4) System.( one ES/OS and One OS )... I need Shift Report (every 8 Hours ) using VBS script or Default...
Replies
1
Views
2,238
Hi I am trying to vary the number of decimal places via a script (MP377) Dim obj Set obj =...
Replies
0
Views
1,308
Back
Top Bottom