2014-10-31 68 views
0

伊夫這樣寫代碼,從傳感器讀取數據:處理數據 - 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

回答

0

像這樣的事情會做的。可伸縮性不高,雖然: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

0

有了這個代碼:

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