FreeMASTER Lite data export to .csv format

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FreeMASTER Lite data export to .csv format

443 Views
prashanth_dhundra
NXP TechSupport
NXP TechSupport

Hi,

Is there any way to generate a .csv file with FreeMASTER Lite?

Please share any relevant information.

 

Thank you,

Prashanth.

0 Kudos
1 Reply

430 Views
iulian_stan
NXP Employee
NXP Employee

Hi @prashanth_dhundra,

Unfortunately FreeMASTER Lite has no native support to export data directly into CSV. But there are 2 way to achieve this:

  1. Using File API exposed by FreeMASTER Lite
  2. Using Web Browser capabilities via JS

1. F. Lite API exposes functions allowing reading from and writing to files. First you need to specify what file permissions in your project file ex:

 

{
  "port": 8090,
  "web_root": "C:/NXP/FreeMASTER 3.2/FreeMASTER Lite/html",
  "dirs": [
    {
      "path": "C:/NXP/FreeMASTER 3.2/FreeMASTER Lite",
      "opts": "w+",
      "exts": ".csv"
    }
  ]
}

 

Note: by default all File IO operations are restricted the. In order to work with files, users need to specify explicitly the path (root folder), exts (list of allowed file extensions) and opts (list of allowed modes/flags).

And here's how you would use the API in your application:

 

<html>  
<head>  
   <title> Download CSV file using FMSTR File API </title>
  <script type="text/javascript" src="./simple-jsonrpc-js.js"></script>
  <script type="text/javascript" src="./freemaster-client.js"></script>
</head>  
<body onload="InitPCM()">  
  <h3> Click the button to save data to CSV file </h3>  
    
  <!-- create an HTML button to save data the CSV file on click -->  
  <button onclick="save_csv_file()"> Save CSV </button>  

  <script> 
    var pcm;

    //create CSV file data in an array  
    var csvFileData = [  
      ['Col 1 Val 1', 'Col 2 Val 1'],  
      ['Col 1 Val 2', 'Col 2 Val 2'],  
      ['Col 1 Val 3', 'Col 2 Val 3'],  
      ['Col 1 Val 4', 'Col 2 Val 4'],  
      ['Col 1 Val 5', 'Col 2 Val 5']  
    ];  
        
    //create a user-defined function to save the CSV file   
    async function save_csv_file() {  
    
      //define the heading for each row of the data  
      var csv = 'Column 1,Column 2\n';  
        
      //merge the data with CSV  
      csvFileData.forEach(function(row) {  
        csv += row.join(',');  
        csv += "\n";  
      });  
    
      try {
        let response = await pcm.LocalFileOpen("C:/NXP/FreeMASTER 3.2/FreeMASTER Lite/data.csv", 'w+');
        let handle = response.data;
        await pcm.LocalFileWriteString(handle, csv);
        await pcm.LocalFileClose(handle);
        console.log("File saved");
      } catch(err) {
        on_error(err);
      }
    }
    
    function InitPCM() { 
      pcm = new PCM("localhost:8090", console.log, on_error, on_error);
      pcm.OnServerError = on_error;
      pcm.OnSocketError = on_error;
    }

    // Notify user about an error
    function on_error(err) {
      console.error(err);
    }
  </script>
</body>  
</html>  

 

Note the save_csv_file function:

  1. First, you need to create an in-memory object that will hold your CSV data.
  2. Then, you convert it into a string.
  3. Finally, you use the API to write data to the file in a C-like approach.

This simple page contains one single button - when pressed it will write data to disk (debug logs a printed in the browser console).

2. Second approach is a web oriented

In this case no changes are required on F. Lite side because all processing is done on client side (browser):

 

<html>  
<head>  
  <title> Download CSV file via HTML/JS </title>  
</head>  
<body>  
  <h3> Click the button to download the CSV file </h3>  
    
  <!-- create an HTML button to download the CSV file on click -->  
  <button onclick="download_csv_file()"> Download CSV </button>  


  <script>  
    //create CSV file data in an array  
    var csvFileData = [  
        ['Col 1 Val 1', 'Col 2 Val 1'],  
        ['Col 1 Val 2', 'Col 2 Val 2'],  
        ['Col 1 Val 3', 'Col 2 Val 3'],  
        ['Col 1 Val 4', 'Col 2 Val 4'],  
        ['Col 1 Val 5', 'Col 2 Val 5']  
    ];  
            
    //create a user-defined function to download CSV file   
    function download_csv_file() {  
    
        //define the heading for each row of the data  
        var csv = 'Column 1,Column 2\n';  
            
        //merge the data with CSV  
        csvFileData.forEach(function(row) {  
        csv += row.join(',');  
        csv += "\n";  
        });  
            
        var hiddenElement = document.createElement('a');  
        hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);  
        hiddenElement.target = '_blank';  
            
        //provide the name for the CSV file to be downloaded  
        hiddenElement.download = 'data.csv';  
        hiddenElement.click();
    }
  </script>
 </body>  
</html>  

 

CSV content creation is quite similar to the previous approach. The difference - is the way the file is saved: the browser has no access to the file system, thus the CSV content is encoded into a URL and added to hidden hyperlink. When the download button is pressed - the browser prompts a download dialog asking the user to select the location where the file is saved.

Hope it helps,
Iulian 

0 Kudos