Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bluetooth 如何将浮点值从一个蓝牙模块发送到另一个蓝牙模块(HC 05)_Bluetooth_Arduino_Arduino Uno_Hc 05 - Fatal编程技术网

Bluetooth 如何将浮点值从一个蓝牙模块发送到另一个蓝牙模块(HC 05)

Bluetooth 如何将浮点值从一个蓝牙模块发送到另一个蓝牙模块(HC 05),bluetooth,arduino,arduino-uno,hc-05,Bluetooth,Arduino,Arduino Uno,Hc 05,我正在做一个项目,我需要从一个arduino中的超声波传感器无线发送数据到另一个arduino,我需要串行监视器中的这些值。但问题是我无法通过蓝牙发送这些值。我试图发送一个字符,它出现在串行监视器中。。但当我尝试对整数值使用相同的值时,它并没有出现在串行监视器中。 我已经为蓝牙配置了主模式和从模式。我已经上传了我用来发送这些值的代码的图像。请帮我做这个。提前谢谢 code //@ transmitting end #define trigPin 12 #define echoPin 11

我正在做一个项目,我需要从一个arduino中的超声波传感器无线发送数据到另一个arduino,我需要串行监视器中的这些值。但问题是我无法通过蓝牙发送这些值。我试图发送一个字符,它出现在串行监视器中。。但当我尝试对整数值使用相同的值时,它并没有出现在串行监视器中。 我已经为蓝牙配置了主模式和从模式。我已经上传了我用来发送这些值的代码的图像。请帮我做这个。提前谢谢

 code 

//@ transmitting end
#define trigPin 12
#define echoPin 11

