B524 and M2 som board showing red light

The code was successfully uploaded initially

// Include Particle Device OS APIs
#include "Particle.h"
#include "ModbusMaster-Particle.h"
#include "JsonParserGeneratorRK.h"

// Test code
SYSTEM_MODE(AUTOMATIC);
SYSTEM_THREAD(ENABLED);

// Show system, cloud connectivity, and application logs over USB

SerialLogHandler logHandler(9600, LOG_LEVEL_WARN, {
    {"app", LOG_LEVEL_TRACE},
    {"system", LOG_LEVEL_INFO}
});

ModbusMaster slave1;

void preTransmission() {
    digitalWrite(D3, 1);
    digitalWrite(D2, 1);
}

void postTransmission() {
    digitalWrite(D3, 0);
    digitalWrite(D2, 0);
}

void idle() {
    delay(100); // in case slave only replies after 10ms
    Particle.process(); // avoids letting the connection close if open
}

void setup() {
    pinMode(D3, OUTPUT);
    pinMode(D2, OUTPUT);
    slave1.begin(7, Serial1); // Use slave ID 7
    slave1.setSpeed(9600, SERIAL_8N1);
    slave1.enableDebug(); // Enable debug logs
    slave1.preTransmission(preTransmission);
    slave1.postTransmission(postTransmission);
    slave1.idle(idle);
}

float conv(int i) {
    uint32_t rawValue = (slave1.getResponseBuffer(i) << 16) | slave1.getResponseBuffer(i + 1);
    float floatValue = *(float*)&rawValue; // Convert raw value to IEEE 754 float
    return floatValue;
}

float readFloatsFromSlave(uint16_t startAddress) {
    uint8_t result = slave1.readInputRegisters(startAddress, 2);

    if (!result) {
        return conv(startAddress);
    } else {
        Log.warn("Modbus read error at address %d: %d", startAddress, result);
        return -1; // Return a default error value if read fails
    }
}

