|

楼主 |
发表于 2004-8-2 22:52:13
|
显示全部楼层
我最后使用了系统的定时器,关于定时的问题暂时解决了。但又遇到了一个奇怪的问题,看如下的代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <gtk/gtk.h>
- #include <signal.h>
- #include <sys/time.h>
- #include <errno.h>
- #include <string.h>
- #include "support.h"
- #define POINT_NUM 20
- static int ffd;
- GtkWidget *GUI;
- void flush(char buf[]) {
- static int hour, min, second;
- static gfloat vin[POINT_NUM], vout[POINT_NUM];
- int i;
- GtkWidget *widget;
- // char tstr[15] = "\0";
- // char str[20] = "Time:";
- sscanf(buf, "%d:%d:%d[6%lf:6%lf]", &hour, &min, &second, &vin[0], &vout[0]);
- printf("gui2: %s: %d, %d, %d, %f, %f\n", buf, hour, min, second, vin[0], vout[0]);
- for(i = 1; i < POINT_NUM; i++) {
- vin[i + 1] = vin[i];
- vout[i + 1] = vout[i];
- }
- // strcpy(str, tstr);
- widget = lookup_widget(GUI, "curve1");
- gtk_curve_set_vector(GTK_CURVE(widget), POINT_NUM, vin);
- widget = lookup_widget(GUI, "curve2");
- gtk_curve_set_vector(GTK_CURVE(widget), POINT_NUM, vout);
- widget = lookup_widget(GUI, "label3");
- // gtk_label_set_text(GTK_LABEL(widget), str);
- }
- int open_fifo() {
- char *fifo = "./fifo";
- if((ffd = open(fifo, O_RDONLY | O_NONBLOCK)) < 0) {
- fprintf(stderr, "can't open fifo: %s\n", strerror(errno));
- // exit(1);
- return -1;
- }
- return ffd;
- }
- gint read_fifo(gpointer data) {
- // void read_fifo(gpointer data, gint fd, GdkInputCondition cond) {
- char buff[64] = "\0";
- // GtkWidget *gui = (GtkWidget *)data;
- read(ffd, buff, 64);
- flush(buff);
- printf("gui: %s\n", buff);
- return 0;
- }
- void handle_timer(int sign) {
- if(sign == SIGALRM) {
- read_fifo(NULL);
- }
- }
- void start_timer() {
- struct itimerval tv;
- tv.it_value.tv_sec = 2; /* give 2 seconds for safety */
- tv.it_value.tv_usec = 0;
- tv.it_interval.tv_sec = 3;
- tv.it_interval.tv_usec = 0;
- signal(SIGALRM, handle_timer);
- setitimer(ITIMER_REAL, &tv, NULL);
- }
- void stop_timer() {
- struct itimerval tv;
- signal(SIGALRM, SIG_IGN);
- tv.it_value.tv_sec = 0;
- tv.it_value.tv_usec = 0;
- tv.it_interval.tv_sec = 0;
- tv.it_interval.tv_usec = 0;
- setitimer(ITIMER_REAL, &tv, NULL);
- }
复制代码
在主函数中,先调用定时器,然后运行gtk_main(),在上面的程序中,如果把read_fifo()中的flush语句注释掉,则它下面的printf语句打印了正确的数据,但如果使用flush函数,则其中的printf语句不能打印正确结果,而前面那个printf打印出空结果。为什么是这样?
我把定时器的值增大,问题依旧。 |
|