B4R Question Type conversion issue

miker2069

Active Member
Licensed User
Longtime User
I'm having really strange results with the following. The result of an Infrared Receive contains a ULONG array of numbers. Along with that is the number of elements. I'm basically attempting to perform some simple math on each of the elements then copy that result to an object array. Ultimatley it's going to be seralized and sent over the network.

My code is something like this:

B4X:
    Private USECPERTICK As ULong
    Private MARK_EXCESS As ULong

    USECPERTICK = 50
    MARK_EXCESS = 100

 Dim code_array(100) As Object
    'init array of objects
    For i = 0 To 99
        code_array(i) = 0
    Next
  
    i = 0
    
    code_array(0) = Result.RawBuffer.Length 'storing length of IR codes in first element
    Dim code As ULong = 0
  
    'For Each code As ULong In Result.RawBuffer
    For i = 0 To Result.RawBuffer.Length - 1
        If ((i Mod 2) = 0) Then 'even
            'Space
            code_array(i+1)  = (Result.RawBuffer(i)  * USECPERTICK) + MARK_EXCESS
            'Log("space newcode: ", newcode)
        Else 'odd
            'Mark
          
            code_array(i+1)  = (Result.RawBuffer(i)  * USECPERTICK) - MARK_EXCESS
          
            'Log("mark newcode: ", newcode)
        End If
      
        'newcode = 4500
      
        'code_array(i) = newcode
      
        Log("First print: ", code_array(i+1))
    Next
  
    ir_recv.Disable
    Log("Received raw code. Infrared Disabled.")
    For i = 0 To 99
        Log("Second print: ", code_array(i))
    Next

I'm doing it this way as I understand it, in order to serialize and send (my stream is in prefixmode) I can only serialize an array of objects and not the array of ulongs directly. hence I copy to a fixed array of objects (in this example max elements = 100). The print line in the loop:
Log("First print: ", code_array(i+1))
Appears to print the correct result. After I've looped through everything and I loop through again in the second loop here:
B4X:
  For i = 0 To 99
        Log("Second print: ", code_array(i))
    Next

It doesn't seem to match up. I don't understand how it seems correct initially and w/o any modification outside the first loop it's off. Some seem correct others are not. Whatever is displayed in the second loop gets sent over the network and received in my B4J test program.

Now if I send the following fixed array of objects:
B4X:
dim my_array2() As Object = Array As Object(4450,4600,500,1800,500,1800,500,1800,500,700,500,700,500,700,500,700,500,650,550,1800,500,1800,500,1800,500,700,500,700,500,700,500,700,500,700,500,1800,500,1800,500,700,500)

I have no issues and it's received properly. Also I should mentioned, in the beginning of my code block I have to initialize the array of objects - if I don't I get total garbarge on the receiving end and it actually crashes.



What am I missing here with my type conversion and setup of my array of objects?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
Please update to rRandomAccessFile v1.90: https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.76644/#content

You should use the new ByteConverter.ObjectSet to set the values of object elements:
B4X:
bc.ObjectSet(code_array(i+1), (Result.RawBuffer(i) * USECPERTICK) + MARK_EXCESS)

Your current code sets the same compiler created object instance to all elements.

I updated to v1.9 however still not getting it to work....I tried a really simple program using the new version of RandomAccessFile and the new ObjectSet method:

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 2000
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public code_array(2) As Object
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
   
    Dim i As Int
    Dim bc As ByteConverter
    Dim code As Object
    Dim size1 As UInt = 2
   
    For i = 0 To 1
        bc.ObjectSet(code_array(i),100)
        Log("code_array(",i,")=",code_array(i))
    Next
   
End Sub

I get:
code_array(0)=
code_array(1)=

I made sure there wasn't any skew between copying the new version of the library over. Am I using it correctly?
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
looking a bit deeper, particularly at the method signature of ObjectSet, it looks like it's ObjectSet(Src Object, Dest Object). So my code is reversed...I believe the following works:

B4X:
bc.ObjectSet(100,code_array(i))

I will retry in my original program and let you know
 
Upvote 0
Top