2016-10-19 812 views
0

我想向Arduino發送兩個不同的十進制值。發送給Arduino的值用逗號(,)分隔:如何向Arduino串行發送多個不同的浮點數或小數值?

例如, 1.23,4.56

我的問題是,當數值由Arduino微控制器接收時,代碼似乎不會輸出所需的結果。

兩個在下面的代碼看到的Serial.println命令輸出用於變量值_1和值_2以下:

1.20

0.00

4.50

0.00

所以我不明白的是爲什麼這兩個變量都有一個額外的「0.00」值。

在此先感謝。

const int MaxChars = 3; // an int string contains up to 3 digits (3 s.f.) and 
         // is terminated by a 0 to indicate end of string 
char strValue_1[MaxChars+1]; // must be big enough for digits and terminating null 
char strValue_2[MaxChars+1]; // must be big enough for digits and terminating null 
int index_1 = 0;   // the index into the array storing the received digits 
int index_2 = 0;   // the index into the array storing the received digits 
double value_1; 
double value_2; 

void setup() 
{ 
    Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud 
} 

void loop() 
{ 
if(Serial.available()) 
{ 
char ch = Serial.read(); 
if(index_1 < MaxChars && ch >= '.' && ch <= '9') 
{ 
    strValue_1[index_1++] = ch; // add the ASCII character to the array; 

} 
else if (ch == ',') 
{ 
    if(index_2 < MaxChars && ch >= '.' && ch <= '9') 
    { 
     strValue_2[index_2++] = ch; // add the ASCII character to the array; 
    } 
} 
else 
{ 
    // here when buffer full or on the first non digit 
    strValue_1[index_1] = 0;  // terminate the string with a 0 
    strValue_2[index_2] = 0;  // terminate the string with a 0 
    value_1 = atof(strValue_1);  // use atof to convert the string to an float 
    value_2 = atof(strValue_2);  // use atof to convert the string to an float 
    Serial.println(value_1); 
    Serial.println(value_2); 
    index_1 = 0; 
    index_2 = 0; 
} 
} 
} 

下面是代碼的最新編輯的版本由@mactro和@aksonlyaks的建議,我仍然無法獲得所需的輸出,因此我願意接受更多的建議。

作爲電流I接收的1.23,4.56以下變量的特定輸入的輸出的是:

strValue的[0]:

1.2

strValue中[1]:

1.2

4.5

VALU E_1:

1.20

0.00

_2:

1.20

4.50

在此先感謝。

這裏是代碼的最新版本:

const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '\0' and 
        // is terminated by a 0 to indicate end of string 

const int numberOfFields = 2; //Amount of Data to be stored 
char strValue[numberOfFields][MaxChars+1]; // must be big enough for digits and terminating null 

int index_1 = 0;   // the index into the array storing the received digits 

double value_1; 
double value_2; 

int arrayVal = 0; 

void setup() 
{ 
    Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud 
} 

void loop() 
{ 

if(Serial.available()) 
{ 
    char ch = Serial.read(); 

if (ch == ',') 
{ 
    arrayVal = 1; 

    if(index_1 < MaxChars-1 && ch >= '.' && ch <= '9') 
    { 
     strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 
    } 
    if(index_1 == MaxChars - 1) 
    { 
     strValue[arrayVal][index_1++] = '\0'; 
    } 

} 
else if(index_1 < MaxChars-1 && ch >= '.' && ch <= '9') 
{ 
    strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 

if(index_1 == MaxChars - 1) 
{ 
    strValue[arrayVal][index_1++] = '\0'; 
} 

} 

else 
{ 

    value_1 = atof(strValue[0]);  // use atof to convert the string to an float 
    value_2 = atof(strValue[1]);  // use atof to convert the string to an float 
    Serial.println(value_1); 
    Serial.println(value_2); 
    index_1 = 0; 
    arrayVal = 0; 
} 
} 
} 

回答

0

我對你的代碼做了一些修改,現在它打印你的願望。修改後的代碼如下:

const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '\0' and 
        // is terminated by a 0 to indicate end of string 

const int numberOfFields = 2; //Amount of Data to be stored 
char strValue[numberOfFields][MaxChars+1]; // must be big enough for digits and terminating null 

int index_1 = 0;   // the index into the array storing the received digits 

double value_1; 
double value_2; 

int arrayVal = 0; 

void setup() 
{ 
    Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud 
} 

void loop() 
{ 

    if(Serial.available()) 
    { 
     char ch = Serial.read(); 

     if (ch == ',') 
     { 
      arrayVal = 1; 
      index_1 = 0; // Initialise this to zero for the float value received after ',' 
/* 
      if(index_1 < MaxChars-1 && ch >= '.' && ch <= '9') 
      { 
       strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 
      } 
      if(index_1 == MaxChars - 1) 
      { 
       strValue[arrayVal][index_1++] = '\0'; 
      } 
*/ 
     } 
     else if((index_1 < MaxChars + 1) && (ch >= '.' && ch <= '9')) // one float value size including null character is 5 (1.23 size 4) 
     { 
      strValue[arrayVal][index_1++] = ch; // add the ASCII character to the array; 
      if(index_1 == MaxChars)    // When we have recevied the 4 character of float value add NULL character 
      { 
       strValue[arrayVal][index_1++] = '\0'; 
      } 
     }else 
     { 
      value_1 = atof(strValue[0]);  // use atof to convert the string to an float 
      value_2 = atof(strValue[1]);  // use atof to convert the string to an float 
      Serial.println(value_1); 
      Serial.println(value_2); 
      index_1 = 0; 
      arrayVal = 0; 
     } 
    } 
} 

此外,我爲您的代碼做了適當的縮進。

讓我知道這是否有幫助。

Regards

+0

是其作爲現在需要的,謝謝所有幫助打印值。 –

0

你永遠不添加任何strValue_2,因爲

if(index_2 < MaxChars && ch >= '.' && ch <= '9') 
{ 
    strValue_2[index_2++] = ch; // add the ASCII character to the array; 
} 

僅被執行時ch==','。當你收到逗號時,你應該設置一個標誌,這個標誌表示一個代碼可以寫入更多字符到strValue_2而不是strValue_1。或者你可以有一個像char strValues[2][MaxChars+1]這樣的字符串數組,以及您寫入的元素的更改和索引strValues[stringNumber][index++]

+0

我給你的方法使用字符串數組試試,我已經將代碼添加到我的問題,你可以看到上面。然而,我仍然無法解決這個問題,你能否給我更多的提示,我這次用新代碼做錯了什麼。我認爲問題在於設置國旗,也許你可以提出一個辦法,讓我能夠更好地做到這一點。非常感謝您的反饋。 –

+0

@bat_wave你現在得到什麼輸出?試試'Serial.println(strValue [0])',這樣你就會知道緩衝區裏面有什麼。 – mactro

+0

我試過'Serial.println(strValue [0])'。我輸入值:1.23,4.56。輸出值爲: 1.2 1.2 我不確定是什麼導致了重複。 並且當我對'1. Serial.println(strValue [1])'做同樣的操作時,對於相同的輸入1.23,4.56。我得到的輸出: 4.5 的ATOF轉換後,給_1: 1.20 1.20 –

相關問題