How to get C program to respond to another softwares commands

This is a discussion on How to get C program to respond to another softwares commands within the C Programming forums, part of the General Programming Boards category; Hey there, Ive got a C program here that works via user inputs on the keyboard and logs time of ...

  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.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What about avoiding the whole cable/machine part, and look at the computer itself?

    What I'm thinking of is a keyboard intercept - when the user presses the keyboard key to start the machine, the key intercept or logger program begins timing, and stops when an error occurs and begins the error time, etc.

    You'd have to find such a utility for OS2, but OS2 has been around a long time, such a program should be available to modify/use, for your purpose.

    Good heavens though - OS2!

    Then for extra points and sweetness, make a bat file for the icon they click on in OS2 to start the machine, and have it start your intercept/logger program just before the program to run the machine. Using the same icon, I doubt anyone would be bothered in the least by your program.
    Last edited by Adak; 06-27-2008 at 02:36 PM.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, since the software is on the PC finding an utility as Adak says is a more normal way to do your job. But again how will it understand an error? The error will be a signal from the machine to the PC and nothing else.

    You could understand the error only from the 15-pin port. So I vote for reading information from the port, if there isn't another way.

    As for the connection, I don't know. There should be functions for that type of port also. Search if you want the net to see what the port is called and create a new topic on the forum. Or just create a topic, asking how to read data from a 15-pin port.

    Note that when you find how to read the data test them FIRST before you do anything else, so you know exactly what the machine sends/receives.

    Generally, the software for the machine should be changed. But since you don't have the source code...

    EDIT: I don't think it is the same, but that's technical stuff. As long as you can read the data sent/received we are happy
    Last edited by C_ntua; 06-27-2008 at 03:27 PM.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Another great idea Adak!!!
    thanks.


    I think im gonna try the original idea first though. And if it prooves to be overly difficult or unsolvable Ill look for another app/other way.


    lol yes, os2!

    I found the schematic for the cable!

    http://img77.imageshack.us/img77/3281/wiringwd2.jpg


    Its hard to read but it reads from top to bottom:

    Not used
    Not used
    Not used
    AENOUT-
    AENOUT+
    CONTACTOR+
    CONTACTOR-
    INTERLOCK+
    INTERLOCK-
    TERMINATE+
    TERMINATE-
    AENRIN-
    AENRIN+
    DATA/CLK-
    DATA/CLK+


    Id probably still have to double check with a volt meter what each pin does when commands are sent.

    and now to figure out how to get the program to read the correct information.


    thanks again for the help so far.
    Last edited by Euphorica; 06-27-2008 at 03:29 PM.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You don't need to check anything. Just find a proper function to read data from the port. Generally you do this:

    port_handler = open(PORT, ....);
    read(port_handler, ...);

    Then it is just like reading from a file. You will have also a function:

    wait_for_data(port_handler);

    etc etc

    The functions will do everything for you. I ll go ahead and create a topic to find these functions

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    I just made a new one

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, 10: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, 06:49 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21