David Meeker
dmeeker@ieee.org
October 25, 2004


Introduction


This note describes an example program that uses the "filelink" method of inter-process communication to talk to FEMM. The example source is available here. "Filelink" uses two temporary files to pass messages back and forth between FEMM and some other process. Although Filelink is relatively slow and inefficient (e.g. in comparison to ActiveX), it is very easy to implement on the client side, and doesn't involve much of a learning curve on the part of the coder implementing the client application. In fact, the source code for the example can probably be cribbed to perform all of the interprocess communication. Filelink can also allow rather disparate applications to communicate. For example, Octave running on a Windows platform via the Cygwin libraries can communicate with FEMM with no problems.


FEMM must first be run with the –filelink command line option. When FEMM is run in Filelink mode, it checks for the existence of a file called ifile.txt that is located in the same directory as the FEMM executable, typically located at:

c:\program files\femm42\bin\femm.exe

FEMM checks for this file each time through FEMM's idle routine. If the file is detected, FEMM reads the text in the file (typically just one line of text), deletes the file, and sends the read string to be interpreted by FEMM's Lua interpreter. Any results produced by the command to Lua are then written to the file ofile.txt, also located in the same directory as the FEMM executable. The result is formatted in a fashion that can be parsed directly by the eval command of Octave or Matlab. That is, all results are closed in square brackets (i.e. []) and numerical values are separated by commas.

As an example, if a program wrote an ifile.txt containing the line:

2+2

FEMM would read the file, interpret the line with Lua, and write to ofile.txt

[4]

Example Program fclient


fclient is an example program that implements a terminal interface for sending commands to FEMM's Lua interpreter and receiving the results of each command's evaluation. The entire example is only 45 lines, including comments.

The main routine is:
_unlink(femmifile);

_spawnl(_P_NOWAITO,femm_exe,femm_exe,"-filelink",NULL);

printf("Terminal interface to FEMM.\nEnter \"quit()\" to exit.\n");

while(strncmp(u,"quit()",6)!=0) callfemm(gets(u));


This routine first makes sure that the input file is
cleared out before FEMM is started, so that there is no spurious old command
"in the pipe" when FEMM starts.

The spawnl function is then used to start an new FEMM process with the -filelink argument.

A message is printed the name of the program and how to end it.

Lastly, the program goes through a loop that reads a line of input from the keyboards and sends it to a function, callfemm, that implements the Filelink transfer:

void callfemm(char *x)
{
	FILE *fid;
	static char u[1024];

	/* Write the command to the input file */
	_unlink(femmofile);
	fid=fopen(femmifile,"wt");
	fprintf(fid,"flput(%s)",x);
	fclose(fid);

	/* Read the results from the output file */
	do{
		while((fid=fopen(femmofile,"rt"))==NULL) Sleep(1);
		fgets(u,1024,fid);
		fclose(fid);
	} while (strlen(u)==0);
	_unlink(femmofile);

	/* Just print the results. If this were a more complicated
	   program, we could parse the results back to numerical values */
	printf("%s",u);
}

The callfemm function writes the input line to the input file. The routine then repeatedly checks for the existences of the results file, sleeping a short time between checks to allow other processes on the machine to run. When the results file is detected, it is read, deleted, and echoed to the screen.

In other applications, one might want to obtain numerical values from the results, rather than simply printing the results. Typically, the number of results expected from the call is known, so that sscanf could be employed to most easily parse the results. For example, if four results were expected from FEMM, the line that took the four resulting numerical values and put them into the previously defined doubles x1 through x4 would be:

sscanf(u,"[%lf,%lf,%lf,%lf]",&x1,&x2,&x3,&x4);

It is, of course, possible that commands to Lua sent through Filelink could result in an error. If this is the case, the Lua error message will be returned in the results file. All error messages start with the word error with the error message actually being in a form that can be parsed by Octave or Matlab.

Conclusions


Filelink is a fairly simple way of implementing inter-process communication with FEMM. Hopefully, this simple example has given you the knowledge necessary to use Filelink to communicate between FEMM and your own programs.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki