/* mterml_sh2.cpp vers. 0.995 Linux FAC May 16 2011 mterml_sh2.cpp is converted from mterml_sh.cpp(linux) and mterm.cpp(windows). Run usage: mterml_sh2 adapted from ctermb vers. 0.9 Linux Compile: Place "mterml_sh2" into "Makefile" and run: make -f ./Makefile or: make mterml_sh2 mterml_sh2.cpp is linux shared memory version Shared mem program development done with progs.: shm_server.cpp, shm_client.cpp Other Files-------------------------- Uses local files mblk10s Win compiled DAP custom program mblk10.dap Dap control program mblk10.env initilize stuff file mblk10.his history file Uses sh mem2 to transfer binary data to "loopget.cpp" etc. Optionally it can still write to the binary files: bin1, bin2, ... DAP feedback binary output files. There are a bunch of these, that get written in a ring style Each time one of the bin* files gets written its name is placed into the bottom of the file "binFiles" Other routines read binFiles to get latest names. User is also requested for a log file name The bin1, etc files may not be used if writting is to the sh mem2 area. Windows older version ------------------------------ Compile: Place the name "mterm.cpp" into "makefile" (see s100.cpp template) then execute command: "dapPC" in DOS window. Note!: You must create "permanent" Comm pipes Cp2IN Cp2OUT before using this program. Search for string "Trammell" in code below. Also on Windows98 file names are not case sensitive. Function: -------------------------- Set up pipes, open files, interface between mblk10 and PC Opens shared memory for commun. with "mstun" Print and examine text coming from DAP, log text to some *.txt log file, Place stress,strain, etc voltages from DAP feedback into file. Periodically send text from mblk10env.txt file to DAP. Read and write shared memory 1 for interaction with mstun.cpp Read and write shared memory 2 for interaction with loopmaster.cpp C Copyright (C) 2010 F.A. Conle C This file is free software; you can redistribute it and/or modify C it under the terms of the GNU General Public License as published by C the Free Software Foundation; either version 2 of the license, or (at C your option) any later version. C This file is distributed in the hope that it will be useful, but C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTA- C BILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public C License for more details. C You should have received a copy of the GNU General PUblic License along C with this program; if not, write to the Free Software Foundation, Inc., C 59 Temple Place -Suite 330, Boston, MA 02111-1307, USA. Try also their C web site: http://www.gnu.org/copyleft/gpl.html */ /*#include //needed for "sleep()" command at least #include //detect keyboard hits (DOS only?)*/ #include #include #include //Linux req. #include //Linux #include //Linux #include //Linux #include //Linux #include //Linux #include //Linux sleep() #include //Linux shared mem. #include //Linux shared mem. #include #include #define MAX_CHARS 2048 //used to read $SysOut and Cp2out pipes #define MAX_BFILES 511 //sets the number of rotating binary out file names #define BUF_SIZE 1024 //sets the size of the binary out files in 16bit words // MAX_BFILES and BUF_SIZE are also used by sh mem2 buffer nos. and size // There are actually 512 buffs, but the first is used for control stuff. #define SHMSZ 1024 //size of shared memory (bytes in Linux). #define SHMSZ2 1048576 //size of shared memory2 (bytes in Linux). // Assuming 8 bit bytes, each buff is 1024*2 bytes // If we use 512 buffers then 1024*2 *512= 1048576 // According to the "ipcs -m" it appears that the shared mem size is // created as a byte argument. // For non-Mstar code use type convention: // int variable start with "i" or "j" or "n" // long int variables start with "il" //void main(int argc, char ** argv) //"void" main not allowed in g++ int main(int argc, char ** argv) { HDAP hdapSysPut, hdapSysGet, hdapBinGet, hdapCp2InPut, hdapCp2OutGet; /*Windos: char *pszDapName = "\\\\.\\Dap0"; */ char *pszDapName = "/Dap0"; // Linux char szTemp[1024]; char szMsg[256]; //Linux to check pipe create error char sentence[100], sentence2[100]; char *sentencePtr = &sentence[0]; char *sentence2Ptr = &sentence2[0]; char *cfield1; //used to extract the first field from a sentence //char *cfield1Ptr = &cfield1[0]; //ibid char *cfield2, *cfield3; //used to extract other fields //char ch; //Windows? may eventually work to read a char from keyboard short int iret; //returned from this program short int i, j; //general use index or counter long int il; //general use index or counter long int il_nhist; //counter for no of points in history long int il_nix, il_error; //counters short int ivolt1, ivolt2; //used in volt to int conversion unsigned long ul_dwTimeWait; // timeout for pipe reading short int isleep; //used as a return for fn sleep() in linux. float histvalue1, histvalue2; // value read from load history long int nramps; float xabs1, xabs2, xabsmax, vhistmax, vhistmin; // used to find max,min history float x, x2, desiredVoltmax, scaledata, meanshift; // used to scale history float scale1; //multiplier to convert volts to int FILE *textLogfile; //ascii log file, dump stuff here for the record char tlname[50]; FILE *binLogfile; //ascii index file for binary file names. char blname[50]; FILE *bfile; //binary file char binfilename[50]; // usually bin1, bin2, ... bin20 etc. short int ibinfileno; //used to create the bin file name for opening. short int databuffer[BUF_SIZE]; long int ilBCount; // count how many bytes transfered from BinOut pipe. long int il_adbuffsWritten; //counter for no. of buffs written. Counter is put in sh mem. long int il_nextFreeBuff; //The number of the next sh mem2 bin buffer about to be written long int il_binVector; // =0 bin data to sh mem2; =1 bin data to binfiles on disk short int imaxChan0, iminChan0; //Used to find max & mins of each bin buffer short int imaxChan1, iminChan1; short int nchan; //no. of A/D chans. Value is sent by mblk10s for sh mem1 long int ilbuffstoremax; //The last valid word in buffer. depends on nchan FILE *envfile; //Read this initilization file and send to DAP char envname[50]; FILE *histfile; //File to be run by mblk10 char histfilename[50]; FILE *threshfile; char threshname[50]; char yon[10]; //char string to read "y" or "n" or other char char yn; char modename[20]; //states which mode we are in: // init monitor pause stop close //Shared memory stuff int shmid, shmid2; key_t key; key_t key2; long int *shm, *s; //shared mem address short int *shm2, *s2; //shared mem address, area 2 long int iln, iln2, iln3; //used for loop index and also data transf. key = 91810; //number just refers to creation date, is used to make a //name for the shared mem segment. key2 = 181110; const int iBlockSize = 1024; //size of DAP binary data blocks( in 16 bit words) unsigned short int BinData[iBlockSize]; TDapBufferGetEx GetBin; // TDapBufferGetEx initialization to handle the $Binout pipe DapStructPrepare(&GetBin, sizeof(GetBin)); // Size of this structure //GetBin.iBytesGetMax = 32768; // Maximum bytes to get //GetBin.iBytesGetMin = 1024; // Minimum bytes to get //GetBin.dwTimeWait = 1000; // Time interval to wait for new data. unsigned long //GetBin.dwTimeOut = 0; // Total time for entire operation: unsigned long //GetBin.iBytesMultiple = 1; // Bytes to get is a multiple of this (see manual) //On Larry T's suggestion: //In order to get better buffer alignment changed Jan 26 2011 : GetBin.iBytesGetMax = 2048; // Bytes per 1024 buffered 'data words' GetBin.iBytesGetMin = 2048; // Only accept full blocks GetBin.dwTimeWait = 1000; // Time interval to wait for new data. unsigned long GetBin.dwTimeOut = 0; // Total time for entire operation: unsigned long GetBin.iBytesMultiple = 2; // Keep data in 'data word' units (see manual) // Note there is another structure defined below in the program. Search for "GetI" // If more than one Dap in system (unlikely) you need this: // if (argc > 1) // pszDapName = argv[1]; // hdapSysPut - \\.\Dap0\$SysIn (text) // hdapSysGet - \\.\Dap0\$SysOut (text) // hdapBinGet - \\.\Dap0\$BinOut (binary) // hdapCp2Get - \\.\Dap0\Cp2In (text) strcpy (modename, "init\0"); printf("# PC: mterml_sh2 vers. 0.995 starts. mode is : %s\n\n",modename); printf("# Opening pipe $SysOut...\n"); //receives from DAP /*Windos: sprintf(szTemp, "%s\\$SysOut", pszDapName); */ sprintf(szTemp, "%s/$SysOut", pszDapName); //Linux hdapSysGet = DapHandleOpen(szTemp, DAPOPEN_READ); if(! hdapSysGet) { printf("# Error: could not open pipe $SysOut \n"); goto CLOSE; } DapInputFlush(hdapSysGet); printf("# Opening pipe $SysIn...\n"); //sends to *.dap module /*Windos: sprintf(szTemp, "%s\\$SysIn", pszDapName); */ sprintf(szTemp, "%s/$SysIn", pszDapName); //Linux hdapSysPut = DapHandleOpen(szTemp, DAPOPEN_WRITE); if(! hdapSysPut) { printf("# Error: could not open pipe $SysIn \n"); goto CLOSE; } printf("# Opening pipe $BinOut...\n"); //voltages from DAP /*Windos: sprintf(szTemp, "%s\\$BinOut", pszDapName); */ sprintf(szTemp, "%s/$BinOut", pszDapName); //Linux hdapBinGet = DapHandleOpen(szTemp, DAPOPEN_READ); if(! hdapBinGet) { printf("# Error: could not open binary pipe $BinOut \n"); goto CLOSE; } DapInputFlush(hdapBinGet); /*Check out command in Dapio32 Ref. manual pdf, pg.66. Also check pg. 55 DapComPipeCreate description. "To create a "permanent" Comm pipe use the Data Acquisition control panel application. Find the DAP board under the 'browser' tab and right click on 'Com pipes' to define a new one ("In" pipes go "In" to the DAP board, text data type). Once you set it up it is always there, restored every time you reboot the system." (above from Larry Trammell) Thus here(Windos) we will only be opening the pipe, not creating it. */ // NOTE: in Windows these pipes already exist. // In Linux they do NOT exist after reboots. (full reboots??) //Linux begin /*printf("# Creating comm pipe Cp2In...\n"); sprintf(szTemp, "%s/Cp2In type=word maxsize=4096",pszDapName); if ( ! DapComPipeCreate(szTemp) ) { printf ("#Error: Could not create the pipe %s\n- %s\n", szTemp, DapLastErrorTextGet((char *) szMsg, sizeof(szMsg) )); goto CLOSE; } printf ("# created.\n"); //Linux end */ printf("# Opening comm pipe Cp2In...\n"); //sends to custom module /*Windos: sprintf(szTemp, "%s\\Cp2In", pszDapName); */ sprintf(szTemp, "%s/Cp2In", pszDapName); //Linux hdapCp2InPut = DapHandleOpen(szTemp, DAPOPEN_WRITE); if(! hdapCp2InPut) { printf ("# Error: Could not open the pipe %s\n- %s\n", szTemp, DapLastErrorTextGet((char *) szMsg, sizeof(szMsg) )); goto CLOSE; } printf ("# opened.\n"); DapInputFlush(hdapCp2InPut); //Linux begin /*printf("# Creating comm pipe Cp2Out...\n"); sprintf(szTemp, "%s/Cp2Out type=word maxsize=4096",pszDapName); if ( ! DapComPipeCreate(szTemp) ) { printf ("# Error:Could not create the pipe %s\n- %s\n", szTemp, DapLastErrorTextGet((char *) szMsg, sizeof(szMsg) )); goto CLOSE; } printf ("# created.\n"); //Linux end */ printf("# Opening comm pipe Cp2Out...\n"); //receives from custom module /*Windos: sprintf(szTemp, "%s\\Cp2Out", pszDapName); */ sprintf(szTemp, "%s/Cp2Out", pszDapName); hdapCp2OutGet = DapHandleOpen(szTemp, DAPOPEN_READ); if(! hdapCp2OutGet) { printf ("# Error: Could not open the pipe %s\n- %s\n", szTemp, DapLastErrorTextGet((char *) szMsg, sizeof(szMsg) )); goto CLOSE; } printf ("# opened.\n"); DapInputFlush(hdapCp2OutGet); // Shared memory 1 set up. (Only in Linux). printf("#Creating the shared mem. segment 1...\n"); if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } printf("#Attaching segment1 to our data space...\n"); if ((shm = (long int *)shmat(shmid, 0, 0)) == (long int *) -1) { perror("shmat"); exit(1); } // shm should now point to the 1st shared mem loc. s = shm; //leave shm same, and use s to index iln = 1; *s = iln; //turns off shared mem write flag for all, for now // see file shmemMap for explanation of locations and functions s = shm+1; iln = -1; *s = iln; //leave a "null command" in the command sh mem. s = shm+2; iln = 0; *s = iln; // Set "status" register to indicate PAUSE printf("#Placing some check numbers into shared mem...\n"); iln = 3; // iln & shared mem are all "long int" s=shm+3; while(1) { *s = iln; printf("#Placed %li in shared mem\n",iln); s=s+1; iln =iln +1; if(iln == 9)break; } // Just prior to starting run, set sh mem loc 0 to 0 and then // we can wait until an "mstun" program attaches sh mem and // writes a "-1" into location 0 // Other items are transmitted by custom and will be placed in // this area. // Set up shared mem 2 (This is the binary data buffer area) printf("#Creating the shared mem. segment 2...\n"); if ((shmid2 = shmget(key2, SHMSZ2, IPC_CREAT | 0666)) < 0) { perror("shmget"); exit(1); } printf("#Attaching segment2 to our data space...\n"); if ((shm2 = ( short int *)shmat(shmid2, 0, 0)) == ( short int *) -1) { perror("shmat"); exit(1); } // shm2 should now point to the 1st loc. in sh. mem. segment2 s2 = shm2; //leave shm2 same, and use s2 to index i= (short)(MAX_BFILES -1); //1st buff is for control. Rest are for bin data // note that "i" and all locs in sh mem2 are short ints. *s2= i; // Write out some words into sh mem2 to allow a check for correct attachment s2=shm2+1020; i = 1020; *s2 = i; s2=shm2+1021; i = 1021; *s2 = i; s2=shm2+1022; i = 1022; *s2 = i; s2=shm2+1023; i = 1023; *s2 = i; //Init the buff is full flags to empty ( =0 ). Flags are in // first 1024 locs. of sh mem2 i=0; while (1) { i=i+1; s2=shm2 + i; if(i >= (MAX_BFILES-1) ) break; //ck end of while loop? *s2 = 0; } printf("#mterml: buffer flags in sh mem2 have been set to 0\n"); //send command to reset the DAP DapLinePut (hdapSysPut, "RESET"); printf("#mterml: sent RESET to DAP\n"); // Send mblk10.dap file to DAP /*Windos: iret = DapConfig(hdapSysPut, "MBLK10.DAP"); */ iret = DapConfig(hdapSysPut, "mblk10.dap"); if(iret != 1) { printf("#Error sending mblk10.dap to DAP\n"); printf("#Make sure mblk10.dap is in same folder as mterml\n"); goto CLOSE; } printf("#mterml: mblk10.dap file sent to DAP\n"); printf("# Opening Disk files:\n"); printf("# Note: If files exist they will be overwritten !\n\n"); printf("# Enter text log file name:\n"); scanf ("%49s", tlname); if ( (textLogfile = fopen (tlname, "w") ) == NULL) { printf("# Error opening file. \n"); iret = 0; goto CLOSE; } strcpy (blname, "binFiles\0"); printf("# Index file for binary file names = %s\n", blname ); if ( (binLogfile = fopen (blname, "w") ) == NULL) { printf("# Error opening index file for binary feedback files= %s\n", blname); iret = 0; goto CLOSE; } ibinfileno=1; sprintf(binfilename, "bin%d\0", ibinfileno ); // the null char "\0" above creates a warning in g++. Maybe extra? printf("# Opening 1st binary file = %s\n", binfilename); if ( (bfile = fopen (binfilename, "wb") ) == NULL) { printf("# Error opening 1st binary feedback file= %s\n", binfilename); iret = 0; goto CLOSE; } /* Code in this comment segment not correct. Use seperate command prog. "modload.cpp" supplied by Mstar. // Activate the custom module if ( ! DapCommandDownload(hdapSysPut,"mblk10m.dlm") ) { printf("mterml: Error sending mblk10m.dlm to DAP. Stopping\n"); printf("#Make sure mblk10m.dlm is in same folder as mterml\n"); printf ("# Error msg from DAP:\n %s\n", DapLastErrorTextGet((char *) szMsg, sizeof(szMsg)) ); goto CLOSE; } */ DapStringPut (hdapSysPut, "START C\r"); printf("#mterml: sent START C to mblk10.dap\n"); // Check for chars in DAP $sysout comm pipe // Coding is similar to Mstar example: dterm32.cpp // For clarity variables are defined here rather than begin of prog. long int ilCount; char inpchars[MAX_CHARS]; TDapBufferGetEx GetInfo; // prep the GetInfo structure for usage DapStructPrepare(GetInfo); GetInfo.iBytesGetMin =1; GetInfo.iBytesGetMax = MAX_CHARS-1; GetInfo.dwTimeWait = 1000; // 1000ms = 1secs. GetInfo.iBytesMultiple = 1; printf("#mterml: Waiting for text on $SysOut pipe (location 1)....\n"); // use sta. above only in debug mode il_nix = 0; //counts how many times nothing in pipe. il_error = 0; //counts errors during DapLineGet while ( 1 ) { ul_dwTimeWait= 100; if ( (ilCount = DapLineGet(hdapSysGet, 100, sentencePtr, ul_dwTimeWait)) == -1) { printf("#SysOut: Error: DapLineGet fails reading $SysOut pipe\n"); il_error = il_error +1; if (il_error == 10 ) // too many read errors { printf("#SysOut: too many errors, reading pipe at 1\n"); fprintf(textLogfile,"#SysOut: too many errors, reading pipe at 1\n"); goto CLOSE; } continue; //jump to end of while statement loop and repeat while } if (ilCount == 0) { il_nix = il_nix +1; //increment nix in pipe counter if (il_nix == 5 ) // = 5 secs total wait. { printf("#mterml: timeout: no text in pipe SysOut after %ld tries\n",il_nix); // use above sta. only in debug mode break; //enough tries for now } continue; // if not full count, try again } // ok, something is in pipe, fetch contents: if (ilCount > 0) { printf("#SysOut: %s\n", sentence); fprintf (textLogfile, "#SysOut: %s\n", sentence); il_nix = 0; //reset the nothing in pipe counter } } //end of sysout pipe while(1) //--------------------- send #RESET= --------------------------------- // Send stuff to Custom module. printf("# Do you want D/A voltages reset to 0.0v ? (y/n): "); fprintf(textLogfile,"# Do you want D/A voltages reset to 0.0v ? (y/n): "); scanf ("%s", yon); // The MS-c++ compiler warns that yon was not set. yn = yon[0]; //yon = (char) getch(); // I could do this too, but there may be left overs. printf("# %1c \n", yn); fprintf(textLogfile, " %1c\r\n", yn); if (yn =='y' || yn =='Y' || yn =='1') { //Yes, send reset command to custom module DapStringPut (hdapCp2InPut, "#RESET= 1 0 0\n"); // fgets is used to read above line. It nees \n at end of input printf("#mterml: Sent #RESET= 1 0 0 to custom.\n"); fprintf(textLogfile, "#mterml: Sent #RESET= 1 0 0 to custom.\r\n"); } else { //No DapStringPut (hdapCp2InPut, "#RESET= 0 0 0\n"); printf("#mterml: Sent #RESET= 0 0 0 to custom (No change).\n"); fprintf(textLogfile,"#mterml: Sent #RESET= 0 0 0 to custom(No change).\r\n"); } //Now check the comm Cp2Out pipe for messages: look for #got#RESET= printf ("#mterml: Waiting for text on Cp2Out pipe ....\n"); il_nix = 0; //counts how many times nothing in pipe. il_error = 0; //counts errors during DapLineGet while ( 1 ) { ul_dwTimeWait= 1000; if ( (ilCount = DapLineGet(hdapCp2OutGet, 100, sentencePtr, ul_dwTimeWait)) == -1) { printf("#Cp2Out: Error: DapLineGet fails reading pipe\n"); il_error = il_error +1; if (il_error == 10 ) // too many read errors { printf("#Cp2Out: too many errors, reading pipe at 1\n"); fprintf(textLogfile,"#Cp2Out: too many errors, reading pipe at 1\n"); goto CLOSE; } continue; //jump to end of while statement loop and repeat while } if (ilCount == 0) { il_nix = il_nix +1; //increment nix in pipe counter if (il_nix == 5 ) // = 5 secs total wait. { printf("#mterml: timeout: no text in pipe Cp2Out after %ld tries\n",il_nix); break; //enough tries for now } continue; // if not full count, try again } // ok, something is in pipe, fetch contents: if (ilCount > 0) { printf("#Cp2Out: %s\n", sentence); fprintf (textLogfile, "#Cp2Out: %s\r\n", sentence); il_nix = 0; //reset the nothing in pipe counter //Check if #got#RESET= cfield1 = strtok (sentence, " "); if(strcmp (cfield1, "#got#RESET=\0") == 0) { printf("#mterml: mblk10m sent: #got#RESET=\n"); fprintf (textLogfile, "#mterml: mblk10m sent: #got#RESET=\r\n"); break; } } } //end of cp2out pipe while(1) //----------------------send mblk10.env ---------------------------- //Start Sending file contents to custom module on DAP printf("# Enter y when ready to transmit *.env file (y): "); scanf ("%s", yon); // The compiler warns that yon was not set. yn = yon[0]; printf("# %1c \n", yn); // Read and send the mblk10.env file containing control values //envname = "mblk10.env"; strcpy(envname, "mblk10.env\0"); printf("#\n# Opening envfile name: %s\n",envname); fprintf(textLogfile, "#\n# Opening envfile name: %s\n",envname); if ( (envfile = fopen (envname, "r") ) == NULL) { printf("# Error opening %s file. \n",envname); iret = 0; goto CLOSE; } //sentence is a char [100] size fgets( sentence, 100, envfile ); //printf("#Cp2In: %s \n", sentence); fprintf(textLogfile, "#Cp2In: %s \n", sentence); while ( !feof( envfile ) ) { DapStringPut (hdapCp2InPut, sentence); //DapLinePut adds a \r to string fgets( sentence, 100, envfile ); // fgets needs \n at end of input //printf("#Cp2In: %s \n", sentence); fprintf(textLogfile, "#Cp2In: %s \n", sentence); } //last line sent to custom should be "#END=..." //Now check the comm Cp2Out pipe for messages: look for #got#RESET= printf ("#mterml: Waiting for text on $Cp2Out pipe ....\n"); il_nix = 0; //counts how many times nothing in pipe. il_error = 0; //counts errors during DapLineGet while ( 1 ) { ul_dwTimeWait= 100; if ( (ilCount = DapLineGet(hdapCp2OutGet, 100, sentencePtr, ul_dwTimeWait)) == -1) { printf("#Cp2Out: Error: DapLineGet fails reading pipe\n"); il_error = il_error +1; if (il_error == 10 ) // too many read errors { printf("#Cp2Out: too many errors, reading pipe at 1\n"); fprintf(textLogfile,"#Cp2Out: too many errors, reading pipe at 1\n"); goto CLOSE; } continue; //jump to end of while statement loop and repeat while } if (ilCount == 0) { il_nix = il_nix +1; //increment nix in pipe counter if (il_nix == 5 ) // = 5 secs total wait. { printf("#mterml: timeout: no text in pipe Cp2Out after %ld tries\n",il_nix); break; //enough tries for now } continue; // if not full count, try again } // ok, something is in pipe, fetch contents: if (ilCount > 0) { printf("#Cp2Out: %s\n", sentence); fprintf (textLogfile, "#Cp2Out: %s\r\n", sentence); il_nix = 0; //reset the nothing in pipe counter //Check if #got#END= cfield1 = strtok (sentence, " "); if(strcmp (cfield1, "#got#END=\0") == 0) { printf("#mterml: mblk10m sent: #got#END=\n"); fprintf (textLogfile, "#mterml: mblk10m sent: #got#END=\r\n"); break; } } } //end of cp2out pipe while(1) fclose (envfile); fflush(textLogfile); //--------------------- read, scale HISTORY ----------------------------- // 1. Read and determine max and min of history. // 2. Rewind and Read, scale, integerize and send the history file printf("# Start sending mblk10.his file? (y): "); scanf ("%s", yon); yn = yon[0]; strcpy(histfilename, "mblk10.his\0"); printf("#\n# Opening history file name: %s\n",histfilename); fprintf(textLogfile, "# Opening history file name: %s\n", histfilename); if ( (histfile = fopen (histfilename, "r") ) == NULL) { printf("# Error opening %s file. \n",histfilename); iret = 0; goto CLOSE; } printf("# Scanning history for Max and Min\n"); fprintf(textLogfile, "# Scanning history for Max and Min\n"); il_nhist = 0; // Init counter of history points // Read the first data point in history. // Read lines and ignore unless begins with "#HISTORY=" while (1) { fgets (sentence, 100, histfile); cfield1 = strtok (sentence, " "); // Note that strcmp(s1,s2) = 0 when s1=s2 if( strcmp (cfield1, "#HISTORY=\0") == 0) { fgets (sentence, 100, histfile); cfield1 = strtok (sentence, " "); // next line should be data. Get it. if( strcmp (cfield1, "#END=\0") == 0) // is it the end of history? { printf("#Error: mterml: no data in history file\n"); goto STOP; } // ok, its not the end string, thus should be a history value sscanf (sentence, "%f%f", &histvalue1, &histvalue2 ); //get first data point il_nhist = il_nhist +1; //count no. of history lines vhistmax = histvalue1; vhistmin = histvalue1; if(vhistmax < histvalue2) vhistmax = histvalue2; if(vhistmin > histvalue2) vhistmin = histvalue2; break; //we have read the 1st data line and init max and min stores } // if its not "#HISTORY=" just ignore it. It is comment. } // We have first data point. // Now read rest of history until we hit an "#END=" while (1) { fgets (sentence, 100, histfile); cfield1 = strtok (sentence, " "); if (strcmp (cfield1, "#END=\0") ==0) { printf("# End of history scan after %ld points\n", il_nhist); printf("# Max hist value = %f \n", vhistmax); printf("# Min hist value = %f \n", vhistmin); fprintf(textLogfile, "# End of history scan after %ld points\n", il_nhist); fprintf(textLogfile, "# Max hist value = %f \n", vhistmax); fprintf(textLogfile, "# Min hist value = %f \n", vhistmin); break; //jump out of while loop } // If not #END= then must still be data. No garbage check for now. sscanf (sentence, "%f%f", &histvalue1, &histvalue2 ); //get data point il_nhist = il_nhist +1; if ( histvalue1 > vhistmax) vhistmax = histvalue1; if ( histvalue1 < vhistmin) vhistmin = histvalue1; if ( histvalue2 > vhistmax) vhistmax = histvalue2; if ( histvalue2 < vhistmin) vhistmin = histvalue2; } //end of max min scan loop xabs1 = fabs(vhistmax); xabs2 = fabs(vhistmin); if ( xabs1 >= xabs2) { xabsmax = xabs1; // Largest is the +ve tensile side printf("#Largest Magnitude history point is %f\n", vhistmax); fprintf(textLogfile, "#Largest Magnitude history point is %f\n", vhistmax); printf("# Enter the Voltage to scale this value to. : "); scanf("%f", &desiredVoltmax); fprintf(textLogfile, "# Enter the Voltage to scale this value to. : %f\n",desiredVoltmax); scaledata = (desiredVoltmax/xabsmax); } else { xabsmax = xabs2; // Largest is the -ve side printf("#Largest Magnitude history point is %f\n", vhistmin); fprintf(textLogfile, "#Largest Magnitude history point is %f\n", vhistmin); printf("# Enter the Voltage to scale this value to. : "); scanf("%f", &desiredVoltmax); fprintf(textLogfile, "# Enter the Voltage to scale this value to. : %f\n ",desiredVoltmax); scaledata = (fabs(desiredVoltmax) /xabsmax); } printf("#Scale factor : %f / %f = %f \n", fabs(desiredVoltmax), xabsmax, scaledata); fprintf(textLogfile, "#Scale factor : %f / %f = %f \n", fabs(desiredVoltmax), xabsmax, scaledata); printf("#Enter value of any Mean shift voltage: "); scanf("%f", &meanshift); fprintf(textLogfile, "#meanshift= %f\n", meanshift); fflush(textLogfile); //----------------------- send HISTORY ---------------------------------- // // scaling and shift numbers are ready. Read history again // scale etc then send to DAP custom module mblk10. rewind (histfile); // read and discard lines down to "#HISTORY=" line while (1) { fgets (sentence, 100, histfile); // "sentence" is probably destroyed by strtok, thus: strcpy (sentence2, sentence); cfield1 = strtok (sentence, " "); // Note that strcmp(s1,s2) = 0 when s1=s2 if( strcmp (cfield1, "#HISTORY=\0") == 0) break; // if its not "#HISTORY=" just ignore it. It is comment. } DapStringPut (hdapCp2InPut, sentence2); //Next lines should be data lines. Check for #END= // There should be il_nhist data point lines, as counted above scale1 = 3276.7; //scale 10v to 16 bit integer il = 0; while (1) { fgets (sentence, 100, histfile); strcpy (sentence2, sentence); cfield1 = strtok (sentence, " "); //There could be comment after data if( strcmp (cfield1, "#END=\0") == 0) break; //must be data //thus extract 3 fields only sscanf (sentence2, "%f%f%ld", &histvalue1, &histvalue2, &nramps); il = il +1; x = histvalue1 * scaledata + meanshift; //peak or valley voltage x2 = histvalue2 * scaledata + meanshift; //peak or valley voltage if ( x > 10.0 || x < -10.0 || x2 > 10.0 || x2 < -10.0 ) { printf("#Error: mterml: history volt exceeds +-10v: pt no. %ld \n", il); printf("#Original hist pt is= %f %f,* multiplier= %f,+ meanshift= %f \n", histvalue1, histvalue2, scaledata, meanshift ); fprintf(textLogfile, "#Error: mterml: hist volt exceeds +-10v: pt no. %ld \n", il); fprintf(textLogfile, "#Original hist1,2 pts= %f %f, * multiplier= %f,+ meanshift= %f \n", histvalue1, histvalue2, scaledata, meanshift ); goto STOP; } ivolt1 = (short)( x * scale1); ivolt2 = (short)( x2 * scale1); //printf("#int pt= %d \n", ivolt1); fprintf(textLogfile, "#cp2in: %d %d %ld\n", ivolt1, ivolt2, nramps); sprintf(sentence, "%d %d %ld \n", ivolt1, ivolt2, nramps); //must have space after number DapStringPut (hdapCp2InPut, sentence); if(il == il_nhist) break; } DapStringPut (hdapCp2InPut, "#END= \n"); //last line sent to custom should be "#END=..." printf("#mterml: sent #END= into cp2in\n"); fprintf(textLogfile, "#mterml: sent #END= into cp2in\n"); fclose (histfile); fflush(textLogfile); // All initial information has been transmitted to the DAP custom. // Now wait for the items for Shared Memory display to be returned. // Each such line will begin with a "#ForSM= " // When a "#READY=" string is received from custom, then turn on the A/D conv. //Now check the comm Cp2Out pipe for messages printf("# Waiting for text on $Cp2Out pipe ....\n"); fprintf(textLogfile, "# Waiting for text on $Cp2Out pipe ....\n"); il_nix = 0; //counts how many times nothing in pipe. il_error = 0; //counts errors during DapLineGet while ( 1 ) { ul_dwTimeWait= 100; if ( (ilCount = DapLineGet(hdapCp2OutGet, 100, sentencePtr, ul_dwTimeWait)) == -1) { printf("#Cp2Out: Error: DapLineGet fails reading pipe\n"); il_error = il_error +1; if (il_error == 10 ) // too many read errors { printf("#Cp2Out: too many errors, reading pipe at 1\n"); fprintf(textLogfile,"#Cp2Out: too many errors, reading pipe at 1\n"); goto CLOSE; } continue; //jump to end of while statement loop and repeat while } if (ilCount == 0) { il_nix = il_nix +1; //increment nix in pipe counter if (il_nix == 5 ) // = 5 secs total wait. { printf("#mterml: timeout: no text in pipe Cp2Out after %ld tries\n",il_nix); fprintf(textLogfile, "#mterml: timeout: no text in pipe Cp2Out after %ld tries\n",il_nix); // Keep looking. We must get the #READY= printf("#mterml: Still waiting for #READY= on Cp2Out...\n"); il_nix = 0; } continue; // if not full count, try again } // ok, something is in pipe, fetch contents: if (ilCount > 0) { printf("#Cp2Out: %s\n", sentence); fprintf (textLogfile, "#Cp2Out: %s\n", sentence); // Check if #READY= is the first field of this sentence cfield1 = strtok (sentence, " "); // Note that strcmp(s1,s2) = 0 when s1=s2 if( strcmp (cfield1, "#READY=\0") == 0) break; //jump out of while loop // Check for shared memory stuff if(strcmp (cfield1, "#ForSM=\0") == 0 ) { //yes its an item for sh mem, figure out which one: cfield2 = strtok (NULL, " "); if (cfield2 == NULL) { printf("#mterml: error: no 2nd field after #ForSM=\n"); goto STOP; } cfield3 = strtok (NULL, " "); if (cfield3 == NULL) { printf("#mterml: error: no 3rd field after #ForSM=\n"); goto STOP; } sscanf (cfield3, "%li", &iln); if (strcmp (cfield2, "#PKERROR=\0") == 0) { // get the pkerror int s = shm + 102; *s = iln; fprintf(textLogfile, "#mterml: shmem PKERROR set %li\n",iln); //Series of if statm. code is not efficient, but it doesnt matter. } if (strcmp (cfield2, "#CMDERROR=\0") == 0) { s = shm + 103; *s = iln; fprintf(textLogfile, "#mterml: shmem CMDERROR set %li\n",iln); } if (strcmp (cfield2, "#RATE1=\0") == 0) { s = shm + 104; *s = iln; fprintf(textLogfile, "#mterml: shmem RATE1 set %li\n",iln); } if (strcmp (cfield2, "#RATE2=\0") == 0) { s = shm + 105; *s = iln; fprintf(textLogfile, "#mterml: shmem RATE2 set %li\n",iln); } if (strcmp (cfield2, "#THRESHOLD=\0") == 0) { s = shm + 106; *s = iln; fprintf(textLogfile, "#mterml: shmem THRESHOLD set %li\n",iln); } if (strcmp (cfield2, "#DEPLETE=\0") == 0) { s = shm + 107; *s = iln; fprintf(textLogfile, "#mterml: shmem DEPLETE set %li\n",iln); } if (strcmp (cfield2, "#STEPSIZE=\0") == 0) { s = shm + 108; *s = iln; fprintf(textLogfile, "#mterml: shmem STEPSIZE set %li\n",iln); } if (strcmp (cfield2, "#MAXREPEATS=\0") == 0) { s = shm + 109; *s = iln; fprintf(textLogfile, "#mterml: shmem MAXREPEATS set %li\n",iln); } if (strcmp (cfield2, "#NCHAN=\0") == 0) { s = shm + 9; *s = iln; nchan=(short)iln; fprintf(textLogfile, "#mterml: shmem NCHAN set %li\n",iln); } } //end of #ForSM line decode s = shm; //leave shm same, and use s to index iln = 0; *s = iln; //turns on shared mem write flag for all // initilize some of the other sh mem locs s=shm+1; iln = 1; *s = iln; //set last command to Run // In case nchan has not been set yet, or gets delayed before its transmission from mblk10 nchan=2; //set it now to something. iln=0; s=shm+10; *s=iln; //set his reps to 0 s=shm+11; *s=iln; //set blk no to 0 s=shm+12; *s=iln; //set ramp no. to 0 s=shm+13; *s=iln; //set a/d buffs written to 0 s=shm+14; *s=iln; //set max ch 1 to 0 s=shm+15; *s=iln; //set min ch 1 to 0 s=shm+16; *s=iln; //set max ch 2 to 0 s=shm+17; *s=iln; //set min ch 2 to 0 s=shm+18; *s=iln; //set max ch 3 to 0 s=shm+19; *s=iln; //set min ch 3 to 0 s=shm+20; *s=iln; //set max ch 4 to 0 s=shm+21; *s=iln; //set min ch 4 to 0 s=shm+23; *s=iln; //set Bin output vector to 0 s=shm+24; *s=iln; //set loopget working Buffer to 0 s=shm+25; *s=iln; //set nl_cycleno to 0 (later written from loopget) s=shm+26; *s=iln; //set get next cycle flag to 0. ( " ) s=shm+101; *s=iln; //set status to 0 // init the To_mblk10 stuff to 0 // These are for the requested changes from mstun s=shm+202; *s=iln; // PKERROR s=shm+203; *s=iln; // CMDERROR s=shm+204; *s=iln; // RATE1 s=shm+205; *s=iln; // RATE2 s=shm+206; *s=iln; // THRESHOLD s=shm+207; *s=iln; // DEPLETE s=shm+208; *s=iln; // STEPSIZE s=shm+209; *s=iln; // MAXREPEATS // also init to 0 the "From_mblk10" column of mstun display s=shm+102; *s=iln; // PKERROR s=shm+103; *s=iln; // CMDERROR s=shm+104; *s=iln; // RATE1 s=shm+105; *s=iln; // RATE2 s=shm+106; *s=iln; // THRESHOLD s=shm+107; *s=iln; // DEPLETE s=shm+108; *s=iln; // STEPSIZE s=shm+109; *s=iln; // MAXREPEATS // Sh mem area 2 items have already been initilized. // Just set the nexFreeBuffNo pointing to buff 1 flag s=shm+22; il_nextFreeBuff = 1; //The next free buffer is buff 1 *s = il_nextFreeBuff; //If the nextFreeBuff is still equal to 1 it has not //been read by loopmaster and we have a circular buffer wrap //condition. This cannot (or should not occur initially) s2 = shm2 + il_nextFreeBuff; i = *s2; if (i == 1) { // The next buff is already full! We have a wrap condition. // or error. Assuming a wrap condition set it to 0 thus // causing an overwrite when next data set comes in. printf("#Warning: mterml: binBuff overwrite: buffno. %li\n",il_nextFreeBuff); printf("# For some reason the buffers for feedback data are full?\n"); fprintf(textLogfile, "#Warning: mterml: binBuff overwite: buffno= %li\n",il_nextFreeBuff); fprintf(textLogfile, "# For some reason the buffers for feedback data are full?\n"); i=0; *s2 = i; //this will free up the buffer. } il_nix = 0; //reset the nothing in pipe counter } } //end of cp2out pipe while(1) // Also set the write to sh mem2 or binary file switch: il_binVector s=shm+23; il_binVector = 0; // =0 write to sh mem2, =1 write to diskfiles. *s = il_binVector; printf("#mterml: Binary data will be written to sh mem2 \n"); printf("# You can change this to disk writes in mstun.\n"); printf("# Now is a good time to change it.\n"); fprintf(textLogfile, "#mterml: Binary data will be written to sh mem2 \n"); fflush(textLogfile); printf("#mterml: Got #READY= on Cp2Out pipe.\n"); fprintf(textLogfile, "#mterml: Got #READY= on Cp2Out pipe.\n"); printf("# Checklist: SPAN Open fully ?\n"); printf("# D/A_0 Connected to COMMAND ?\n"); printf("# A/D_0 Connected to FEEDBACK ?\n"); printf("# A/D_1 Connected to OtherChan ?\n"); printf("# Gain OK ?\n"); printf("# Saved old rawLoopOut.txt file ?\n"); // Start the A/Ds printf("# Start A/Ds in DAP? (y): "); scanf ("%s", yon); yn = yon[0]; DapStringPut (hdapSysPut, "START A\r"); printf("#mterml: Sent START A to *.dap to start a/ds\n"); fprintf(textLogfile, "#mterml: Sent START A to *.dap to start a/ds\n"); //Tell mblk10 that A/Ds are running DapStringPut (hdapCp2InPut, "#ADON= 1 \n"); printf("#mterml: Sent #ADON= 1 to mblk10. \n"); fflush(textLogfile); fflush(binLogfile); il_adbuffsWritten = 0; MONITOR: strcpy (modename, "monitor\0"); //While in the MONITOR state the PCside program will // 1. check the $Binout pipe for A/D samples and log them to // the binary output file or shared mem 2. // 2. check the $Sysout pipe for text from the *.dap or mblk10 // and print it to screen and log it in the text log file. // printf etc from either modules will end up in this pipe. // 3. check the Cp2Out pipe for text from DAP custom // and act on it and log to text log file. // 4. check for a user interaction using program "mstun" and // shared memory. Then send approrpiate command to DAP custom //Check for content in $Binout pipe // blocksize for disk is probably 1024 (see top of program, was originally 500) // Data is read into buffer BinData[iBlockSize] //printf("# Flush BinGet pipe? (y): "); //scanf ("%s", yon); //yn = yon[0]; //DapInputFlush(hdapBinGet); il_nix = 0; //counts how many times nothing in pipe. il_error = 0; //counts errors during DapLineGet while (1) { //ul_dwTimeWait= 100; //if ( (ilBCount = DapBufferGetEx(hdapBinGet, &GetBin, &BinData)) == -1) // Attempt to read 1024 words. ilBCount should be 4 * BUF_SIZE ilBCount = DapBufferGet( hdapBinGet, sizeof(short)*BUF_SIZE, databuffer); if (ilBCount == -1) { // failure here means not enough data in buffer probably il_error = il_error+1; printf("#mterml: MONITOR: Error reading BinOut pipe: \n# %s\n", DapLastErrorTextGet(szTemp, sizeof(szTemp) ) ); fprintf(textLogfile, "#mterml: MONITOR: Error reading BinOut pipe: \n# %s\n", DapLastErrorTextGet(szTemp, sizeof(szTemp) ) ); if (il_error == 10 ) // too many read errors { printf("#BINOUT: too many errors, reading pipe at MONITOR\n"); fprintf(textLogfile,"#BINOUT: too many errors, reading pipe at MONITOR\n"); goto CLOSE; } continue; //jump to end of while statement loop and repeat while } if (ilBCount == 0) // unclear if this would actually occur { il_nix = il_nix +1; //increment nix in pipe counter if (il_nix == 2 ) { printf("#mterml: timeout: no words in pipe BinOut after %ld tries\n",il_nix); // use above sta. only in debug mode break; //enough tries for now, exit while() loop } continue; // if not il_nix full count, try again } // ok, something was in pipe, write out contents: should be BUF_SIZE words if (ilBCount > 0) { //Check to see where to put data: sh_mem2 or disk? if(il_binVector == 0) { // Data should go to sh mem2 //also check for max and mins. Only 2 chans are active now. imaxChan0 = -32767; //init the max, min checkers iminChan0 = 32767; imaxChan1 = -32767; iminChan1 = 32767; //Next free buffer number is in il_nextFreeBuff s2 = shm2 + (il_nextFreeBuff * BUF_SIZE); // 1024 (BUF_SIZE) is in 16 bit words ilbuffstoremax = (long int) (( (short int)((BUF_SIZE - 7)/nchan) ) * nchan +6 ); //Note: The 1st 7 words of buffer are not data j=databuffer[0]; // = 32767 marker *s2=j; fprintf(textLogfile, "#ml: Buf loc 0 = %i\n", j); j=databuffer[1]; // = -32767 marker s2=s2+1; *s2=j; fprintf(textLogfile, "#ml: Buf loc 1 = %i\n", j); j=databuffer[2]; // = 0 2 words for block no. s2=s2+1; *s2=j; fprintf(textLogfile, "#ml: Buf loc 2 = %i\n", j); j=databuffer[3]; // = 1 s2=s2+1; *s2=j; fprintf(textLogfile, "#ml: Buf loc 3 = %i\n", j); j=databuffer[4]; // = 0 2 words for buffer no. s2=s2+1; *s2=j; fprintf(textLogfile, "#ml: Buf loc 4 = %i\n", j); j=databuffer[5]; // = 1 s2=s2+1; *s2=j; fprintf(textLogfile, "#ml: Buf loc 5 = %i\n", j); j=databuffer[6]; // = nwords in this buffer s2=s2+1; *s2=j; fprintf(textLogfile, "#ml: Buf loc 6 = %i\n", j); if(j != (short int)ilbuffstoremax) { //Not the same, this is a problem printf("#mt: Error: buff last loc [6]= %i not= ilbuffstoremax= %li\n", j,ilbuffstoremax); fprintf(textLogfile, "#mt: Error: buff last loc [6]= %i not= ilbuffstoremax= %li\n", j,ilbuffstoremax); } i=6; while(1) { i=i+1; j=databuffer[i]; //The 1st data pt read above will be a/d ch 0 if(j>imaxChan0) imaxChan0=j; if(jimaxChan1) imaxChan1=j; if(j= (short)( ilbuffstoremax ) )break; } // increment the file counter in sh mem il_adbuffsWritten= il_adbuffsWritten + 1; s=shm+13; *s= il_adbuffsWritten; //place a copy into sh mem //write the max, mins into sh mem1 s=shm+14; iln = (long)(imaxChan0); *s= iln; s=shm+15; iln = (long)(iminChan0); *s= iln; s=shm+16; iln = (long)(imaxChan1); *s= iln; s=shm+17; iln = (long)(iminChan1); *s= iln; //Set to buff empty/full flag to full = 1 s2 = shm2 + il_nextFreeBuff; i=1; *s2=i; printf("#mt:wrote shm buff= %li\n",il_nextFreeBuff); // Buff has been written. Ck next buff's flag il_nextFreeBuff = il_nextFreeBuff+1; if(il_nextFreeBuff == (MAX_BFILES+1) ) il_nextFreeBuff = 1; //wrap to 1st buff //Check the flag to see if this buff is actually free: //If the new flag is still equal to 1 it has not //been read by loopmaster and we have a circular buffer wrap //condition. In that case we will clobber the old contents and force the // flag to 0. s2 = shm2 + il_nextFreeBuff; i = *s2; if (i != 0) // 0=free, 1=full { // The next buff is already full! We have a wrap condition. // or error. Assuming a wrap condition set it to 0 thus // causing an overwrite when next data set comes in. printf("#Warning: mt: binBuff overwrite: buffno. %li\n",il_nextFreeBuff); printf("# For some reason the buffers for feedback data are full?\n"); printf("# Have you turned on LOOPMASTER ?\n"); fprintf(textLogfile, "#Warning: mt: binBuff overwite: buffno= %li\n",il_nextFreeBuff); fprintf(textLogfile, "# For some reason the buffers for feedback data are full?\n"); i=0; *s2 = i; //this will force free up the buffer. } //Ok, flag was, or is now, 0 (available for write) //Save the next free buff no. into sh mem1 so that loopmaster can read it. s=shm+22; *s= il_nextFreeBuff; } //------------------------------------------------------------------------write to bin file if(il_binVector == 1) // See if bin output is to files { // Data should go to disk files fwrite (databuffer, 1, sizeof(short)*BUF_SIZE, bfile); // Close the file written to, and open a new binary file fclose (bfile); // increment the file counter in sh mem il_adbuffsWritten= il_adbuffsWritten + 1; s=shm+13; *s= il_adbuffsWritten; //place a copy into sh mem fprintf(binLogfile, "%s\n", binfilename ); //write the ascii filename to binLogfile ibinfileno=ibinfileno+1; if (ibinfileno == (MAX_BFILES+1) ) ibinfileno=1; // reset to first filename sprintf(binfilename, "bin%d\0", ibinfileno ); printf("#mterml: Opening binary feedback file = %s\n", binfilename); if ( (bfile = fopen (binfilename, "wb") ) == NULL) { printf("#mterml: Error opening binary feedback file= %s\n", binfilename); iret = 0; goto CLOSE; } //also check for max and mins. Only 2 chans are active now. imaxChan0 = -32767; //init the max, min checkers iminChan0 = 32767; imaxChan1 = -32767; iminChan1 = 32767; i=6; //the first 7 words of databuffer are not data points //Note that the first word is in loc [0] while(1) { i=i+1; j= databuffer[i]; if(j>imaxChan0) imaxChan0=j; if(jimaxChan1) imaxChan1=j; if(j= (short)(ilbuffstoremax ) )break; } //write the max, mins into sh mem1 s=shm+14; iln = (long)(imaxChan0); *s= iln; s=shm+15; iln = (long)(iminChan0); *s= iln; s=shm+16; iln = (long)(imaxChan1); *s= iln; s=shm+17; iln = (long)(iminChan1); *s= iln; } //end of "if" for vector to disk file il_nix = 0; //reset the nothing in pipe counter break; // exit while() loop } } //end of $BinOut pipe while(1) fflush(binLogfile); //----------------------------------------------------Check SysOut pipe //printf("# Waiting for text on $SysOut pipe ....\n"); // use sta. above only in debug mode // While running we don't really expect to get much here, and we // dont want the binary pipe above to get plugged up. il_nix = 0; //counts how many times nothing in pipe. il_error = 0; //counts errors during DapLineGet while ( 1 ) { ul_dwTimeWait= 1; //the number is in milliseconds if ( (ilCount = DapLineGet(hdapSysGet, 100, sentencePtr, ul_dwTimeWait)) == -1) { printf("#SysOut: Error: DapLineGet fails reading $SysOut pipe\n"); il_error = il_error +1; if (il_error == 10 ) // too many read errors { printf("#SysOut: too many errors, reading pipe at MONITOR\n"); fprintf(textLogfile,"#SysOut: too many errors, reading pipe at MONITOR\n"); goto CLOSE; } continue; //jump to end of while statement loop and repeat while } if (ilCount == 0) { il_nix = il_nix +1; //increment nix in pipe counter if (il_nix == 2 ) // = 2 millisecs total wait, given ul_dwTimeWait above. { //printf("#mterml: timeout: no text in pipe SysOut after %d tries\n",il_nix); // use above sta. only in debug mode break; //enough tries for now } continue; // if not full count, try again } // ok, something is in pipe, fetch contents: if (ilCount > 0) { printf("#SysOut: %s\n", sentence); fprintf (textLogfile, "#SysOut: %s\n", sentence); il_nix = 0; //reset the nothing in pipe counter } } //end of sysout pipe while(1) //----------------------------------------------------Check Cp2Out pipe //Check text pipe Cp2Out from DAP custom //printf("#mterml: MONITOR: check for text on Cp2Out pipe ....\n"); // use above sta. only in debug mode il_nix = 0; //counts how many times nothing in pipe. il_error = 0; //counts errors during DapLineGet while ( 1 ) { ul_dwTimeWait= 1; if ( (ilCount = DapLineGet(hdapCp2OutGet, 100, sentencePtr, ul_dwTimeWait)) == -1) { printf("#Cp2Out: Error: DapLineGet fails reading Cp2Out pipe\n"); il_error = il_error +1; if (il_error == 10 ) // too many read errors { printf("#Cp2Out: too many errors, reading pipe at MONITOR\n"); fprintf(textLogfile,"#Cp2Out: too many errors, reading pipe at MONITOR\n"); goto CLOSE; } continue; //jump to end of while statement loop and repeat while } if (ilCount == 0) { il_nix = il_nix +1; //increment nix in pipe counter if (il_nix == 2 ) // = 2 millisecs total wait. { //printf("#mterml: timeout: no text in pipe SysOut after %d tries\n",il_nix); // use above sta. only in debug mode break; //enough tries for now } continue; // if not full count, try again } // ok, something is in pipe, fetch contents: if (ilCount > 0) { printf("#Cp2Out: %s\n", sentence); fprintf (textLogfile, "#Cp2Out: %s\n", sentence); // Check for stuff to be sent to shared mem. // The only things that should be coming here are // HISREPEATS=, BLOCKNO=, and maybe RAMPNO= cfield1 = strtok (sentence, " "); if(strcmp (cfield1, "#ForSM=\0") == 0 ) { //yes its an item for sh mem, figure out which one: cfield2 = strtok (NULL, " "); if (cfield2 == NULL) { printf("#mterml.MONITOR: error: no 2nd field after #ForSM=\n"); goto STOP; } cfield3 = strtok (NULL, " "); if (cfield3 == NULL) { printf("#mterml.MONITOR: error: no 3rd field after #ForSM=\n"); goto STOP; } sscanf (cfield3, "%li", &iln); if (strcmp (cfield2, "#HISREPEATS=\0") == 0) { s = shm + 10; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem HISREPEATS set %li\n",iln); } if (strcmp (cfield2, "#BLOCKNO=\0") == 0) { s = shm + 11; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem BLOCKNO set %li\n",iln); } if (strcmp (cfield2, "#RAMPNO=\0") == 0) { s = shm + 12; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem RAMPNO set %li\n",iln); } if (strcmp (cfield2, "#STATUS=\0") == 0) { s = shm + 101; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem STATUS set %li\n",iln); } //other ForSH items should be transfered during PAUSE // Nah... keep things simple, do it all here: if (strcmp (cfield2, "#PKERROR=\0") == 0) { s = shm + 102; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem PKERROR set %li\n",iln); } if (strcmp (cfield2, "#CMDERROR=\0") == 0) { s = shm + 103; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem CMDERROR set %li\n",iln); } if (strcmp (cfield2, "#RATE1=\0") == 0) { s = shm + 104; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem RATE1 set %li\n",iln); } if (strcmp (cfield2, "#RATE2=\0") == 0) { s = shm + 105; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem RATE2 set %li\n",iln); } if (strcmp (cfield2, "#THRESHOLD=\0") == 0) { s = shm + 106; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem THRESHOLD set %li\n",iln); } if (strcmp (cfield2, "#DEPLETE=\0") == 0) { s = shm + 107; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem DEPLETE set %li\n",iln); } if (strcmp (cfield2, "#STEPSIZE=\0") == 0) { s = shm + 108; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem STEPSIZE set %li\n",iln); } if (strcmp (cfield2, "#MAXREPEATS=\0") == 0) { s = shm + 109; *s = iln; fprintf(textLogfile, "#mterml.MONITOR: shmem MAXREPEATS set %li\n",iln); } if (strcmp (cfield2, "#NCHAN=\0") == 0) { s = shm + 9; *s = iln; nchan=(short)iln; fprintf(textLogfile, "#mterml.MONITOR: shmem NCHAN set %li\n",iln); } }//end of check for ForSH il_nix = 0; //reset the nothing in pipe counter } } //end of cp2out pipe read while(1) fflush(textLogfile); //-----------------------------------------------------------------Check Sh mem 1 //Check the shared memory area to see if a "mstun" program // has made an entry. For a map of the shared memory area // see file "shmemoryMap.txt" iln = *shm; if (iln != -1)goto MONITOR; //no request this time. //mstun has written a -1 in loc. 0, a command has been sent s = shm +1; //set prt to 2nd loc. iln2 = *s; printf("#mterml.MONITOR: got mstun command= %li\n",iln2); fprintf(textLogfile,"#mterml.MONITOR: got mstun command= %li\n",iln2); if(iln2 == 999) { // STOP everything and EXIT (Unclear whether one should send RESET or STOP to DAP) DapLinePut (hdapSysPut, "RESET"); printf("#mterml.MONITOR: sent RESET to DAP\n"); fprintf(textLogfile,"#mterml.MONITOR: sent RESET to DAP\n"); DapStringPut (hdapCp2InPut, "#STOP= 999 \n"); printf("#mterml.MONITOR: Sent #STOP= 999 to mblk10. \n"); fprintf(textLogfile,"#mterml.MONITOR: Sent #STOP= 999 to mblk10. \n"); s= shm+101; *s=999; //set status s = shm; *s = 0; //allow mstun access to sh mem goto STOP; } if(iln2 == 0) { //PAUSE request DapStringPut (hdapCp2InPut, "#PAUSE= 0 0 0 \n"); printf("#mterml.MONITOR: Sent #PAUSE= 0 0 0 to mblk10. \n"); fprintf(textLogfile,"#mterml.MONITOR: Sent #PAUSE= 0 0 0 to mblk10. \n"); s = shm; *s = 0; goto MONITOR; //command to continue will be sent from mstun } if(iln2 == 1) { //RUN request //DapStringPut (hdapCp2InPut, "#RUN= 1 \n"); //printf("#mterml.MONITOR: Sent #RUN= 1 to mblk10. \n"); //fprintf(textLogfile,"#mterml.MONITOR: Sent #RUN= 1 to mblk10. \n"); DapStringPut (hdapCp2InPut, "#CONTINUE= 0 0 0\r"); printf("#mterml.MONITOR: sent #CONTINUE= 0 to mblk10\n"); fprintf(textLogfile,"#mterml.MONITOR: sent #CONTINUE= 0 to mblk10\n"); s = shm; *s = 0; goto MONITOR; } if(iln2 == 1000) { //send command to reset the DAP DapLinePut (hdapSysPut, "RESET"); printf("#mterml.MONITOR: sent RESET to DAP\n"); fprintf(textLogfile,"#mterml.MONITOR: sent RESET to DAP\n"); // Send mblk10.dap file to DAP /*Windos: iret = DapConfig(hdapSysPut, "MBLK10.DAP"); */ iret = DapConfig(hdapSysPut, "mblk10.dap"); if(iret != 1) { printf("#mterml.MONITOR Error sending mblk10.dap to DAP\n"); printf("#mterml.MONITOR Make sure mblk10.dap is in same folder as mterml\n"); goto CLOSE; } printf("#mterml.MONITOR: mblk10.dap file sent to DAP\n"); s = shm; *s = 0; //allow mstun access to sh mem goto MONITOR; } if(iln2 == 1001) { //Start mblk10 DapStringPut (hdapSysPut, "START C\r"); printf("#mterml.MONITOR: sent START C to DAP to start mblk10\n"); fprintf(textLogfile,"#mterml.MONITOR: sent START C to DAP to start mblk10\n"); s = shm; *s = 0; //allow mstun access to sh mem goto MONITOR; } if(iln2 == 1100) { //Stop mblk10 DapStringPut (hdapSysPut, "STOP C\r"); printf("#mterml.MONITOR: sent STOP C to DAP to stop mblk10\n"); fprintf(textLogfile,"#mterml.MONITOR: sent STOP C to DAP to stop mblk10\n"); s = shm; *s = 0; //allow mstun access to sh mem goto MONITOR; } if(iln2 == 1200) { //Stop & exit printf("#mterml.MONITOR: got STOP mterml\n"); fprintf(textLogfile,"#mterml.MONITOR: got STOP mterml\n"); s = shm; *s = 0; //allow mstun access to sh mem goto CLOSE; } if(iln2 == 2) //PKERROR change { //get new value in sh mem s=shm+202; iln3=*s; sprintf(sentence, "#ILPKERROR= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILPKERROR= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILPKERROR= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 3) //CMDERROR change { //get new value in sh mem s=shm+203; iln3=*s; sprintf(sentence, "#ILCMDERROR= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILCMDERROR= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILCMDERROR= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 4) //RATE1 change { //get new value in sh mem s=shm+204; iln3=*s; sprintf(sentence, "#ILRATE1= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILRATE1= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILRATE1= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 5) //RATE2 change { //get new value in sh mem s=shm+205; iln3=*s; sprintf(sentence, "#ILRATE2= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILRATE2= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILRATE2= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 6) //THRESHOLD change { //get new value in sh mem s=shm+206; iln3=*s; sprintf(sentence, "#ILTHRESHOLD= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILTHRESHOLD= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILTHRESHOLD= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 7) //DEPLETE change { //get new value in sh mem s=shm+207; iln3=*s; sprintf(sentence, "#ILDEPLETE= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILDEPLETE= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILDEPLETE= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 8) //STEPSIZE change { //get new value in sh mem s=shm+208; iln3=*s; sprintf(sentence, "#ILSTEPSIZE= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILSTEPSIZE= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILSTEPSIZE= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 9) //MAXREPEATS change { //get new value in sh mem s=shm+209; iln3=*s; sprintf(sentence, "#ILMAXREPEATS= %li \n",iln3); //must have space after no. DapStringPut (hdapCp2InPut, sentence); printf("#mterml.MONITOR: sent: #ILMAXREPEATS= %li to mblk10\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: sent: #ILMAXREPEATS= %li to mblk10\n",iln3); *shm=0; goto MONITOR; } if(iln2 == 1300) //Disk or Sh Mem2 buff write change { //get new value in sh mem s=shm+23; iln3=*s; //this is only used by mterml_sh2. Nothing is sent to mblk10 il_binVector = iln3; printf("#mterml.MONITOR: got bin Vector buff write= %li\n",iln3); fprintf(textLogfile,"#mterml.MONITOR: got bin Vector buff write= %li\n",iln3); *shm=0; goto MONITOR; } // if none of the above printf("#mterml.MONITOR: unknown sh mem Command= %li\n",iln2); fprintf(textLogfile,"#mterml.MONITOR:unknown sh mem Command= %li\n",iln2); *shm=0; //allow mstun to write another command goto MONITOR; STOP: strcpy (modename, "stop\0"); DapStringPut (hdapCp2InPut, "#STOP= 0 0 0\r"); printf("#ml: STOP= sent to mblk10\n"); fprintf(textLogfile, "#ml: STOP= sent to mblk10\n"); //Windos Sleep(100); isleep = sleep(5); //linux 5 sec sleep DapStringPut(hdapSysPut, "STOP\r"); printf("#ml: STOP\\r sent to *.dap \n"); fprintf(textLogfile, "#ml: STOP\\r sent to *.dap \n"); CLOSE: strcpy (modename, "close\0"); printf("#ml: Closing files: textLogfile and binLogfile\n"); fclose(textLogfile); fclose(binLogfile); printf("#ml: Closing Dap pipes: hdapSysPut, hdapSysGet, hdapBinGet\n"); DapHandleClose(hdapSysPut); DapInputFlush(hdapSysGet); DapHandleClose(hdapSysGet); DapInputFlush(hdapBinGet); DapHandleClose(hdapBinGet); //return (iret); } //end of main