Thread: How to get C program to respond to another softwares commands

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    20

    How to get C program to respond to another softwares commands

    Hey there,
    Ive got a C program here that works via user inputs on the keyboard and logs time of a machine.

    start count = 1
    error=2
    fixed error=3
    stop and log to file = 4
    quit=5

    when you look at the log file of time.
    it has:
    date and time of start
    date and time of stop
    elapsed time(in seconds)
    error time(in seconds)

    I would like for the program to be automatic and to start when the machine software starts.

    the big thing I would like is for the program to log time when the user hits "START" on the other program .
    so instead of having to hit 1 in the program itll automatically start when they hit "start" in this other program.
    when an error pops up, for it to start counting error time
    when they hit ok to correct the error for it to stop logging error time.

    when the machine run is done for it to stop and log everything.


    I have no idea how I would go about doing this.

    Any help is appreciated.

    ps: the machine software is os2 based
    though the pc has both windows and os2 on it...and you are able to switch back and forth between the 2 anytime.
    and Im not sure if I can get access to the source code of this software I am trying to get it to respond to
    Last edited by Euphorica; 06-26-2008 at 10:27 AM.

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    anyone?

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So you want to change the C program, given a specific software program/software. Will assume you cannot change the software of the machine.

    How is the machine connected to the PC? That is the first step.

    If it was connected through a serial port, you would need some functions to read information from the serial port, and do whatever you want with them.
    You would then press the buttons you want, see what values you get, and change the C program accordingly to respond to those values. Nothing else.

    Now, you cannot make your program start when the machine starts without using a third program. When the machine starts it MAY sent a value through the connection line that indicates its startup. If it does you need a program already running that will read this value. Either a third program that will execute your C-program, or your C-program itself.

    I suppose what you want is your program be executed when the OS starts. Then sleep and wait for a signal. In that case either you can have something like:

    while(..)
    sleep for some seconds;
    check the connection port for signal;

    or

    while(..)
    check the connection port for signal;

    or a more sophisticated way that is more complicated. Like making the OS call the program (somehow) when the machine connects. Which I don't think is possible without changing the driver of the port.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    I want the small c program counter/timer to respond to the output commands of the machine software.

    If you click "start" the little program will start to count time
    if an error occurs, for it to count error time
    when the error is fixed the operator must hit "enter" for the run to continue, so the program would respond to that and stop counting error time.

    and then when the machine run is done, for the program to stop...and idle to wait for the next commands.

    I have been researching a bunch of things...dll's, namedpipes, filemapping, all way above my knowledge and expertise.


    In the computer there is a what is called a BIF board and the machine hooks to that via what I believe is a serial cable.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The buttons "start", "enter" etc are on the machine right? Just to make clear.

    Well, as I said, the first thing you need to do is read the data send by the machine.

    Search the internet for appropriate functions. Or better, open a new topic asking about them. Make sure though that the machine connects indeed with a serial port.

    When you see how the functions read data from the serial port everything is easy, right?

    As I understand you have the program ready, but it reads values from the keyboard. Thus, from the console. So wherever you have scanf() for example, you will put something like read_from_serial_port(). Then you will get the valued sent. In order to see which value is sent you read it and printf() it on the screen. You will find then the value for each button of the machine. Find which one is for "start." Then instead of
    if character read from keyboard equals 1
    you ll have:
    if character read from serial port equals the value of button "start"

    Assuming that the machine does give you values through a serial port for each button pressed. And assuming it will give some value for an error.

    What you want to do is pretty easy. I hope I understand correctly what you are asking for.
    The communication of the machine and your computer is done through a serial port. So you use appropriate functions to read data from the serial port. This functions are pretty easy to use by the way.

    So:
    1) Make sure how the machine and computer connect. Then ask in the forum for the appropriate functions/tools or search the net
    2) I assume that the buttons are pressed on the machine, a program is running on the computer and that the program is ready, just need to change it to read the output of the machine and not the input from the console (keyboard)

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Good stuff. Sorry i wasnt clear on some things.


    The "start" command for the machine is in the software on the pc. when an error occurs the machine automacitally stops.
    then you must click ok(or hit enter on the keyboard) for the machine to continue working.

    when the run is done the machine will stop on its own.

    I want the program to respond to the "Start" button push, error stoppage, error fix(continue to work) and end run stoppage. then to log that information to a text file.

    you are correct in that i have a program already that can do this but it responds to inputs from the keyboard. I want it to respond automatically from the software commands .

    Im pretty sure its serial. Ill double check .

    what you are saying makes perfect sense. It should be giving a value through the serial to the machine to do various things(Start, stop with an error etc)


    I just have to figure out how to get that information. Ill research/ask around .


    Thanks for the help so far!!
    Last edited by Euphorica; 06-27-2008 at 01:56 PM.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Ok, just checked again.

    its NOT a serial cable...

    it is a 15 pin connector/ribben cable.(same look as a serial just a bit bigger) Should work the same as a serial though. just more pins, right?

    this will be so sweet if we can figure this out.




    here is the code to the program I currently have. it works via user inputs from the keyboard.


    Code:
    #include <stdio.h>
    #include <time.h>
    
    //This is a seperate function that is called whenever the data is to be save to a file.  It takes in as arguments the
    //time that the current recording started (time since last save or the time since the start command was issued), the 
    //time that the current recording ended and time that was spent (in seconds) in the error state.
    void print (time_t st, time_t end, double errortime);
    
    char *dfname (time_t stime);
    
    int main () {
    
    //VARIABLE DECLARATIONS
    time_t time1=0, time2=0,startdate,enddate;
    double count=0;
    int in=0;
    char state='i';
    
    printf("A $ will appear when program is waiting for next action.\nCommands\n========\n1)Start\t2)Error\t3)Error Fixed\t4)Write to File\t5)Quit");
    
    
    while (in!=5){
    
    printf("\n==================================================");
    	printf("\n$");
    	scanf("&#37;d",&in);
    
    	if (in==1&& state=='i'){
    		printf("\nStart signal has been given.  Start date has been recorded. \nNow in state:Run\n");
    		startdate=time(NULL);
    		state='r';
    
    	}
    	else if (in==1&&state!='i'){
    		printf("\nCan't \"Start\" unless current state is \"Idle\"\n");
    		printf("\nCurrent state is:");
    		if(state=='r')
    			printf("Run\n");
    		else if (state=='e')
    			printf("Error\n");
    		else printf("unknown\n");
    
    	}	
    
    	if (in==2&& state=='r'){
    		time1=time(NULL);
    		state='e';
    		printf("\nError had been detected.  Now in state:Error\n");
    	}
    	else if(in==2 && state!='r'){
    
    		printf("\nCan't have \"Error\" unless current state is \"Run\"\n");
    		printf("\nCurrent state is:");
    
    		if(state=='i')
    			printf("Idle\n");
    		else if (state=='e')
    			printf("Error\n");
    		else printf("unknown\n");
    
    	}
    	
    	if (in==3&& state=='e'){
    		time2=time(NULL);
    		count=count+difftime(time2,time1);
    		state='r';
    		printf("\nError has been fixed.  Back to state:Run\n");
    
    	}
    		
    	else if (in==3&& state!='e'){
    
    
    		printf("\nCan't have \"Fixed Error\" unless current state is \"Error\"\n");
    		printf("\nCurrent state is:");
    
    		if(state=='r')
    			printf("Run\n");
    		else if (state=='i')
    			printf("Idle\n");
    		else printf("unknown\n");
    
    	}
    	if (in==4&&state=='r') {
    		printf("\n\nFile save command has been detected.  End date has been stored.\nStart date will be reset.");
    		enddate=time(NULL);
    		
    		print(startdate,enddate,count);
    		count=0;
    		startdate=time(NULL);
    	}
    
    	
    
    	else if (in==4&&state!='r'){
    		printf("\n\nCan't save data unless state is run.  Fix errors or start program running.");
    	}
    printf("\n==================================================");
    }
    
    
    printf("\n==================================================");
    if (state=='e'){
    	time2=time(NULL);
    	count=count+difftime(time2,time1);
    }
    
    
    printf("\nQuit signal has been received. End date has been recorded.\nTotal error time will be calculated.");
    
    enddate=time(NULL);
    
    print(startdate,enddate,count);
    
    printf("\n==================================================");
    return 0;
    }
    
    
    
    
    
    
    
    void print (time_t st, time_t end, double errortime){
    	
    	double elapsed;
    	char filename[30],*temp;
    	int i=0,j=0;
    	FILE *userfile;
    	char *sdate,*edate;
    	struct tm savetime;
    	
    	elapsed=difftime(end,st);
    		
    	edate=ctime(&end);
    
    	while (*(edate+i)!='\n'){
    		if(*(edate+i)!=' '&&*(edate+i)!=':'){
    			filename[j]=*(edate+i);
    			j++;
    		}
    		i++;
    	}
    	filename[j]='.';
    	
    	filename[j+1]='t';
    
    	filename[j+2]='x';
    
    	filename[j+3]='t';
    
    	filename[j+4]='\0';
    
    	userfile=fopen(filename,"w");
    
    	if(userfile){
    		
    		printf("\nFile is being written to the default filename:%s",filename);	
    		fprintf(userfile, "\n\nTime Saved\n==========\n%s",edate);
    		sdate=ctime(&st);
    		fprintf(userfile, "Time Started\n============\n%s",sdate);
    		fprintf(userfile, "\n\nTime Elapsed\n============\n%.0lf",elapsed);
    		fprintf(userfile, "\n\nError Time\n==========\n%.0lf",errortime);
    
    	}
    
    	else{
    		printf("\n\nFile has not been successfully created.");
    	}
    	fclose(userfile);
    }
    Last edited by Euphorica; 06-27-2008 at 02:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling System Commands from within your program
    By xiver0m in forum C Programming
    Replies: 13
    Last Post: 04-18-2007, 12:28 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM