B4R Question Write data to public array

Mostez

Well-Known Member
Licensed User
Longtime User
Hello,
In part of software, user enters account data i.e. account id, card number and user name one step after another in separate subs, each sub should copy entered value to public array, this array then will be written to SD card file. everything goes fine, but I get only first step OK, other failed to copy data to public array. I tried byte converter and random access file, both gave me same result. for example if user entered account data as follows:

01234567891
ABC02001
Mostez
Desc 1
Desc 2

entered value tested OK, but I get only first copied value '01234567891' in public array, any ideas how to solve this problem.

Thanks

B4X:
private Sub AddUserAccount(KeyFromMenuHandler As String)
    MenuIndex = MENU_ADD_USER_ACCOUNT
    RFIDmode = RFID_MODE_ENTRY
    If KeyFromMenuHandler = KEY_NONE Then
        For m = 0 To UserAccountSettingsArray.Length -1 'reset user account settings array
            UserAccountSettingsArray(m) = Asc(" ") 'fill array with spaces
        Next
        NewEntry("Card Number:",10) 'title and max allowed char
        Return
    End If
    Dim Result As Byte = NumericEntry(KeyFromMenuHandler,False)
    If Result = KEY_ENTER Then
        Dim Cid(10) As Byte 'tmp array
        BC.ArrayCopy2(EntryBuffer,0,Cid,0,10) 'copy entry buffer to local tmp array
        BC.ArrayCopy2(Cid,0,UserAccountSettingsArray,0,10) 'copy tmp array to public user account array
        UserAccountSettingsArray(10) = TAB
       
'        Dim raf As RandomAccessFile
'        raf.Initialize(UserAccountSettingsArray,False)
'        raf.CurrentPosition = 0
'        raf.WriteBytes(Cid,0,10,raf.CurrentPosition)
'        raf.WriteByte(TAB,raf.CurrentPosition) 'TAB delimeter
'        Log(BC.StringFromBytes(Cid))
        EnterUserAccountID(KEY_NONE) 'goto next step
    Else if Result = KEY_CANCEL Then
        RFIDmode = RFID_MODE_NONE
        LCD.Clear
        MenuUserAccount(KEY_NONE)
    End If   
End Sub

private Sub EnterUserAccountID(KeyFromMenuHandler As String)
    MenuIndex = MENU_ENTER_USER_ACCOUNT_ID
    RFIDmode = RFID_MODE_NONE
    If KeyFromMenuHandler = KEY_NONE Then
        NewEntry("Account ID:",8)
        Return
    End If
    Dim Result As Byte = AlphaNumericEntry(KeyFromMenuHandler,False,ENTRY_ID_TYPE)
    If Result = KEY_ENTER Then
        Dim Cid(8) As Byte
        BC.ArrayCopy2(EntryBuffer,0,Cid,0,8)
        BC.ArrayCopy2(Cid,0,UserAccountSettingsArray,11,8)
        UserAccountSettingsArray(27) = TAB
'        Dim raf As RandomAccessFile
'        raf.Initialize(UserAccountSettingsArray,False)
'        raf.CurrentPosition = 11
'        raf.WriteBytes(Cid,0,8,raf.CurrentPosition)
'        raf.WriteByte(TAB,raf.CurrentPosition) 'TAB delimeter
'        Log(BC.StringFromBytes(Cid))
        EnterUserAccountDescription1(KEY_NONE) 'goto next step
    Else if Result = KEY_CANCEL Then
        RFIDmode = RFID_MODE_NONE
        LCD.Clear
        MenuUserAccount(KEY_NONE)
    End If   
End Sub

private Sub EnterUserAccountDescription1(KeyFromMenuHandler As String)
    MenuIndex = MENU_ADD_USER_ACCOUNT_DESCRIPTION_1
    RFIDmode = RFID_MODE_NONE
    If KeyFromMenuHandler = KEY_NONE Then
        NewEntry("User Name",16)
        Return
    End If
    Dim Result As Byte = AlphaNumericEntry(KeyFromMenuHandler,False,ENTRY_ALPHANUMERIC)
    If Result = KEY_ENTER Then
        Dim Cid(16) As Byte
        BC.ArrayCopy2(EntryBuffer,0,Cid,0,16)
        BC.ArrayCopy2(Cid,0,UserAccountSettingsArray,28,16)
        UserAccountSettingsArray(48) = TAB