void loop() {
    struct Parameter {
        const char* CorrelationId;
        const char* DisplayName;
        const char* Address;
        float Value;
    };

    // Define parameters with addresses and correlation IDs
    Parameter parameters[] = {
        {"Plants_RVoltage", "L-F1-RV", "30001", readFloatsFromSlave(0)},
        {"PlantLifetimeEnergy", "L-F1-E", "30073", readFloatsFromSlave(74)},
        {"Plants_Frequency", "L-F1-F", "30071", readFloatsFromSlave(70)},
        {"Plants_RPower", "L-F1-RP", "30013", readFloatsFromSlave(12)}        // Add more parameters as needed
    };

// Parameter parameters[] = {
//     {"DG_Run_Hour", "SG-BIO-RH", "30227", 154.28},
//     {"Plants_DG_Power", "SG-BIO-P", "30053", 0},
//     {"Load_MinCurrent", "L-F1-MIA", "30143", 0},
//     {"Plants_DG_Voltage", "SG-BIO-ACV", "30043", 0},
//     {"MPPTS1_Run_Hour_Ch1", "MP1-RH-CH1", "30057", 0},
//     {"Plants_DG_LifeTimeEnergy", "SG-BIO-E", "30073", 2983.95},
//     {"MPPTS1_Current_Ch2", "MP1-DCA-CH2", "30005", 0},
//     {"Plants_Inverter_MPPTS1DCPower", "PG-DD-1P", "30019", 0},
//     {"Plants_BPowerFactor", "L-F1-BPF", "30035", 0.95},
//     {"Plants_RVoltage", "L-F1-RV", "30001", 240.45},
//     {"MPPTS1_Current_Ch3", "MP1-DCA-CH3", "30007", 0},
//     {"MPPTS1_Run_Hour_Ch3", "MP1-RH-CH3", "30061", 0},
//     {"MPPTS1_Energy_Exp_Ch1", "MP1-EE-CH1", "30031", 0},
//     {"MPPTS1_Power_Ch3", "MP1-DCP-CH3", "30015", 0},
//     {"Plants_Inverter_MPPTS1DCVoltage", "PG-DD-1DCV", "30001", 0},
//     {"Plants_BPower", "L-F1-BP", "30017", 0.44},
//     {"Plants_DG_RVoltage", "SG-BIO-RV", "30001", 0},
//     {"Plants_UPS_BattDisEnergy", "ES-PB-DE", "30039", 23234},
//     {"MPPTS1_Current_Ch4", "MP1-DCA-CH4", "30009", 0},
//     {"Plants_UPS_BattCurrent", "ES-PB-DCA", "30003", 8.25},
//     {"Plants_DG_BVoltage", "SG-BIO-BV", "30005", 0},
//     {"MPPTS1_Run_Hour_Ch2", "MP1-RH-CH2", "30059", 79009},
//     {"Battery power", "PG-S-SR", "30011", 2.08},
//     {"Plants_DG_OutputPowerFactor", "SG-BIO-PF", "30063", 1},
//     {"DG_On_Hour", "SG-BIO-ONH", "30229", 15139.38},
//     {"Load_MaxVoltage", "L-F1-MAV", "30133", 271.84},
//     {"Load_MinVoltage", "L-F1-MIV", "30135", 0},
//     {"Batt_No_of_Interuptions", "ES-PB-INT", "30213", 289},
//     {"Batt_Run_Hour", "ES-PB-RH", "30205", 14844.16},
//     {"Batt_On_Hour", "ES-PB-ONH", "30203", 15189.18},
//     {"DG_No_of_Interuptions", "SG-BIO-INT", "30231", 281},
//     {"MPPTS1_Current_Ch1", "MP1-DCA-CH1", "30003", 0},
//     {"PlantLifetimeEnergy", "L-F1-E", "30073", 25155.52},
//     {"Load_On_Hour", "L-F1-RH", "30229", 15130.1},
//     {"Plants_YVoltage", "L-F1-YV", "30003", 240.63},
//     {"Load_Run_Hour", "L-F1-ONH", "30227", 14161.07},
//     {"Load_MaxCurrent", "L-F1-MAA", "30141", 128.99},
//     {"Load_No_of_Interrupts", "L-F1-INT", "30231", 282},
//     {"MPPTS1_Energy_Exp_Ch2", "MP1-EE-CH2", "30033", 0},
//     {"Plants_RPowerFactor", "L-F1-RPF", "30031", 1},
//     {"MPPTS1_Energy_Imp_Ch2", "MP1-IE-CH2", "30025", 0},
//     {"MPPTS1_Power_Ch2", "MP1-DCP-CH2", "30013", 0},
//     {"MPPTS1_Run_Hour_Ch1", "MP1-RH-CH1", "30057", 0},
//     {"Plants_YPower", "L-F1-YP", "30015", 0.51},
//     {"Plants_Frequency", "L-F1-F", "30071", 49.98},
//     {"MPPTS1_Power_Ch4", "MP1-DCP-CH4", "30017", 0},
//     {"MPPTS1_Energy_Imp_Ch1", "MP1-IE-CH1", "30023", 40639},
//     {"Plants_Inverter_MPPTS1Energy", "PG-DD-1E", "30039", 40639},
//     {"Plants_DG_OutputFrequency", "SG-BIO-F", "30071", 0},
//     {"MPPTS1_Power_Ch1", "MP1-DCP-CH1", "30011", 0},
//     {"Plants_DG_YVoltage", "SG-BIO-YV", "30003", 0},
//     {"MPPTS1_Run_Hour_Ch4", "MP1-RH-CH4", "30063", 0},
//     {"Plants_UPS_BattVoltage", "ES-PB-DCV", "30001", 251.94},
//     {"PlantLifetimeExportEnergy", "L-F1-EE", "30075", 0.02},
//     {"Plants_UPS_BattChaEnergy", "ES-PB-CE", "30041", 21966},
//     {"Plants_RPower", "L-F1-RP", "30013", 0}
// };

    // Create JSON object
    JsonWriterStatic<4096> jsonWriter;
    jsonWriter.startObject();
    jsonWriter.insertKeyValue("PublishTimestamp", Time.format(Time.now(), "%Y-%m-%d %H:%M:%S"));

    // Insert Content key and start array
    jsonWriter.insertKeyArray("Content");
    
    jsonWriter.startObject();
    jsonWriter.insertKeyValue("HwId", "Statcon-RTU");

    // Insert Data key and start array
    jsonWriter.insertKeyArray("Data");

    for (auto& param : parameters) {
        // jsonWriter.startObject();
        // jsonWriter.insertKeyValue("HwId", "Statcon-RTU");

        // // Insert Data key and start array
        // jsonWriter.insertKeyArray("Data");
        
        jsonWriter.startObject();
        jsonWriter.insertKeyValue("CorrelationId", param.CorrelationId);
        jsonWriter.insertKeyValue("SourceTimestamp", Time.format(Time.now(), "%Y-%m-%D %H:%M:%S"));

        // Insert Values key and start array
        jsonWriter.insertKeyArray("Values");
        
        jsonWriter.startObject();
        jsonWriter.insertKeyValue("DisplayName", param.DisplayName);
        jsonWriter.insertKeyValue("Address", param.Address);
        jsonWriter.insertKeyValue("Value", String::format("%.2f", param.Value));

        jsonWriter.finishObjectOrArray();  // Close Values object
        jsonWriter.finishObjectOrArray();   // Close Values array
        
        jsonWriter.finishObjectOrArray();  // Close Data object
    }
    jsonWriter.finishObjectOrArray();  // Close individual parameter object

    jsonWriter.finishObjectOrArray();   // Close Content array
    jsonWriter.finishObjectOrArray();  // Close main object
    jsonWriter.finishObjectOrArray();  // Close main object

    // Retrieve the JSON data
    String jsonData = String(jsonWriter.getBuffer());

    // Publish JSON data to the Particle Cloud
    Particle.publish("ModbusData", jsonData, PRIVATE);

    // Delay for the next reading
    delay(30000); // Adjust delay as needed
}

