在Arduino上使用串行软件发送3个浮点变量 [英] Sending 3 float variables with serial software on Arduino
问题描述
我正在尝试使用软件串行库通过带有Arduino的无线模块发送机器人的位置.我发现它一次只能发送1个字节.我发送的号码不能超过255,我需要发送彩信直到40000.我该怎么做?
I'm trying to send position of my robot through wireless module with Arduino using Software Serial library. I found that it can only send 1 byte at time. I can't send more than number 255 and I need to send floats till 40000. How I can do that?
这是我的发射器的一个示例:
Here is an example of my transmitter:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() // run over and over
{
float i=40000;
mySerial.write(i);
//Serial.println(i);}
}
我的收信人
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // RX, TX
int i=0;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
if(mySerial.available()){
i=mySerial.read();
Serial.println(i);
}
}
推荐答案
您必须使用 Serial.println(floatVal);
将其以纯文本格式发送,并带有任何空格分隔符,并通过 Serial.parseFloat()
方法.
You have to use Serial.println(floatVal);
to send it in plain text with any white space separator and receive it by Serial.parseFloat()
method.
write
方法非常适合发送原始数据(单个字符,c字符串或某些缓冲区)
The write
method is good for sending raw data (single character, c-string or some buffer)
如果您真的想以二进制形式发送浮点数,则必须将其作为缓冲区传递: Serial.write((uint8_t *)& floatVal,sizeof(intVal));
.在接收器端,您还必须阅读整个浮动. read()
方法仅读取一个字符.
If you really want sending floats in its binary form, you have to pass it as an buffer: Serial.write((uint8_t*)&floatVal, sizeof(intVal));
. And on the receiver side you have to read whole float too. The read()
method reads only one character.
这篇关于在Arduino上使用串行软件发送3个浮点变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!