'        Dim raf As RandomAccessFile
'        raf.Initialize(UserAccountSettingsArray,False)
'        raf.CurrentPosition = 28
'        raf.WriteBytes(Cid,0,16,raf.CurrentPosition)
'        raf.WriteByte(TAB,raf.CurrentPosition) 'TAB delimeter
'        Log(BC.StringFromBytes(Cid))
        EnterUserAccountDescription2(KEY_NONE)
    Else if Result = KEY_CANCEL Then
        RFIDmode = RFID_MODE_NONE
        LCD.Clear
        MenuUserAccount(KEY_NONE)
    End If   
End Sub

private Sub EnterUserAccountDescription2(KeyFromMenuHandler As String)
    MenuIndex = MENU_ADD_USER_ACCOUNT_DESCRIPTION_2
    RFIDmode = RFID_MODE_NONE
    If KeyFromMenuHandler = KEY_NONE Then
        NewEntry("Profession:",16)
        Return
    End If
    Dim Result As Byte = AlphaNumericEntry(KeyFromMenuHandler,False,ENTRY_ALPHANUMERIC)
    If Result = KEY_ENTER Then
        Dim Cid(16) As Byte
        BC.ArrayCopy2(EntryBuffer,0,Cid,0,16)
        BC.ArrayCopy2(Cid,0,UserAccountSettingsArray,49,16)
        UserAccountSettingsArray(69) = TAB
'        Dim raf As RandomAccessFile
'        raf.Initialize(UserAccountSettingsArray,False)
'        raf.CurrentPosition = 49
'        raf.WriteBytes(Cid,0,16,raf.CurrentPosition)
'        raf.WriteByte(TAB,raf.CurrentPosition) 'TAB delimeter
'        MsgBox("Account Created","","",MSGBOX_INF,1000)
'        Log(BC.StringFromBytes(Cid))
        Log(BC.StringFromBytes(UserAccountSettingsArray))
        MenuUserAccount(KEY_NONE)
       
    Else if Result = KEY_CANCEL Then
        RFIDmode = RFID_MODE_NONE
        LCD.Clear
        MenuUserAccount(KEY_NONE)
    End If   
End Sub
 

Mostez

Well-Known Member
Licensed User
Longtime User
OK, now I can store and restore arrays to\from global store. but when I try to copy restored arrays to local array(to pass it to CreateUserAccount sub), i get same old result!! all I want is to pass 101 bytes array to CreateUserAccount sub

B4X:
Dim o() As Byte
        Dim tmp(101) As Byte
        For m = 0 To tmp.Length -1 'reset user account array
            tmp(m) = Asc(" ")
        Next
       
        Dim RAF As RandomAccessFile
        RAF.Initialize(tmp,False)
       
        o = GlobalStore.Objects(0)
        RAF.WriteBytes(o,0,o.Length,0)
       
        o = GlobalStore.Objects(1)
        RAF.WriteBytes(o,0,o.Length,11)
       
        o = GlobalStore.Objects(2)
        RAF.WriteBytes(o,0,o.Length,28)
       
        o = GlobalStore.Objects(3)
        RAF.WriteBytes(o,0,o.Length,49)

B4X:
private Sub CreateUserAccount(AccountData() As Byte) As Boolean
    Dim CardNumberArray(8) As Byte
    BC.ArrayCopy2(AccountData,2,CardNumberArray,0,8)
    Dim IDCardNumber As String = BC.StringFromBytes(CardNumberArray)
    Dim ufn As String = GetUserAccountFileName(IDCardNumber)
    Dim IsExist As Boolean = IsUserAccountExist(IDCardNumber)
    If IsExist = True Then
        If Sd.Remove(ufn) = False Then Return False
    End If
    If Sd.OpenReadWrite(ufn) = False Then Return False
    Sd.Position = Sd.CurrentFile.Size
    Sd.Stream.WriteBytes(AccountData,0,AccountData.Length)
    Sd.Close
    Return True
End Sub
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
tmp array passed to CreateUserAccount sub

