B4R Question Strange crash after writing to file (WeMos)

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi,

I'm using the rESP8266FileSystem library to write to / read from a config file on my WeMos D1 R2's built-in flash file system.

When I first boot the board, I can write to the config file as many times as I want with no issues - however when I send config data from a B4A app via UDP, copy that config data into variables on the WeMos & then write it to the config, the board crashes with this error:


The weird thing is that this crash happens after the file has been written & closed - so it's not an issue with the format of the data (it's the same variables that I'm writing every time anyway & even if I hard code data in there, I still get the crash).

I've done some searching online & it seems that the "Exception(9)" is something to do with the WiFi resetting (or something), but I haven't really been able to find anything to help me.

Here's the code I use to write to the config:

B4X:
Private Sub saveConfig
    Private buffer() As Byte
    Private bc As ByteConverter
   
    If fs.OpenReadWrite("myFuelConfig.dat") Then
        Log("Config open for writing")
        fs.Position = 0
        buffer = bc.StringToBytes(JoinStrings(Array As String(supplyRate, ":", returnRate, ":", calibrationOffset))) 'local config variables
        Log("BUFFER: ", buffer)
        fs.Stream.WriteBytes(buffer, 0, buffer.Length)
        fs.Stream.Flush
        fs.Close
        Log("Config Saved")
    End If
End Sub

This is driving me absolutely nuts. I can't get it to work no matter what I do.

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Avoid using strings and especially avoid using JoinStrings. This can easily lead to memory issues.
You should instead call fs.Stream.WriteBytes multiple times.

What is the type of supplyRate, returnRate and calibrationOffset?
Thanks Erel,

The types are:

B4X:
Public supplyRate As Double
Public returnRate As Double
Public calibrationOffset As Int

I was sending the items in a colon-separated list - eg: "3.46:2.55:6" & then running a ByteConverter Split iterator to separate them on the receiving end - however I have just rewritten the code so that I send each calibration item individually from the B4A client & then once they have all been received on the B4R end, I save the config. This seems to have resolved the issue without removing the JoinStrings.

From what I could find on the web, it seems that this issue can be caused by long-running loops, so the change I have made has eliminated the Split iterator loop - but I really can't see how a loop that separates 3 values could be "long-running"...

The code I was using to split the received values was:

B4X:
Public Sub calibrationUpdateReceived(msg As String)
    Private cal(4) As String
    Private i As Int = 0
    Private bc As ByteConverter
       
    For Each s() As Byte In bc.Split(msg, ":")
        cal(i) = bc.StringFromBytes(s)
        i = i + 1
    Next
    supplyRate = cal(1)
    returnRate = cal(2)
    calibrationOffset = cal(3)
    saveConfig
End Sub

& getting rid of this seems to have resolved the issue.

- Colin.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…