B4R Question Error handling in b4R, how to catch?

yaniv hanya

Active Member
Licensed User
I need to create a app that will read a info from eeprom in the start.

my problem is that in the first time (or after Reset), the eeprom is empty and return Error.
I understand there is no "try/catch" block in b4r. so in first time reading, the app crashes...

How can i solve the problem? i need to do something like-
Try to read username, if it is empty return false, so we will go to the user to get the username.

For now, empty return error… and app stops.

thanks
 

yaniv hanya

Active Member
Licensed User
so, i need to install a first app, that will insert the "magic marking", and then the main app that will get and check the userName?
since the "Regular" app should run again and again, in case of wifi disconnection. or electric stop and so on...
 
Upvote 0

candide

Active Member
Licensed User
no need a first app to initialize EEPROM
First, when user/password is written, the 2 first bytes aree Magic byte and length of data
After,you can read first 2 first bytes in eeprom , use/passwors if present if byte 0= Magic EEPROM in byte 0, and byte 1 = length of data
something like that:
B4X:
Private const MAGIC_EEPROM As Byte = 213

Public Sub GetStoredDataLength As Byte
    Dim header() As Byte = eeprom.ReadBytes(0, 2)
'    Log("header lu=",header(0),"+",header(1))
    If header(0) = MAGIC_EEPROM Then
        Return header(1)
    End If
    Return 0
End Sub

Sub    GetStoredData As Byte
    Dim length As Byte = GetStoredDataLength    
'    Log("length wifi=",length)
    If length > 0 Then
        Dim ObjectsBuffer(18) As Object
        Dim Data() As Byte = eeprom.ReadBytes(2, length)
        Dim Objects() As Object = sr.ConvertBytesToArray(Data, ObjectsBuffer)
        If Objects.Length <> 0 Then
'          For Each o As Object In Objects
'            Log("obj_wifi",o)
'          Next
          For i = 0 To 17
          GStore1.Put(i,Objects(i))
          Next
          Return 1
        Else 
          Return 0
        End If    
    Else
        Return 0    
    End If          
End Sub

Public Sub SaveNetworkDetails(Data() As Byte)
    Log("Saving Network data")
    Log("data length: ", Data.Length)
'    Log("data to eeprom=",Data)
    eeprom.WriteBytes(Array As Byte(MAGIC_EEPROM, Data.Length), 0)
    eeprom.WriteBytes(Data, 2)
 '   ConnectToNetwork
End Sub
 
Upvote 0

candide

Active Member
Licensed User
this code was used on esp8266 /esp32 to avoid issue in case of no data.

to clear data, i just erase bytes 0 and 1 in eeprom
 
Upvote 0
Top