B4X:
Dim o() As Byte
        Dim tmp(101) As Byte
        For m = 0 To tmp.Length -1 'reset user account array
            tmp(m) = Asc(" ")
        Next
      
        Dim RAF As RandomAccessFile
        RAF.Initialize(tmp,False)
      
        o = GlobalStore.Objects(0)
        RAF.WriteBytes(o,0,o.Length,0)
      
        o = GlobalStore.Objects(1)
        RAF.WriteBytes(o,0,o.Length,11)
      
        o = GlobalStore.Objects(2)
        RAF.WriteBytes(o,0,o.Length,28)
      
        o = GlobalStore.Objects(3)
        RAF.WriteBytes(o,0,o.Length,49)
       Log(BC.StringFromBytes(tmp)) 'only first copied item appears
        CreateUserAccount(tmp)
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
stack size is 1.5K, Log statement is only for debugging and will be removed and OK i will fix RAF.current position, here is the code that stores arrays in the store

B4X:
private Sub AddUserAccount(KeyFromMenuHandler As String)
    MenuIndex = MENU_ADD_USER_ACCOUNT
    RFIDmode = RFID_MODE_ENTRY
    If KeyFromMenuHandler = KEY_NONE Then
        NewEntry("Card Number:",10,Asc(" ")) 'title and max allowed char
        Return
    End If
    Dim Result As Byte = NumericEntry(KeyFromMenuHandler,False)
    If Result = KEY_ENTER Then 'if enter then copy entry buffer to tmp array and store it in global store slot 0
        Dim Cid(10) As Byte
        BC.ArrayCopy2(EntryBuffer,0,Cid,0,10)
        GlobalStore.Store(0,Cid)

        EnterUserAccountID(KEY_NONE) 'goto next step, enter account ID
    Else if Result = KEY_CANCEL Then 'if cancelled then return to previous menu
        RFIDmode = RFID_MODE_NONE
        LCD.Clear
        MenuUserAccount(KEY_NONE)
    End If   
End Sub

private Sub EnterUserAccountID(KeyFromMenuHandler As String)
    MenuIndex = MENU_ENTER_USER_ACCOUNT_ID
    RFIDmode = RFID_MODE_NONE
    If KeyFromMenuHandler = KEY_NONE Then
        NewEntry("Account ID:",8,Asc(" "))
        Return
    End If
    Dim Result As Byte = AlphaNumericEntry(KeyFromMenuHandler,False,ENTRY_ID_TYPE)
    If Result = KEY_ENTER Then
        Dim Cid(16) As Byte
        BC.ArrayCopy2(EntryBuffer,0,Cid,0,16)
        GlobalStore.Store(1,Cid)
       
        EnterUserAccountDescription1(KEY_NONE)
    Else if Result = KEY_CANCEL Then
        RFIDmode = RFID_MODE_NONE
        LCD.Clear
        MenuUserAccount(KEY_NONE)
    End If   
End Sub

private Sub EnterUserAccountDescription1(KeyFromMenuHandler As String)
    MenuIndex = MENU_ADD_USER_ACCOUNT_DESCRIPTION_1
    RFIDmode = RFID_MODE_NONE
    If KeyFromMenuHandler = KEY_NONE Then
        NewEntry("Account Name:",16,Asc(" "))
        Return
    End If
    Dim Result As Byte = AlphaNumericEntry(KeyFromMenuHandler,False,ENTRY_ALPHANUMERIC)
    If Result = KEY_ENTER Then
        Dim Cid(16) As Byte
        BC.ArrayCopy2(EntryBuffer,0,Cid,0,16)
        GlobalStore.Store(2,Cid)
       
        EnterUserAccountDescription2(KEY_NONE)
    Else if Result = KEY_CANCEL Then
        RFIDmode = RFID_MODE_NONE
        LCD.Clear
        MenuUserAccount(KEY_NONE)
    End If   
End Sub
 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
you're right Erel, I changed it to
B4X:
Log(BC.HexFromBytes(tmp), "-" ,tmp.Length)
and I got this long line. I will continue with code and will keep you updated. Thanks so much

3030303538333637373700414243202020202020202020202020200044454620202020202020202020202020202020200047484920202020202020202020202020202020200020202020202020202020202000202020202020202020202020002000200020-101
 
Upvote 0
Top