Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello guys, i am c programming user. i have a project using raspberry Pi connect with sensors. i am currently stuck how to make the value update in second. i had tried using gtk_label_set_text and g_timeout_add_seconds. but the temperature unable update itsself once run, it need to close and rerun again ti get its update value. the below is the code i current make, please help me correct it if there is wrong. the project is only want C programming not other language. the below code are


#include <wiringPi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <glib.h>


#define		BUFSIZE		128

// typedef unsigned char uchar;
// typedef unsigned int  uint;


float tempRead(void)
{
	float temp;
	int i, j;
    int fd;
	int ret;

	char buf[BUFSIZE];
	char tempBuf[5];
	
	fd = open("/sys/bus/w1/devices/28-0314979400b5/w1_slave", O_RDONLY);

	if(-1 == fd){
		perror("open device file error");
		return 1;
	}

	while(1){
		ret = read(fd, buf, BUFSIZE);
		if(0 == ret){
			break;	
		}
		if(-1 == ret){
			if(errno == EINTR){
				continue;	
			}
			perror("read()");
			close(fd);
			return 1;
		}
	}

	for(i=0;i<sizeof(buf);i++){
		if(buf[i] == 't'){
			for(j=0;j<sizeof(tempBuf);j++){
				tempBuf[j] = buf[i+2+j]; 	
			}
		}	
	}

	temp = (float)atoi(tempBuf) / 1000;

	close(fd);

	return temp;
}

int main(int argc, char *argv[])
{
	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !");
		return 1; 
	}
    float temp;
    while(1){
		temp = tempRead();
                char Str[100];
		// printf("Current temperature : %0.3f\n", temp);
                sprintf( Str, "Current temperature : %0.3f\n", temp);

     gboolean update_label_time (gpointer ptr) {
      // gchar *t = tempRead();
      gtk_label_set_text(GTK_LABEL(ptr), Str);
      // g_free (t);
      return G_SOURCE_CONTINUE;
    }


   gchar *t;
   GtkWidget *window;
   GtkWidget *label_time;

   gtk_init (&argc, &argv);
   GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);

   gtk_window_set_default_size(GTK_WINDOW(win), 250, 150);

   // t = tempRead();
   // label_time = tempRead;
   // g_free (t);

   GtkWidget *lbl = gtk_label_new (Str);

   // g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
   gtk_container_add (GTK_CONTAINER (win), lbl);

   g_timeout_add_seconds(1.0, update_label_time, lbl);
   // tempRead = gtk_timeout_add_seconds(1.0, update_label_time, lbl))

   gtk_widget_show_all (win);

   gtk_main();
    }


}


What I have tried:

i am still starter in c program, i cant tell too detail. you can run these code above if you have ds18b20 sensor.
Posted
Updated 12-Mar-20 22:29pm
Comments
markkuk 14-Mar-20 9:27am    
Why do you keep starting new threads with essentially the same question? Don't you read the answers to any of the previous ones?

1 solution

Why does it close? Because you told it to!
Look at your code:
C++
while(1){
       ...
       return G_SOURCE_CONTINUE;
    }
When the main function hits a return statement, that's it: the program ends. That is exactly what it is designed to do.

And since that return is the only way out of your while loop, the code below that loop is never, ever, executed.

Stop guessing: sit down and think about exactly what you are trying to do, plan it out manually, and then try to work out how to replicate that as English instructions. Then try to "execute" those English language instructions exactly and see if it works. If it doesn't, improve the instructions and try again.
When they do work, start translating them into code and testing that.

But at the moment, you are rushing right into code without thinking about what you need to do, and that will never work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900