如何向Arduino串行发送多个不同的浮点数或十进制值?
How to send multiple different floating point or decimal values serially to Arduino?
我正在尝试将两个不同的十进制值串行发送到 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 的输出是:
str值[0]:
1.2
str值[1]:
1.2
4.5
value_1:
1.20
0.00
value_2:
1.20
4.50
提前致谢。
这是代码的最新版本:
const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '[=11=]' 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++] = '[=11=]';
}
}
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++] = '[=11=]';
}
}
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;
}
}
}
您永远不会向 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++]
.
的元素的更改和索引
我对您的代码做了一些修改,现在可以打印您想要的内容。修改后的代码如下:
const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '[=10=]' 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++] = '[=10=]';
}
*/
}
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++] = '[=10=]';
}
}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;
}
}
}
此外,我为您的代码做了适当的缩进。
如果有帮助请告诉我。
此致
我正在尝试将两个不同的十进制值串行发送到 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 的输出是:
str值[0]:
1.2
str值[1]:
1.2
4.5
value_1:
1.20
0.00
value_2:
1.20
4.50
提前致谢。
这是代码的最新版本:
const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '[=11=]' 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++] = '[=11=]';
}
}
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++] = '[=11=]';
}
}
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;
}
}
}
您永远不会向 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++]
.
我对您的代码做了一些修改,现在可以打印您想要的内容。修改后的代码如下:
const int MaxChars = 4; // an int string contains up to 3 digits (3 s.f.) including the '[=10=]' 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++] = '[=10=]';
}
*/
}
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++] = '[=10=]';
}
}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;
}
}
}
此外,我为您的代码做了适当的缩进。
如果有帮助请告诉我。
此致