0

2 つの異なる 10 進数値を Arduino にシリアルで送信しようとしています。Arduino に送信される値は、コンマ (,) で区切られます。

例えば ​​1.23,4.56 の場合

私の問題は、値が Arduino マイクロコントローラーによって受信されたときに、コードが目的の結果を出力していないように見えることです。

以下のコードに示されている Serial.println コマンドは、変数 value_1 と value_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 によって提案されたコードの最新の編集バージョンです。目的の出力をまだ取得できていません。したがって、より多くの提案を受け付けています。

現在、次の変数の特定の入力 1.23,4.56 に対して受け取る出力は次のとおりです。

strValue[0]:

1.2

strValue[1]:

1.2

4.5

値_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;
}
}
}
4

2 に答える 2