void setup() {

  Serial.begin(38400); // Default communication rate of the Bluetooth module
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {


 long duration;
  float distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

 Serial.println(distance,2); // Sends floatValue 
 delay(500);

}


//@ receving end

#include <SoftwareSerial.h>
#define led 13
SoftwareSerial BTSerial(10, 11);
int data=0;
void setup() {

  pinMode(led,OUTPUT);
  Serial.begin(38400);
  BTSerial.begin(38400); // Default communication rate of the Bluetooth module
}

void loop() {
  int number;
 if(Serial.available() > 0){ // Checks data is  from the serial port
 data = BTSerial.read(); // Reads the data from the serial port
 //analogWrite(led,data);
 delay(10);
 //Serial.println(data);

 }
 Serial.println(data);
}
code
//@发射端
#定义trigPin 12
#定义echoPin 11
无效设置(){
Serial.begin(38400);//蓝牙模块的默认通信速率
引脚模式(trigPin,输出);
pinMode(echoPin,输入);
}
void循环(){
持续时间长;
浮动距离;
digitalWrite(trigPin,低);//添加了此行
延迟微秒(2);//添加了这一行
数字写入(trigPin,高);
延迟微秒(10);//添加了这一行
数字写入(trigPin,低电平);
持续时间=脉冲强度(echoPin,高);
距离=(持续时间/2)/29.1;
Serial.println(距离,2);//发送浮点值
延迟(500);
}
//@接收端
#包括
#定义led 13
软件串行BTSerial(10,11);
int数据=0;
无效设置(){
引脚模式(led,输出);
Serial.begin(38400);
BTSerial.begin(38400);//蓝牙模块的默认通信速率
}
void循环(){
整数;
如果(Serial.available()>0){//检查数据是否来自串行端口
data=BTSerial.read();//从串行端口读取数据
//模拟写入(led,数据);
延迟(10);
//Serial.println(数据);
}
Serial.println(数据);
}
我需要串行监视器上的整数值。但是这里我得到了一些符号,比如?/…

来自,
Serial.read()
只读取串行缓冲区中可用的第一个字节。由于
int
是在8个字节上编码的,因此我认为您需要按顺序读取传入的字节,以获得完整的值

也许您可以通过将
(Serial.available()>0)
放入
while
循环中来实现这一点,例如,将
字符[8]
中得到的值串联起来,然后将该
字符
转换为整数值

另外,请注意,您发送的是
浮点值
,而不是
int

,从
Serial.read()
只读取串行缓冲区中可用的第一个字节。由于
int
是在8个字节上编码的,因此我认为您需要按顺序读取传入的字节,以获得完整的值

也许您可以通过将
(Serial.available()>0)
放入
while
循环中来实现这一点,例如,将
字符[8]
中得到的值串联起来,然后将该
字符
转换为整数值

另外,请注意,您发送的是
浮动
,而不是
int

谢谢您的帮助。。! 我修改了接收端的代码,以便从发送器获取浮点值。。这是我修改过的代码

#include <SoftwareSerial.h>
int bluetoothTx = 10; 
int bluetoothRx = 11;
String content; //content buffer to concatenate characters

char character; //To store single character


SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup(){
  bluetooth.begin(38400);
  Serial.begin(9600);
}

void loop(){
  bluetooth();

}

void bluetooth(){ //
  while(bluetooth.available()){
    character = bluetooth.read();
    content.concat(character);
   if(character == '\r'){ // find if there is carriage return
     Serial.print(content); //display content (Use baud rate 9600)
     content = ""; //clear buffer
     Serial.println();
  }
  }
}
#包括
int bluetoothTx=10;
int bluetoothRx=11;
字符串内容//用于连接字符的内容缓冲区
字符//存储单个字符
软件串行蓝牙(bluetoothTx、bluetoothRx);
无效设置(){
蓝牙开始(38400);
Serial.begin(9600);
}
void循环(){
蓝牙();
}
无效蓝牙(){//
while(bluetooth.available()){
character=bluetooth.read();
内容:concat(字符);
if(character=='\r'){//查找是否有回车符
Serial.print(内容);//显示内容(使用波特率9600)
content=”“;//清除缓冲区
Serial.println();
}
}
}
谢谢你的帮助。。! 我修改了接收端的代码,以便从发送器获取浮点值。。这是我修改过的代码

#include <SoftwareSerial.h>
int bluetoothTx = 10; 
int bluetoothRx = 11;
String content; //content buffer to concatenate characters

char character; //To store single character


SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup(){
  bluetooth.begin(38400);
  Serial.begin(9600);
}

void loop(){
  bluetooth();

}

void bluetooth(){ //
  while(bluetooth.available()){
    character = bluetooth.read();
    content.concat(character);
   if(character == '\r'){ // find if there is carriage return
     Serial.print(content); //display content (Use baud rate 9600)
     content = ""; //clear buffer
     Serial.println();
  }
  }
}
#包括
int bluetoothTx=10;
int bluetoothRx=11;
字符串内容//用于连接字符的内容缓冲区
字符//存储单个字符
软件串行蓝牙(bluetoothTx、bluetoothRx);
无效设置(){
蓝牙开始(38400);
Serial.begin(9600);
}
void循环(){
蓝牙();
}
无效蓝牙(){//
while(bluetooth.available()){
character=bluetooth.read();
内容:concat(字符);
if(character=='\r'){//查找是否有回车符
Serial.print(内容);//显示内容(使用波特率9600)
content=”“;//清除缓冲区
Serial.println();
}
}
}

您似乎将其作为文本发送,但将其作为数字读取。你需要把它读成文本,然后转换成一个数字。你似乎把它当作文本发送,但读起来却是一个数字。您需要将其读取为文本,然后将其转换为数字。
根据具体情况,int按8字节编码。在小的Arduino上,int是2个字节。最终更容易发送可读文本,包括在发送二进制文件时终止字符,也可能是单个字节(0..255)。无论如何,你应该理解数据类型。是的,你是对的。无论如何,它不是一个字节,所以这甚至可能是问题所在。
int是在8个字节上编码的,这取决于。在小的Arduino上,int是2个字节。最终更容易发送可读文本,包括在发送二进制文件时终止字符,也可能是单个字节(0..255)。无论如何,你应该理解数据类型。是的,你是对的。无论如何,它不是一个字节,所以这可能就是问题所在。