处理数据 - C编程

处理数据 - C编程

问题描述:

伊夫这样写代码,从传感器读取数据:处理数据 - C编程

char c; 
do{ 
    while(!read(fd, &c, 1));  
} while (c!='$'); 

do{ 
    while(!read(fd, &c, 1));  
} while (c!=','); 

do{ 
    while(!read(fd, &c, 1)); 

    printf("%c",c); 
    fp = fopen("/var/www/Sensor_data.txt", "a"); 
    fprintf(fp, "%c%", c); 
    fclose(fp); 

} while (c!='\n'); 

的文件,那么看起来像

518.088131,0.272969,0.376242,0.522998,0.368944, 0.347478,0.343025,0.252323,0.612587,0.552753,0.120302,806.230415

我然后像一个办法procces这个数据,所以我把DA ta编号2,3,4,5,6,7,8(粗体字),并转换为新文件。 任何人都可以举一个这样的程序的例子吗?或者更喜欢修改我的程序,使其生成一个包含所有数据的文件,以及其他7个包含1个数据的文件。

+2

我不太同意你读数据的方式。如果您从传感器读取错误,您可能会陷入无限循环。 – SlySherZ 2014-10-31 13:34:09

+0

在do循环内部打开和关闭是SLOW。在do循环之前仅打开一次,然后在循环退出后仅关闭一次。 – user3629249 2014-11-01 05:41:16

+0

通过首先使用select()等待数据可用,将所有数据读入缓冲区,然后丢弃前导$ ....然后写入剩余的数据,执行读取将是一个非常好的主意,在单个写入中输出到输出文件。这将更快,操作系统的负担更小。 – user3629249 2014-11-01 05:44:05

像这样的事情会做的。可伸缩性不高,虽然:S

// This part has to go before main function starts 
char int_to_char(int n){ 
    return (char) n + (int)'0'; 
} 

// and this replaces what you already had 
int reading = 1; 
do{ 
    while(!read(fd, &c, 1));   // <- you should change this 

    printf("%c",c); 
    fp = fopen("/var/www/Sensor_data.txt", "a"); 
    fprintf(fp, "%c", c); 
    fclose(fp); 

    if (c == ',') 
     reading++ ; 

    else{   
     if (reading >= 2 && reading <= 8){    // 2nd to 8th reading 
      char temp[] = "/var/www/Sensor_data .txt"; // <- Added a space, which I'm 
                // gonna substitute with a number 
      temp[20] = int_to_char(reading);   // Substitute the space 
      fp = fopen(temp, "a"); 
      fprintf(fp, "%c", c); 
      fclose(fp); 
     } 
    } 

} while (c!='\n'); 

从int转换只为char工作,如果数量为0至9

+0

嗯我明白你的想法,但是当我尝试运行该程序时,我得到“Segmentation Fault”:/任何想法为什么? – Jacob 2014-11-03 08:31:10

+0

@Jacob我想我解决了这个问题。我在只读内存中分配温度,这是造成问题的原因。请尝试这个版本,并告诉我它是否工作:D – SlySherZ 2014-11-03 09:43:12

+0

嗯它可以编译和运行。但创建的文件只是名为Sensor_data?.txt,当我编写sudo nano Sensor_data?.txt时,它会打开7个填充随机数的文件。所以我不知道写入不同的文件部分作品。看起来像它没有得到正确的数据至少 – Jacob 2014-11-03 10:28:22

有了这个代码:

FILE *fp; 
    char c; 
    do{ 
     while(!read(fd, &c, 1));  
    }while (c!='$');  

    char int_to_char(int n){ 
    return (char) n + (int)'0'; 
} 

int reading = 1; 
do{ 
    while(!read(fd, &c, 1));   // <- you should change this 

    printf("%c",c); 
    fp = fopen("/var/www/Sensor_data.txt", "a"); 
    fprintf(fp, "%c", c); 
    fclose(fp); 

    if (c == ',') 
     reading++ ; 

    else{   
     if (reading >= 2 && reading <= 8){    // 2nd to 8th reading 
      char temp[] = "/var/www/Sensor_data .txt"; // <- Added a space, which I'm 
                // gonna substitute with a number 
      temp[20] = int_to_char(reading);   // Substitute the space 
      fp = fopen(temp, "a"); 
      fprintf(fp, "%c", c); 
      fclose(fp); 
     } 
    } 

} while (c!='\n'); 

我得到523.488072,0.326624,0.471786,0.472091,0.317701,0.430966,0.292683,0.260292,,0.653686,0.608487,0.116710,806.211857,在Sensor_data.txt文件中。 在其他7个文件中,我得到2 3。 4 8 8 0. 1每个文件中的值。 我打算用粗体将7值标记为7个单独的文件。我不知道这是否清楚,以及它发生了什么,为你SlySherZ?

只要读取int到char之间的转换,只有在0-9之间的数字可以成为问题时才起作用?

+0

'read'将在失败时返回-1,-1被认为是true。 – 2014-11-03 13:10:18

+0

@Jacob这段代码不会运行,因为您定义了一个函数错误的地方。 – SlySherZ 2014-11-03 18:17:47

+0

现在就开始工作:)非常感谢您的帮助和耐心! SlySherZ – Jacob 2014-11-04 08:22:24