I was trying to replicate the 2-register Modbus long integer behavior by constructing a 32-bit integer from two 16-bit words.
[CR-6 Firmware: CR6.Std.12.00 CR6-WIFI.05.03]
Example:
High Word: 100 0x0064 00000000 01100100
Low Word: 200 0x00C8 00000000 11001000
Combined: 6553800 0x006400C8 00000000 01100100 00000000 11001000
We can quickly code up a function to handle this conversion:
Public highword As Long, lowword As Long, test As Long Function highlow(hi As Long, lo As Long) Dim holder As Long holder = 0 MoveBytes (holder,0,hi,2,2,0) MoveBytes (holder,2,lo,2,2,0)
'or holder += lo Return holder End Function 'Main Program BeginProg Scan (1,Sec,3,0) test = highlow(highword, lowword) NextScan EndProg
This seems to work well, but falls apart on larger integer words:
Broken Example:
High Word: 20000 0x4E20 01001110 00100000 Low Word: 11467 0x2CCB 00101100 11001011 Combined: 1310731467 0x4E202CCB 01001110 00100000 00101100 11001011
However, function returns: 1310731520 0x4E202D00 01001110 00100000 00101101 00000000
Difference:
01001110 00100000 00101100 11001011 - 1310731467
01001110 00100000 00101101 00000000 - 1310731520
01001110 00100000 0010110X XXXXXXXX - -53
This fails on the last 9 or 10 bits. And it happens whether I move bytes, add the low word to the shifted high word, or initially assign the value of holder to the low word.
This is massively confusing as 20000 shifted 16 bits left = 1310720000 and the logger can handle the calculation of 1310720000 + 11467 correctly. Why is it missing on:
holder = 0 MoveBytes (holder,0,hi,2,2,0) holder += lo
Thanks!