Jump to content
OMRON Forums

how to record data into a text file from PMAC IDE?


moha_sa

Recommended Posts

Anyone knows how to record data into a text file from PMAC IDE for example from a background C program to be used for post processing in other softwares like MATLAB for  visualization? I tried "fprintf" but it does not seem generating any text file anywhere in the program folder on windows. 

Link to comment
Share on other sites

We have a larger application note titled "Advanced Data Gathering on Power PMAC" which may give you some good guidance on using the Gather program to do this. You should be able to request it from your appropriate Omron support representatives for them to get for you.

 

That said, as somewhat of an excerpt, we have used code that looks roughly like this:

int main(void)
{
	InitLibrary();
	char str[100];
	while(1 == 1)
	{
		while(GatherFlag == 0) sleep(0.001);
		GatherFlag = 2;
		sprintf(str, "/usr/bin/gather > /media/disk/MyData.txt");
		system(str);
		sleep(0.001);
		GatherFlag = 0;
	}
	CloseLibrary();
}

In that case, when an external program sets "GatherFlag" to 1, this will stream the data actively being gathered into a text file on a flashdrive inserted into the PMAC. Once the data gathering is stopped, the flashdrive can then be removed and the data can be observed on another computer.

  • Like 2
Link to comment
Share on other sites

Hi moha_sa,

I'm currently working in a fast data acquisition application using native gather inside a C Background Program.

On 3/1/2022 at 8:24 PM, moha_sa said:

I tried "fprintf" but it does not seem generating any text file anywhere in the program folder on windows. 

The Background Program runs on Linux, inside the controller. If you are trying to write some file, the fopen path argument (to create a file handler for fprintf, where it will write to) must exist on controller's Linux, not on Windows where IDE runs. Are you checking fopen's return value? 

/*For more information see notes.txt in the Documentation folder */
#include <gplib.h>

#define _PPScriptMode_		// for enum mode, replace this with #define _EnumMode_
#include "../../Include/pp_proj.h"

int main()
{
  FILE * fp;

  fp = fopen ("/var/ftp/gather/file.txt", "w+");

  // Check if file handler is not corrupted
  if(!fp)
  {
    printf("Error trying to open the file, is everything ok?\n");
    return -1;
  }

  fprintf(fp, "%s %s %s", "Write", "some", "stuff");

  fclose(fp);

  printf("File was written successfully!\n");

  return 0;
}

This snippet is a simple file writer that check it. Connect to the controller via SSH, run the above program and check if /var/ftp/gather/file.txt was created correctly after "File was written successfully!" message from the program. Now you can use pscp (Windows) or scp (Linux), or another tool, to copy the file to your Windows host where IDE is running. A strong evidence that there is something wrong with the path argument is fopen returning NULL value.

Besides that, the IDE program folder isn't synchronized with controller's folder when you Download the project, it's just a copy (to /opt/ppmac/usrflash/Project). Also, the project folder inside Power PMAC is write protected and you may modify the permissions to write to it (Honestly I don't like this idea).

On 3/4/2022 at 9:46 PM, AAnikstein said:

That said, as somewhat of an excerpt, we have used code that looks roughly like this:

If you want to have some more control to the gather process (and complete AAnikstein's code) I did some manipulation with fork and file descriptors:

// Create gather process
pid_t pid=fork();

/* Child process */
if(pid==0)
{
  // Open gather output file
  int gather_file_fd=open(file_path, O_WRONLY | O_CREAT, 0777);
  if(gather_file_fd==-1)
  {
    return 2;
  }

  // Make stdout of gather program as input of output gather file
  int gather_file_fd_dup=dup2(gather_file_fd, STDOUT_FILENO);
  close(gather_file_fd);

  // Give this process to gather control, like a parasite
  int err;
  err=execl("/usr/bin/gather","gather","-w",NULL);

  if(err==-1)
  {
    return 2;
  }
}
else
{
  /* Parent process */
  return pid; // Return child PID
}

Doing that you are changing the gather output from stdout to the file and also you have the PID of the gather program instead of system command that do not allow you to manipulate gather process.

Regards,

George

  • Like 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...