Saving Array Values

Discussion in 'Pascal Logic Code Examples' started by pspeirs, Oct 25, 2018.

  1. pspeirs

    pspeirs

    Joined:
    Nov 23, 2013
    Messages:
    185
    Likes Received:
    10
    Location:
    Sydney
    I've been looking at how to store a bunch of array values so that I can restore when the system reboots. One method below seems to work well. If there is a better way to handle this then I'd be happy to hear it.

    Code:
    
    { VARIABLES }
    // Array definition for pulse input testing
    // Pulses per minute for 1 hour
    arrPulse_1: array[1..59] of integer;    // Holds accumulated values for each minute in the hour
    iPulse: integer;
    i: integer;
    cnt: integer;
    tmp_str: string;
    out_str: array[0..236] of char;   // Number of elements x 4 = 59 x 4 = 236
    
    { FUNCTION }
    begin
    
        out_str := '';
    
        { Read the array we've been populating elsewhere and create a big string }
        { This code can be run immediately after updating the array elements }
        for i := 1 to 59 do
        begin
            arrPulse_1[i] := random(65535);    // Used for testing only. Add a random value up to 65536
            IntToHexString(arrPulse_1[i], 4, tmp_str);  // The more bits, the longer 'out_str' needs to be
            Append(out_str, tmp_str);
        end;
     
        writeln(out_str);    // Just for testing
    
        // Save string to System IO.  String size will be number of elements * 4
        SetStringSystemIO("CBUS - Pulse Input 1 String", out_str);
    
        out_str := '';
        cnt := 1;
        i:= 1;
    
    
    { This section can go in the init function to reload the values on system restart }
        // Read the string back to array on initialization
        GetStringSystemIO("CBUS - Pulse Input 1 String", out_str)
       
        while i < Length(out_str) do
        begin
            Copy(tmp_str, out_str, i, 4);
            arrPulse_1[cnt] := HexStringToInt(tmp_str);
            cnt := cnt + 1;
            i:= i + 4;
        end;
    
    { This section is just to verify the string is read back correctly, not needed in a live system }
        for i := 1 to 59 do
        begin
            IntToHexString(arrPulse_1[i], 4, tmp_str);
            Append(out_str, tmp_str);
            writeln(tmp_str);
        end;
    
        writeln(out_str);
    
    end;
    
    
     
    pspeirs, Oct 25, 2018
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.