but after making small changes to the values in the code, it is no longer uploading. The device is showing the following error


and device is also following error

. Your assistance is needed to resolve this issue quickly.

By red light, do you mean the status LED is blinking red? What is the LED pattern?

The most common reason for that is a bug in your code that causes a hard fault. In that link, look for Safe Mode which will prevent your code from running and should allow you to flash new code. DFU mode (blinking yellow) can also be used if you are flashing locally from Workbench.

@rickkas7 yes it is the RGB LED. I have also attached the link to the video showcasing the blinking pattern. I think it is Heap error from the document could you confirm from your side. (https://drive.google.com/file/d/1ve4Tz0dB_1LMbTocnMStEXfm9xOJ-gFw/view?usp=drive_link)

I think that's 1 blink, hard fault. But in any case it doesn't really matter as long as you can put the device in safe mode you can flash new code to it.

However you do need to fix the underlying bug in your code that is causing the hard fault, typically using an uninitialized or delete pointer, or overwriting the end of a memory block.

@rickkas7 i have steps mentioned in above docs to shift to safe mode. but currently no led is blinking . I am attaching screen shots for reference. please your swift response is appreciated.


and also when i am flashing basic code


// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);

// Run the application and system concurrently in separate threads
SYSTEM_THREAD(ENABLED);

// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);

// setup() runs once, when the device is first turned on
void setup() {
  // Put initialization like pinMode and begin functions here
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.

  // Example: Publish event to cloud every 10 seconds. Uncomment the next 3 lines to try it!
  // Log.info("Sending Hello World to the cloud!");
  Particle.publish("Hello world!");
  // delay( 10 * 1000 ); // milliseconds and blocking - see docs for more info!
}

it is displaying following msg

If you cannot get the device into safe mode, you can try putting it in DFU mode (blinking yellow) and do Particle: Flash application and Device OS (local) from Workbench instead.