Thread: Interrupt Handler control+C

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Interrupt Handler control+C

    Hi everybody.
    I made a C program, but I'm having some doubts about how to implement an ISR. What I liked to do was that inside the loop instead of always opening and closing the files, write files only when made ^ C.
    Can you help me?
    Many thanks.

    Code:
    	while(1){
    	        logfile = fopen(filename, "a+");
    	        gpslogfile = fopen(filename2, "a+");
    
    
    	        if ((rs232res=read(rs232fd,rs232buf,MAXBUFLEN)) != -1)
    		{
    		        rs232buf[rs232res]='\0';
    			fprintf(gpslogfile, "%s\n", rs232buf);
    			strncpy(temp, rs232buf, 6);
    			temp[6] = '\0';
    			if(strcmp(temp, "$GPRMC") == 0)
    			{
    		                strtok(rs232buf, ",");
    		                xtime = strtok(NULL, ",");
    		                strtok(NULL, ",");
    		                xlat = strtok(NULL, ",");
    		                xns = strtok(NULL, ",");
    		                xlong = strtok(NULL, ",");
    		                xew = strtok(NULL, ",");
    		                fprintf(logfile, "--> %s - %s%s, %s%s\n", xtime, xlat, xns, xlong, xew);
    		                printf("--> %s - %s%s, %s%s\n", xtime, xlat, xns, xlong, xew);
    				strncpy(lastxtime, xtime, 10);
    				lastxtime[10] = '\0';
    			}
    			rs232buf[0] = '\0';
    		}
    
    
    		/* PACKET CREATED BY THE CLIENT AND BROADCASTED  */
    		sprintf(buf, "ID %d - time %s", msgID, lastxtime);
    
    //		usleep(100000);
    
    		time++;
    		if (time >= WAITTIME)
    		{
    			sendto(sockfdTX, buf, strlen(buf)*sizeof(char), 0, (struct sockaddr *)&their_addr, sizeof their_addr);
    			fprintf(logfile, "TX: ID %d - time %s - dest %s\n", msgID, lastxtime, inet_ntoa(their_addr.sin_addr));
    			printf("TX: ID %d - time %s - dest %s\n", msgID, lastxtime, inet_ntoa(their_addr.sin_addr));
    			msgID++;
    			time = 0;
    		}
    
    
    		fclose(logfile);
    		fclose(gpslogfile);
    
    		}

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    ISR? Last time I heard of them was in MS-DOS days, before windows 3 existed.

    In any event, those things are very operating system and compiler/library specific.

    Generally, however, it's a good idea to keep ISR as simple as possible. A common approach is for the ISR to set a flag, and for that flag to be checked in your processing loop and reset. For example, the flag might be a volatile int. The ISR sets it to non-zero value when CTRL-C is hit. Your loop writes to file when the value is non-zero and resets it to zero.

    Depending on how you want your program to interact with the user, there are more modern approaches (eg a GUI with a worker thread).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Ok, thanks... it is so old?
    But how can I transmit to the program that Im pressing ^C?
    It is only necessary that in the console, when you click ^ C, the program finishes and close the file.
    And in the loop how can I call de interrupt function?
    Thanks again.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Are you /completely/ sure you want to handle is a processor interrupt and not something like a user level signal? If so, you got to tell which processor architecture you are programming for so anyone can say anything useful.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Yes, I would like to have a user level signal. How does it work?

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    If you're on UNIX or UNIX-ish system, check your systems (or POSIX which I linked) manual for signal.h, sigaction(2) and then refer to other manuals if needed. ^C raises normally SIGINT, sometimes it is configured to SIGTERM and can be configured to pretty much anything.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    And if you're on Windows the venerable SetConsoleCtrlHandler (SetConsoleCtrlHandler Function (Windows)) will do the trick very nicely.

    I can see what you mean by ISR as these are pretty much the same thing, with the only difference really being implementation and level of abstraction (the old ISRs were much closer to the hardware).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sleeping or blocking an interrupt handler
    By jackb in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2010, 01:00 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  4. interrupt handler functions in Visual C++ 6.0
    By scromer in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-07-2002, 07:06 PM